HDU 2642 (tree-like array template two-dimensional)

Source: Internet
Author: User

Stars

Time limit:5000/2000 MS (java/others) Memory limit:32768/65536 K (java/others)
Total submission (s): 1350 Accepted Submission (s): 554

Problem Description
Yifenfei is a romantic guy and he likes to count the stars in the sky.
The problem Easier,we considerate the sky is a two-dimension plane. Sometimes the star would be bright and sometimes the star would be dim. At First,there are no bright star in the Sky,then some information would 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" mea n 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 is in the region correspond x1,x 2,y1,y2.
There is the only one case.

Input
The first line contain a m (M <= 100000) and then M Line followed.
Each line start with a operational character.
If the character is B or d,then, x, y (0 <=x,y<=) followed.
If the character is a Q then four integer x1,x2,y1,y2 (0 <=x1,x2,y1,y2<=) followed.

Output
For each query,output the number of bright stars on 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

Standard two-dimensional template problem

#include <stdio.h> #include <iostream> #include <string.h>using namespace Std;const int N=1010;int c[n ][n];bool vis[n][n];int lowbit (int x) {return x& (-X);}    void Add (int x,int y,int val) {int i=y;        while (x<=n) {y=i;            while (y<=n) {c[x][y]+=val;        Y+=lowbit (y);    } x+=lowbit (x);    }}int sum (int x,int y) {int i,sum=0;    I=y;        while (x) {y=i;            while (y) {sum+=c[x][y];        Y-=lowbit (y);    } x-=lowbit (x); } return sum;}    int main () {int n,x1,x2,y1,y2;    scanf ("%d", &n);    Memset (C,0,sizeof (c));    memset (vis,0,sizeof (VIS));        while (n--) {GetChar ();        char c;        scanf ("%c", &c);            if (c== ' Q ') {scanf ("%d%d%d%d", &x1,&x2,&y1,&y2);            x1++,x2++,y1++,y2++;            if (x1>x2) swap (X1,X2);            if (y1>y2) swap (Y1,Y2); printf ("%d\n", (SUM (x2,y2) +sum (x1-1,y1-1)-sum (x1-1,y2)-sum (x2,y1-1));            } else if (c== ' B ') {scanf ("%d%d", &x1,&y1);            if (vis[x1+1][y1+1]==1) continue;            else Add (x1+1,y1+1,1);        Vis[x1+1][y1+1]=1;            } else if (c== ' D ') {scanf ("%d%d", &x1,&y1);            if (vis[x1+1][y1+1]==0) continue;            else Add (x1+1,y1+1,-1);        vis[x1+1][y1+1]=0; }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2642 (tree-like array template two-dimensional)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.