树状数组二维扩展

See you~

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3418    Accepted Submission(s): 1075


Problem Description
Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanced to the World Finals last year.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.
 

Input
In the first line of the input file there is an Integer T(1<=T<=10), which means the number of test cases in the input file. Then N test cases are followed.
For each test case, in the first line there is an Integer Q(1<Q<=100,000), means the queries of the case. Then followed by Q queries.
There are 4 kind of queries, sum, add, delete and move.
For example:
S x1 y1 x2 y2 means you should tell me the total books of the rectangle used (x1,y1)-(x2,y2) as the diagonal, including the two points.
A x1 y1 n1 means I put n1 books on the position (x1,y1)
D x1 y1 n1 means I move away n1 books on the position (x1,y1), if less than n1 books at that position, move away all of them.
M x1 y1 x2 y2 n1 means you move n1 books from (x1,y1) to (x2,y2), if less than n1 books at that position, move away all of them.
Make sure that at first, there is one book on every grid and 0<=x1,y1,x2,y2<=1000,1<=n1<=100.
 

Output
At the beginning of each case, output "Case X:" where X is the index of the test case, then followed by the "S" queries.
For each "S" query, just print out the total number of books in that area.
 

Sample Input
  
  
2 3 S 1 1 1 1 A 1 1 2 S 1 1 1 1 3 S 1 1 1 1 A 1 1 2 S 1 1 1 2
 

Sample Output
  
  
Case 1: 1 3 Case 2: 1 4
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int C[1010][1010];
int Sum(int x, int y)
{
    int ret = 0;
    for (int i = x; i > 0; i -= (i & (-i)))
        for (int j = y; j > 0; j -= (j & (-j)))
        {
            ret += C[i][j];
        }
    return ret;
}
void add(int x, int y, int d)
{
    for (int i = x; i < N; i += (i & (-i)))
        for (int j = y; j < N; j += (j & (-j)))
        {
            C[i][j] += d;
        }
}
int cnt = 1;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n;
        int x1, y1, x2, y2, n1;
        char op[1000];
        scanf("%d", &n);
        memset(C, 0, sizeof(C));
        printf("Case %d:\n", cnt++);
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < N; j++)
            {
                add(i, j, 1);
            }
        }
        for (int i = 0; i < n; i++)
        {
            scanf("%s", op);
            if (op[0] == 'S')
            {
                scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
                if (x1 < x2)swap(x1, x2);
                if (y1 < y2)swap(y1, y2);
                int ans = Sum(x1 + 1, y1 + 1) - Sum(x1 + 1, y2) - Sum(x2, y1 + 1) + Sum(x2, y2);
                printf("%d\n", ans);
            }
            else if (op[0] == 'A')
            {
                scanf("%d%d%d", &x1, &y1, &n1);
                add(x1 + 1, y1 + 1, n1);
            }
            else if (op[0] == 'D')
            {
                scanf("%d%d%d", &x1, &y1, &n1);
                int v = Sum(x1 + 1, y1 + 1) - Sum(x1 + 1, y1) - Sum(x1, y1 + 1) + Sum(x1, y1);
                if (v < n1)n1 = v;
                add(x1 + 1, y1 + 1, -n1);
            }
            else
            {
                scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n1);
                int v = Sum(x1 + 1, y1 + 1) - Sum(x1, y1 + 1) - Sum(x1 + 1, y1) + Sum(x1, y1);
                if (v < n1)n1 = v;
                add(x1 + 1, y1 + 1, -n1);
                add(x2 + 1, y2 + 1, n1);
            }
        }
    }

    return 0;
}

Stars

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


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



#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int C[N][N];
int state[N][N];
int Sum(int x, int y)
{
    int sum = 0;
    for (int i = x; i > 0; i -= (i & -i))
    {
        for (int j = y; j > 0; j -= (j & -j))
        {
            sum += C[i][j];
        }
    }
    return sum;
}
void Add(int x, int y, int d)
{
    for (int i = x; i < N; i += (i & -i))
    {
        for (int j = y; j < N; j += (j & -j))
        {
            C[i][j] += d;
        }
    }
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n) + 1)
    {
        char op[100];
        int x1, y1, x2, y2;
        memset(C, 0, sizeof(C));
        memset(state, 0, sizeof(state));
        for (int i = 0; i < n; i++)
        {
            scanf("%s", op);
            if (op[0] == 'B')
            {
                scanf("%d%d", &x1, &y1);
                if (!state[x1][y1])
                {
                    state[x1][y1] = 1;
                    Add(x1 + 1 , y1 + 1 , 1);
                }
            }
            else if (op[0] == 'Q')
            {
                scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
                if (x1 < x2)swap(x1, x2);
                if (y1 < y2)swap(y1, y2);
                int ans = Sum(x1 + 1, y1 + 1) - Sum(x1 + 1, y2) - Sum(x2, y1 + 1) + Sum(x2, y2);
                printf("%d\n", ans);
            }
            else if (op[0] == 'D')
            {
                scanf("%d%d", &x1, &y1);
                if (state[x1][y1])
                {
                    state[x1][y1] = 0;
                    Add(x1 + 1 , y1 + 1, -1);
                }
            }
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值