"bzoj-1176&2683" mokia& simple problem CDQ divided treatment

Source: Internet
Author: User

1176: [Balkan2007]mokia time limit:30 Sec Memory limit:162 MB
submit:1854 solved:821
[Submit] [Status] [Discuss] Description

Maintains a w*w matrix with an initial value of S. Each operation can increase the weight of a lattice, or ask for the total weight of a sub-matrix. Modify the operand m<=160000, asking the number q<=10000,w<=2000000.

Input

The first row of two integers, s,w; S is the matrix initial value; W is the matrix size

Next, each behavior is one of three inputs (without quotation marks):

"1 x y a"

"2 x1 y1 x2 y2"

"3"

Input 1: You need to increase the lattice weights (x, y)

Input 2: You need to ask for the upper left corner (X1,Y1), the lower right corner is (x2,y2) the weight of all the lattice in the matrix, and output

Input 3: Indicates end of input

Output

For each input 2, the output line, that is, the answer to enter 2

Sample Input0 4
1 2 3 3
2 1 1) 3 3
1 2 2 2
2 2 2) 3 4
3Sample Output3
5HINT

Ensure that the answer does not exceed the int range

Source 2683: Simple Question Time limit:50 Sec Memory limit:128 MB
submit:821 solved:339
[Submit] [Status] [Discuss] 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
hint1<=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

Solution

Can not be directly hard to do, so the use of CDQ division, the general process is:

First, a query operation, split into 4. ++--;

Then all of our operations are sorted, sorted by X-dimension;

Then we set up a tree-like array of y-dimensions, and here we can find that if you press X from small to large, a query is equivalent to the Y-dimension prefix and;

Then CDQ (l,r), enumerate these operations, if you modify the operation sequence number <=mid, then modify, if the query operation ordinal >mid then calculate the answer. This sort and input order by x is not contradictory, you can get the correct answer

And then you have to restore it and then recursively divide it.

Code

BZOJ2683:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;inlineintRead () {intx=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx;}#defineMAXQ 200010#defineMAXN 500010intS,W,ANS[MAXQ];namespacebit{intTREE[MAXN]; InlineintLowbit (intx) {returnx&-x;} InlinevoidChange (intPosintD) { for(intI=pos; i<=w; I+=lowbit (i)) tree[i]+=D;} InlineintQuery (intPOS) {intRe=0; for(intI=pos; I I-=lowbit (i)) re+=tree[i];returnre;}}using namespaceBIT;structasknode{intId,x,y,opt,del,id; BOOL operator< (ConstAsknode & A)Const        {            return(x==a.x && y==a.y)? Opt<a.opt: ((x==a.x)? y<a.y:x<a.x); }}Q[MAXQ<<2],tmp[maxq<<2];voidCDQ (intLintR) {    if(L==R)return; intMid= (l+r) >>1, z1=l,z2=mid+1;  for(intI=l; i<=r; i++)        {             if(q[i].opt==1&& q[i].id<=mid) Bit::change (Q[i].y,q[i].del); if(q[i].opt==2&& q[i].id>mid) ans[q[i].id]+=bit::query (Q[I].Y) *Q[i].del; }     for(intI=l; i<=r; i++)if(q[i].opt==1&& q[i].id<=mid) Bit::change (q[i].y,-Q[i].del);  for(intI=l; i<=r; i++)if(Q[i].id<=mid) tmp[z1++]=q[i];Elsetmp[z2++]=Q[i];  for(intI=l; i<=r; i++) q[i]=Tmp[i]; CDQ (L,mid); CDQ (Mid+1, R);}intz,t;voidPrework (intX1,intY1,intX2,inty2) {Z++; T++; q[t].id=t; Q[t].id=z; Q[t].x=x1-1; Q[t].y=y1-1; Q[t].del=1; q[t].opt=2; T++; q[t].id=t; Q[t].id=z; q[t].x=x2; Q[t].y=y2; Q[t].del=1; q[t].opt=2; T++; q[t].id=t; Q[t].id=z; Q[t].x=x1-1; Q[t].y=y2; q[t].del=-1; q[t].opt=2; T++; q[t].id=t; Q[t].id=z; q[t].x=x2; Q[t].y=y1-1; q[t].del=-1; q[t].opt=2;}intMain () {W=read (); T=0; z=0;  while(1)        {            intOpt=read ();if(opt==3) Break; if(opt==1) {t++; Q[t].x=read (), Q[t].y=read (), Q[t].del=read (), q[t].id=t,q[t].opt=1;} if(opt==2) {intX1=read (), Y1=read (), X2=read (), y2=read ();        Prework (x1,y1,x2,y2);} } sort (Q+1, q+t+1);//for (int i=1; i<=t; i++) printf ("%d%d%d%d%d\n", q[i].id,q[i].opt,q[i].del,q[i].x,q[i].y);CDQ (1, T);  for(intI=1; i<=z; i++) printf ("%d\n", Ans[i]); return 0;} 

"bzoj-1176&2683" mokia& simple problem CDQ divided treatment

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.