• BZOJ 2683 简单题 ——CDQ分治


    简单题

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define F(i,j,k) for (int i=j;i<=k;++i)
    #define D(i,j,k) for (int i=j;i>=k;--i)
    #define maxn 2000005
     
    int sum[maxn];
    void add(int x,int f)
    {
    //  printf("Add %d on %d
    ",f,x);
        for (;x<maxn;x+=x&(-x)) sum[x]+=f;
    }
     
    int gs(int x)
    {
    //  printf("gs %d ",x);
        int ret=0;
        for (;x;x-=x&(-x)) ret+=sum[x];
    //  printf("= %d
    ",ret);
        return ret;
    }
     
    struct query{
        int opt;
        int x,y,a;
        int id;
        void print()
        {
            printf("%d %d %d %d %d
    ",opt,x,y,a,id);
        }
    }a[maxn];
     
    int opt,top=0,x,y,xx,yy,tot,ans[maxn];
    bool cmp(query A,query B)
    {return A.x<B.x;}
     
    void CDQ(int l,int r)
    {
        if (l==r) return;
        int mid=l+r>>1;
        CDQ(l,mid); CDQ(mid+1,r);
    //  printf("CDQ %d %d
    ",l,r);
    //  F(i,l,r) a[i].print();
        int pl=l;
        F(i,mid+1,r)
        if (a[i].opt==2){
    //      printf("now is %d %d
    ",a[i].x,a[i].y);
    //      a[pl].print();
            while (a[i].x>=a[pl].x&&pl<=mid)
            {
                if (a[pl].opt==1)
                {
                    add(a[pl].y,a[pl].a);
    //              printf("Add %d on %d
    ",a[pl].a,a[pl].y);
                }
                pl++;
            }
            ans[a[i].id]+=a[i].a*gs(a[i].y);
    //      printf("%d += %d
    ",a[i].id,a[i].a*gs(a[i].y));
        }
        F(i,l,pl-1) if (a[i].opt==1) add(a[i].y,-a[i].a);
        sort(a+l,a+r+1,cmp);
    //  printf("Over
    ");
    }
     
    int main()
    {
        scanf("%*d");
        while (scanf("%d",&opt)!=EOF&&opt!=3)
        {
            switch (opt)
            {
                case 1:
                    ++top;scanf("%d%d%d",&a[top].x,&a[top].y,&a[top].a);a[top].opt=1;break;
                case 2:
                    ++tot;
                    scanf("%d%d%d%d",&x,&y,&xx,&yy);
                    ++top;a[top].id=tot;a[top].x=xx; a[top].y=yy;  a[top].a=1; a[top].opt=2;
                    ++top;a[top].id=tot;a[top].x=x-1;a[top].y=y-1; a[top].a=1; a[top].opt=2;
                    ++top;a[top].id=tot;a[top].x=xx; a[top].y=y-1; a[top].a=-1;a[top].opt=2;
                    ++top;a[top].id=tot;a[top].x=x-1; a[top].y=yy; a[top].a=-1;a[top].opt=2;
                    break;
            }
        }
        CDQ(1,top);
        F(i,1,tot) printf("%d
    ",ans[i]);
    }
    

      

  • 相关阅读:
    jQuery 在 IE 上 clone checkbox 的問題。
    C/C++ typedef用法
    C++继承
    map常用操作
    C++ JsonCpp 使用(含源码下载)
    string常用操作
    C++虚函数
    STL容器迭代过程中删除元素技巧(转)
    关于IE下用HTTPS无法下载/打开文件(转)
    C++STL概览
  • 原文地址:https://www.cnblogs.com/SfailSth/p/6607078.html
Copyright © 2020-2023  润新知