Bzoj2683 Simple Questions

Source: Internet
Author: User
Tags x2 y2

Time limit:50 Sec Memory limit:128 MB
submit:1071 solved:428 Description you have a n*n board with an integer in each grid, all at 0 at the beginning, and now you need to maintain two operations:

Command

Parameter limits

Content

1 x y A

1<=x,y<=n,a is a positive integer

Add the numbers in the lattice x, Y, plus a

2 x1 y1 x2 y2

1<=x1<= x2<=n

1<=y1<= y2<=n

Output x1 y1 x2 y2 the number within the rectangle and

3

No

Terminating programs

Input file The first line of a positive integer n. Next, one action per line. Output for each 2 operation, outputs a corresponding answer. Sample Input4
1 2 3 3
2 1 1) 3 3
1 2 2 2
2 2 2) 3 4
3

Sample OUTPUT3
5

HINT

1<=n<=500000, no more than 200,000 operations, memory limit of 20M.
For 100% of the data, the A in operation 1 is not more than 2000.

Source

Well, it's a simple question, it's only been tuned for two days.

K-dtree is periodically refactored to enforce data maintenance. If the constants are not written well, they will fly T.

Before the 47 rows of the left and right boundary of the wrong, the complexity of the time to directly break through the sky.

1 /*by Silvern*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8 #defineLL Long Long9 using namespacestd;Ten Const intmxn=250010; One LL Read () { ALL x=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} the     returnx*F; - } - structnode{ -     intL,r; +     intmin[2],max[2]; -     intd[2]; +     intW; A LL sum; at }T[MXN]; - introot=0, Nowd; - intcmpConstNode A,ConstNode B) { -     returna.d[nowd]<B.d[nowd]; - } - intn,cnt; in intlim=0; -InlinevoidPushup (intRtintx) { tot[rt].max[0]=max (t[rt].max[0],t[x].max[0]); +t[rt].max[1]=max (t[rt].max[1],t[x].max[1]); -t[rt].min[0]=min (t[rt].min[0],t[x].min[0]); thet[rt].min[1]=min (t[rt].min[1],t[x].min[1]); *     return; $ }Panax NotoginsengInlineBOOL inch(intX1,intY1,intX2,intY2,intk) { -     return(x1<=t[k].min[0] && t[k].max[0]&LT;=X2 && they1<=t[k].min[1] && t[k].max[1]<=y2); + } AInlineBOOL  out(intX1,intY1,intX2,intY2,intk) { the     return(x1>t[k].max[0] || x2<t[k].min[0] || y1>t[k].max[1] || y2<t[k].min[1]); + } - intBuild (intLintRintD) { $     if(L>r)return 0; $Nowd=d;intMid= (l+r) >>1; -Nth_element (t+l,t+mid,t+r+1, CMP);/// -t[mid].max[0]=t[mid].min[0]=t[mid].d[0]; thet[mid].max[1]=t[mid].min[1]=t[mid].d[1]; -t[mid].sum=T[MID].W;WuyiT[mid].l=build (l,mid-1, d^1); the     if(T[MID].L) pushup (MID,T[MID].L); -T[mid].r=build (mid+1, r,d^1); Wu     if(T[MID].R) pushup (MID,T[MID].R); -t[mid].sum=t[mid].w+t[t[mid].l].sum+t[t[mid].r].sum; About     returnmid; $ } - voidInsertint&now,intXintD) { -     if(!now) {now=x;return;} -     if(T[x].d[d]==t[now].d[d] && t[x].d[! d]==t[now].d[!D]) { At[now].w+=T[X].W; +t[now].sum+=T[X].W; the--CNT; -         return; $     } the     if(t[x].d[d]<T[now].d[d]) { theInsert (t[now].l,x,d^1); the pushup (NOW,T[NOW].L); the     } -     Else{ inInsert (t[now].r,x,d^1); the pushup (NOW,T[NOW].R); the     } Aboutt[now].sum=t[now].w+t[t[now].l].sum+t[t[now].r].sum; the     return; the } theLL Query (intRtintX1,intY1,intX2,inty2) { +     if(!RT)return 0; -LL res=0; the     if(inch(X1,Y1,X2,Y2,RT)) {returnt[rt].sum;}Bayi     if( out(X1,Y1,X2,Y2,RT)) {return 0;} the     if(x1<=t[rt].d[0] && t[rt].d[0]&LT;=X2 && they1<=t[rt].d[1] && t[rt].d[1]&LT;=Y2) res+=T[RT].W; -Res+=query (t[rt].l,x1,y1,x2,y2) +query (t[rt].r,x1,y1,x2,y2); -     returnRes; the } the intMain () { the     inti,j,op,x,y,w; theN=read (); lim=10000; -     intX1,x2,y1,y2; the      while(1){ theop=read (); the         if(op==3) Break;94         if(op==1){ thet[++cnt].d[0]=read (); t[cnt].d[1]=read (); theT[cnt].w=read (); t[cnt].sum=T[CNT].W; thet[cnt].max[0]=t[cnt].min[0]=t[cnt].d[0];98t[cnt].max[1]=t[cnt].min[1]=t[cnt].d[1]; AboutInsert (ROOT,CNT,0); -             if(cnt==Lim) {101lim+=10000;102Root=build (1Cnt0);103             }104         } the         Else{106  107X1=read (); Y1=read (); X2=read (); Y2=read ();108printf"%lld\n", Query (Root,x1,y1,x2,y2));109         } the     }111     return 0; the}

Bzoj2683 Simple Questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.