程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 2642 二維樹狀數組 單點更新區間查詢 模板水題

hdu 2642 二維樹狀數組 單點更新區間查詢 模板水題

編輯:C++入門知識

Stars
Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 785    Accepted Submission(s): 335

 

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


Author
teddy
 


Source
百萬秦關終屬楚
 


Recommend
teddy
 
 
 
 
題意:  有一個地圖   每個點都是一個燈    輸入B 之後輸入坐標 表示對應的點的燈變亮    輸入D  輸入坐標 表示變暗  輸入Q  輸入一個矩形的左下角和右上角 問矩形內的亮著的燈的個數
 
思路:
很明顯的 二維樹狀數組  注意 燈亮過了就不能再亮了 而且燈關了就不能再關了 所以可以用一個flag數組標記是否可以進行關燈開燈操作   
 
注意下標要從1開始  題目從0開始  所以要加1
 

#include<stdio.h>
#include<string.h>
const int N=1003;
int c[N+5][N+5],flag[N+5][N+5],n,m;
int mmax(int a,int b)
{
    return  a>b?a:b;
}
int lowbit(int x)
{
	return x&(-x);
}

void update(int x,int y,int delta )
{
	int i, j;
	for(i=x;i<=N;i+=lowbit(i))
	{
		for(j=y; j<=N; j+=lowbit(j))
		{
			c[i][j] += delta;
		}
	}
}

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

int main()
{
	int x1,x2,y1,y2;

	while(scanf("%d",&m)!=EOF)
	{
	    memset(c,0,sizeof(c));
	    memset(flag,0,sizeof(flag));
	    while(m--)
        {
            char s[2];
            scanf("%s",s);
            if(s[0]=='Q')
            {
                scanf("%d %d %d %d",&x1,&x2,&y1,&y2);
                x1++;y1++;x2++;y2++;
                if(x1>x2) {int temp=x1;x1=x2;x2=temp;}
                if(y1>y2) {int temp=y1;y1=y2;y2=temp;}
                int ans=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1);
                printf("%d\n",ans);
            }
            else  if(s[0]=='B')
            {
                scanf("%d %d",&x1,&y1);
                x1++;y1++;
                if(flag[x1][y1]==1) continue;
                flag[x1][y1]=1;
                update(x1,y1,1);
            }
            else
                {
                   scanf("%d %d",&x1,&y1);
                   x1++;y1++;
                   if(flag[x1][y1]==0) continue;
                   flag[x1][y1]=0;
                   update(x1,y1,-1);
                }
        }
	}
	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved