zoukankan      html  css  js  c++  java
  • HDU 1892 See you~ (二维树状数组)

    See you~

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


    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
     
    Author
    Sempr|CrazyBird|hust07p43
     
    Source
     
    Recommend
    lcy
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=1100;
    
    int arr[N][N],map[N][N];
    
    int lowbit(int x){
        return x&(-x);
    }
    
    void update(int i,int j,int val){
        while(i<N){
            int tmp=j;
            while(tmp<N){
                arr[i][tmp]+=val;
                tmp+=lowbit(tmp);
            }
            i+=lowbit(i);
        }
    }
    
    int Sum(int i,int j){
        int ans=0;
        while(i>0){
            int tmp=j;
            while(tmp>0){
                ans+=arr[i][tmp];
                tmp-=lowbit(tmp);
            }
            i-=lowbit(i);
        }
        return ans;
    }
    
    void swap(int &x,int &y){
        int tmp=x;x=y;y=tmp;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,cases=0;
        int x,x1,y1,x2,y2,q;
        char op[10];
        scanf("%d",&t);
        while(t--){
            printf("Case %d:
    ",++cases);
            scanf("%d",&q);
            memset(map,0,sizeof(map));
            memset(arr,0,sizeof(arr));
            while(q--){
                scanf("%s%d%d",op,&x1,&y1);
                x1++,   y1++;   //给它们加1,避免函数当它们为0时,函数update死循环
                switch(op[0]){
                    case 'S':{
                        scanf("%d%d",&x2,&y2);
                        x2++,   y2++;
                        if(x1>x2)   swap(x1,x2);    //坐标大小不确定
                        if(y1>y2)   swap(y1,y2);
                        printf("%d
    ",(Sum(x2,y2)+Sum(x1-1,y1-1)-Sum(x2,y1-1)-Sum(x1-1,y2))+(x2-x1+1)*(y2-y1+1));   //// 在这里将初始每个格子的值加上
                    }break;
                    case 'A':{
                        scanf("%d",&x);
                        map[x1][y1]+=x;
                        update(x1,y1,x);
                    }break;
                    case 'D':{
                        scanf("%d",&x);
                        int tmp=map[x1][y1];
                        if(tmp+1<x)
                            x=tmp+1;
                        update(x1,y1,-x);
                        map[x1][y1]-=x;
                    }break;
                    case 'M':{
                        scanf("%d%d%d",&x2,&y2,&x);
                        x2++,   y2++;
                        int tmp=map[x1][y1];
                        if(tmp+1<x)
                            x=tmp+1;
                        map[x1][y1]-=x;
                        map[x2][y2]+=x;
                        update(x1,y1,-x);
                        update(x2,y2,x);
                    }break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Github 上 36 个最实用的 Vue 开源库
    C 语言快速入门,21 个小项目足矣!「不走弯路就是捷径」
    18个挑战项目带你快速入门深度学习
    Linux 运维入门到跑路书单推荐
    Python 网络爬虫的常用库汇总
    45 个常用Linux 命令,让你轻松玩转Linux!
    [新手必备]Python 基础入门必学知识点笔记
    快速入门 Python 数据分析实用指南
    18位不重复订单号
    相对路径转绝对路径
  • 原文地址:https://www.cnblogs.com/jackge/p/3245316.html
Copyright © 2011-2022 走看看