• bzoj 2683: 简单题


     一维时间,二维X,树状数组维护,模板题。妈的什么时候借一个BZOJ权限号搞一搞。。。还不知道对不对。。。

      貌似KD-tree也可以。。。留坑。。。

    #include<bits/stdc++.h>
    #define LL long long
    #define lson rt<<1
    #define rson rt<<1|1
    using namespace std;
    const int maxx = 2e5+6;
    const int maxn = 2e6+6;
    struct node{
      int x,y,id,op;
      LL w;
      bool operator < (const node &s)const{
          if (x==s.x){
             if (y==s.y){
                 return op<s.op;
             }
             return y<s.y;
          }
          return x<s.x;
      }
    }a[maxx],b[maxx];
    int h[maxx];
    LL ans[maxx];
    LL sum[maxn];
    int w;
    int lowbit(int x){
       return x&(-x);
    }
    void add(int x,LL ww){
       for (int i=x;i<=w;i+=lowbit(i)){
          sum[i]+=ww;
       }
    }
    LL query(int x){
      LL ans=0;
      for (int i=x;i;i-=lowbit(i)){
         ans+=sum[i];
      }
      return ans;
    }
    void clear_bit(int x){
       for (int i=x;i<=w;i+=lowbit(i)){
          if (sum[i]==0)break;
          sum[i]=0;
       }
       return;
    }
    void cdq(int l,int r){
       if(l==r){
         return ;
       }
       int mid=(l+r)>>1;
       cdq(l,mid);
       cdq(mid+1,r);
       int i=l,j=mid+1,k=l;
       while(i<=mid && j<=r){
          if (a[i].x<=a[j].x){
             if (a[i].op==1){
                add(a[i].y,a[i].w);
             }
             b[k++]=a[i++];
          }else {
             if (a[j].op==2){
                ans[a[j].id]+=query(a[j].y);
             }else if (a[j].op==3){
                ans[a[j].id]-=query(a[j].y);
             }
             b[k++]=a[j++];
          }
       }
       while(i<=mid){
         if (a[i].op==1)add(a[i].y,a[i].w);
         b[k++]=a[i++];
       }
       while(j<=r){
         if (a[j].op==2)ans[a[j].id]+=query(a[j].y);
         else if (a[j].op==3)ans[a[j].id]-=query(a[j].y);
         b[k++]=a[j++];
       }
       for (i=l;i<=r;i++){
         clear_bit(a[i].y);
         a[i]=b[i];
       }
    }
    int main(){
      int t;
      int op;
      int tot=0;
      int now=0;
      LL s;
      int x,y;
      LL tmp;
      while(~scanf("%lld%d",&s,&w)){
         now=0;
          while(1){
            scanf("%d",&op);
            if (op==3)break;
            if (op==1){
               scanf("%d%d%lld",&x,&y,&tmp);
               a[++tot]={x,y,0,1,tmp};
            }else{
               int lx,ly,rx,ry;
               now++;
               scanf("%d%d%d%d",&lx,&ly,&rx,&ry);
               ans[now]=(LL)(rx-lx+1)*(ry-ly+1)*s;
               a[++tot]={lx-1,ly-1,now,2,0};
               a[++tot]={rx,ry,now,2, 0};
               a[++tot]={lx-1,ry,now,3,0};
               a[++tot]={rx,ly-1,now,3,0};
            }
          }
          cdq(1,tot);
          for (int i=1;i<=now;i++){
             printf("%lld
    ",ans[i]);
          }
    
    
      }
      return 0;
    }
    有不懂欢迎咨询 QQ:1326487164(添加时记得备注)
  • 相关阅读:
    将字符数组写到字符串
    两种比较不错的密码修改方案
    数组的应用 结构类型 使用深复制和浅复制 显示员工信息
    字符串处理技巧
    uva 1339
    11039
    uva 11636
    Uva401Palindromes
    poj2524-Ubiquitous Religions
    Poj1611The Suspects
  • 原文地址:https://www.cnblogs.com/bluefly-hrbust/p/11478911.html
Copyright © 2020-2023  润新知