[BZOJ]2683: 簡單題 CDQ分治+樹狀數組

來源:互聯網
上載者:User

Description
你有一個N*N的棋盤,每個格子內有一個整數,初始時的時候全部為0,現在需要維護兩種操作:
命令
參數限制
內容
1 x y A
1<=x,y<=N,A是正整數
將格子x,y裡的數字加上A
2 x1 y1 x2 y2
1<=x1<= x2<=N
1<=y1<= y2<=N
輸出x1 y1 x2 y2這個矩形內的數字和
3
無
終止程式 題解:

CDQ分治直接上就行了,注意最後兩句話的順序…… 代碼:

#include<bits/stdc++.h>using namespace std;#define LL long long#define pa pair<int,int>const int Maxn=500010;const int Maxm=200010;const int inf=2147483647;int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();    return x*f;}//1 修改 2 減 3 加 struct Opt{int px,py,x,type;}q[Maxm<<2],temp[Maxm<<2];bool operator < (Opt a,Opt b){    if(a.px!=b.px)return a.px<b.px;    if(a.py!=b.py)return a.py<b.py;    return a.type<b.type;}int n,m=0,cnt=0,cntq=0,num[Maxm];LL ans[Maxm],s[Maxn];void add(int x,LL y){for(;x<=n;x+=(x&-x))s[x]+=y;}LL getsum(int x){LL re=0;for(;x;x-=(x&-x))re+=s[x];return re;}void solve(int l,int r){    if(l==r)return;    int mid=l+r>>1;    solve(l,mid);solve(mid+1,r);    int i=l,j=mid+1,len=0,o=0;    while(i<=mid&&j<=r)    {        if(q[i].px<q[j].px||(q[i].px==q[j].px&&q[i].type<q[j].type))        {            if(q[i].type==1)add(q[i].py,(LL)(q[i].x)),num[++o]=i;            temp[++len]=q[i++];        }        else        {            if(q[j].type==2)ans[q[j].x]-=getsum(q[j].py);            else if(q[j].type==3)ans[q[j].x]+=getsum(q[j].py);            temp[++len]=q[j++];        }    }    while(i<=mid)temp[++len]=q[i++];    while(j<=r)    {        if(q[j].type==2)ans[q[j].x]-=getsum(q[j].py);        else if(q[j].type==3)ans[q[j].x]+=getsum(q[j].py);        temp[++len]=q[j++];    }    for(int p=1;p<=o;p++)add(q[num[p]].py,(LL)(-q[num[p]].x));    for(int p=1;p<=len;p++)q[l+p-1]=temp[p];}int main(){    n=read();    int op=read();    while(op!=3)    {        m++;        int x=read(),y=read(),x1,y1;        if(op==1)        {            q[++cnt].x=read();q[cnt].type=1;            q[cnt].px=x;q[cnt].py=y;        }        else        {            x1=read(),y1=read();            q[++cnt].px=x-1;q[cnt].py=y-1;q[cnt].type=3;            q[++cnt].px=x1;q[cnt].py=y1;q[cnt].type=3;            q[++cnt].px=x1;q[cnt].py=y-1;q[cnt].type=2;            q[++cnt].px=x-1;q[cnt].py=y1;q[cnt].type=2;            cntq++;            for(int i=cnt-3;i<=cnt;i++)q[i].x=cntq;        }        op=read();    }    solve(1,cnt);    for(int i=1;i<=cntq;i++)printf("%lld\n",ans[i]);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.