HDU 2642 二维树状数组

题意很明确 给你一个图某坐标上的星星亮 暗 条件 求出当前区间内所有亮星星的总数 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
int tree[maxn + 2][maxn + 2];
inline int Lowbit(int x)
{
    return x&(-x);
}
inline void Update(int x,int y,int v)
{
    for(int i=x; i<=maxn; i+=Lowbit(i))
        for(int j=y; j<=maxn; j+=Lowbit(j))
            tree[i][j]+=v;
}
inline int Query(int x,int y)
{
    int sum=0;
    for(int i=x; i>0; i-=Lowbit(i))
        for(int j=y; j>0; j-=Lowbit(j))
            sum+=tree[i][j];
    return sum;
}
bool map[1005][1005];
int main()
{
    int n;
    char c[3];
    memset(tree,0,sizeof(tree));
    memset(map,0,sizeof(map));
    cin>>n;
    while(n--)
    {
        scanf("%s",c);
        if(c[0]=='B')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(!map[x+1][y+1])
                map[x+1][y+1]=1,Update(x+1,y+1,1);
        }
        else if(c[0]=='D')
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(map[x+1][y+1]==1)
                map[x+1][y+1]=0,Update(x+1,y+1,-1);
        }
        else if(c[0]=='Q')
        {
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
            if(x1>x2) swap(x1,x2);
            if(y1>y2) swap(y1,y2);
            int ans=Query(x2+1,y2+1)-Query(x1,y2+1)-Query(x2+1,y1)+Query(x1,y1);
            printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-08-06 20:34:25

HDU 2642 二维树状数组的相关文章

HDU 1892 二维树状数组

题意给的操作讲的很明白 注意不能出现负数 坐标值可能为0 两个坐标大小不能确定  #include <iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1002; int tree[maxn + 2][maxn + 2]; int n; inline int Lowbit(int x) { return x&(-x); } inline void

poj 2155 Matrix 二维树状数组

   经典题,把一个-=写成=-了查了半天都没查出来-- /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include

[php]运用变量引用实现一维数组转多维树状数组

/** * 运用 变量引用 实现 一维数组 转 多维树状数组 * @param $array * @param array $options = ['id'=>'id', 'pid'=>'pid', 'sub'=>'_sub', 'root'=>0] * @return array */ public static function array2Tree($array, $options = []) { /** merge Options */ $opt = array_merge

树状数组专题【完结】

1 国外的论文点击打开链接 2 我的总结点击打开链接 任何能够造成树状数组下表为0的操作都会使得程序TLE,这是个很重要的地方! 第一题 poj 2299 Ultra-QuickSort 点击打开poj 2299 思路: 离散化+树状数组 分析: 1 题目的意思就是要求逆序数对 2 题目的输入个数有500000的规模但是每个数的最大值为999999999,因此我们需要离散化这些数据 3 对于数据9 1 0 5 4我们离散化成5 2 1 4 3 那么对于输入一个树a[i]我们去求一下它的离散化后的

树状数组

                                                     1 一维树状数组   1 什么是树状数组        树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+...+A[n]的时,间是log级别的,而且是一个在线的数据结构.   2 树状数组作用        我们经常会遇到动态连续和查询问题,给定n个元素A[1~N],让我们求sum[L,R] = A[L]+...+A[R],或者更改A[i]

树状数组学习笔记

一.理论准备           从图上看出:C[1] = A[1],C[2] = A[1] + A[2],C[3] = A[3],C[4] = A[1] + A[2] + A[3] + A[4],C[5] = A[5],C[6] = A[5] + A[6],C[7] = A[7],C[8] = A[1] + A[2] + A[3] + A[4] + A[5] + A[6] + A[7] + A[8]. 二.发现规律         1(0001).3(0011).5(0101).7(0111)

hdu 1556 Color the ball 树状数组

    最基本的树状数组,成段更新,感觉只要构造的时候保证两端平衡,即对后面非更新地方无影响即可,所以这题是a++ (b+1)--    温习了下快速IO,速度果然提升1倍 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #

HDU 4311 树状数组+二分

题意:给出10W个点,找出一个点使得每点按网格走到它的步数和最小,求最小步数和.每步这么走Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).. a点到达b点的步数为abs(b.x-a.x)+abs(b.y-a.y) 那么a点到所有点的步数和为abs(pi.x-a.x)+abs(pi.y-ay)所以按照从小到大的顺序吧x,y的值排序,二分查找a.x a.y在数组中的位置,就是给上面那个求和在打开就变成abs(num

HDOJ/HDU 1556 Color the ball(树状数组)

Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗? Input 每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N