HDU_2642_二维树状数组

Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1628    Accepted Submission(s):
683

Problem Description

Yifenfei is a romantic guy and he likes to count the
stars in the sky.
To make the problem easier,we considerate the sky is a
two-dimension plane.Sometimes the star will be bright and sometimes the star
will be dim.At first,there is no bright star in the sky,then some information
will be given as "B x y" where ‘B‘ represent bright and x represent the X
coordinate and y represent the Y coordinate means the star at (x,y) is
bright,And the ‘D‘ in "D x y" mean the star at(x,y) is dim.When get a query as
"Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the
region correspond X1,X2,Y1,Y2.

There is only one case.

Input

The first line contain a M(M <= 100000), then M line
followed.
each line start with a operational character.
if the character
is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the
character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000)
followed.

Output

For each query,output the number of bright stars in one
line.

Sample Input

5

B 581 145

B 581 145

Q 0 600 0 200

D 581 145

Q 0 600 0 200

Sample Output

1

0

初学树状数组,看了一些文章,算是有了初步的认识。

自己的理解:用二进制表示的方法来实现二分。一个二进制数c[k]表示的是从a[k]开始往前的lowbit(k)个数的和(lowbit(k)为k的最低位,也即将k的除最低位外的所有为置为0)。

更新:

void add(int k,int x)
{
    for(int i=k;i<MAXN;i+=lowbit(i))
        c[i]+=x;
}

lowbit():

int lowbit(int x)   //取最低位
{
    return x&(-x);
}

查询:

int get_sum(int k)
{
    int res=0;
    for(int i=k;i>0;i-=lowbit(i))
        res+=c[i];
    return res;
}

这道题是一道二维树状数组,原理其实也就是这样。

注意:题目中坐标从0开始,可能对一颗star做两次同样的操作。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stdlib.h>
#include<stack>
#include<vector>
using namespace std;

const int MAXN=1010;
int a[MAXN][MAXN];
bool b[MAXN][MAXN];

int lowbit(int x)
{
    return x&(-x);
}

void modify(int x,int y,int data)
{
    for(int i=x; i<MAXN; i+=lowbit(i))
        for(int j=y; j<MAXN; j+=lowbit(j))
            a[i][j]+=data;
}

int getsum(int x,int y)
{
    int res=0;
    for(int i=x; i>0; i-=lowbit(i))
        for(int j=y; j>0; j-=lowbit(j))
            res+=a[i][j];
    return res;
}

int main()
{
    int n,x,y,x1,y1;
    char str[2];
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",str);
        if(str[0]==‘B‘)
        {
            scanf("%d%d",&x,&y);
            x++;
            y++;
            if(b[x][y]) continue;
            modify(x,y,1);
            b[x][y]=1;
        }
        else if(str[0]==‘D‘)
        {
            scanf("%d%d",&x,&y);
            x++;
            y++;
            if(b[x][y]==0) continue;
            modify(x,y,-1);
            b[x][y]=0;
        }
        else
        {
            scanf("%d%d%d%d",&x,&x1,&y,&y1);
            x++;x1++;y++;y1++;
            if(x>x1) swap(x,x1);
            if(y>y1) swap(y,y1);
            int ans=getsum(x1,y1)-getsum(x-1,y1)-getsum(x1,y-1)+getsum(x-1,y-1);
            printf("%d\n",ans);
        }
    }
    return 0;
}
时间: 2024-08-05 02:01:11

HDU_2642_二维树状数组的相关文章

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

HDOJ 4456 Crowd 离散化+二维树状数组

将坐标旋转45度就可以得到正方形,可以用二维树状数组求解... 为了节省内存,提前将树状数组中会被更新的点全都存下来,并离散化 Crowd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1199    Accepted Submission(s): 282 Problem Description City F in the south

POJ2029:Get Many Persimmon Trees(二维树状数组)

Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of

POJ1195 Mobile phones 【二维树状数组】

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14288   Accepted: 6642 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

MooFest_二维树状数组

Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer,

HDU_4456_二维树状数组

http://acm.hdu.edu.cn/showproblem.php?pid=4456 第一道二维树状数组就这么麻烦,题目要计算的是一个菱形范围内的和,于是可以把原来的坐标系旋转45度,就是求一个正方形范围内的和,这里还涉及到坐标系平移和放大,由于题目数据较大,用了离散化来保存需要处理的点,放在h数组内,对应的二维树状数组存在tree数组内. #include<iostream> #include<cstring> #include<cstdio> #includ