Description
Maintain 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, ask the number q<=10000,w<=2000000. (In 2683, there is no S, the initial value is 0).
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
Analysis:
To divide the operation, the left side of the changes must affect the right side of the query, this part as long as the X sort, do a tree-like array is good.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 structA6 {7 intx, Y, pos, ctrl, ANS;8} t[200010], q[200010];9 TenInlineBOOLcmpConstA &a,ConstA &b) One { A returnA.pos <B.pos; - } - the intS, W, K, X, Y, a, B, CNT; - intkey[2000010], late[2000010], now; - - voidModifyintPosintdata) + { - for(; Pos <= W +1; POS + pos &-POS) + { A if(Late[pos]! = Now) Key[pos] =0; atLate[pos] = Now, Key[pos] + =data; - } - } - - intQueryintPOS) - { in intRET =0; - for(pos; pos-= pos &-POS) to if(Late[pos] = now) ret + =Key[pos]; + returnret; - } the * voidCdqintLeftintRight ) $ {Panax Notoginseng if(left = right)return; - intMid = left + right >>1; theCDQ (left, mid); CDQ (Mid +1, right); +now++; A for(inti = mid +1, j = left; I <= right; i++) the { + for(; J <= Mid && q[j].x <= q[i].x; j + +) - if(Q[j].ctrl = =0) Modify (Q[j].y, Q[j].ans); $ if(Q[i].ctrl >0) Q[i].ans + =query (Q[I].Y); $ } - for(inti = left, j = left, K = mid +1; I <= right; i++) - { the if((q[j].x <= q[k].x && J <= mid) | | k >Right ) -T[i] = q[j++];Wuyi ElseT[i] = q[k++]; the } -memcpy (q + left, T + left,sizeof(q[0]) * (Right-left +1)); Wu } - About intMain () $ { -scanf"%d%d", &s, &W); - while(k! =3) - { Ascanf"%d", &k); + if(k = =1) the { -scanf"%d%d%d", &x, &y, &a); $cnt++, q[cnt] = (A) {x +1, Y +1Cnt0, a}; the } the if(k = =2) the { thescanf"%d%d%d%d", &x, &y, &a, &b); -a++, b++; incnt++, q[cnt] = (A) {x, Y, CNT,1, X * y *s}; thecnt++, q[cnt] = (a) {A, B, CNT,2, A * b *s}; thecnt++, q[cnt] = (a) {A, Y, CNT,2, A * y *s}; Aboutcnt++, q[cnt] = (A) {x, B, CNT,2, X * b *s}; the } the } theCdq1, CNT); +Std::sort (q +1, q + cnt +1, CMP); - for(inti =1; I <= CNT; i++) the {Bayi if(Q[i].ctrl = =1) the { theprintf"%d\n", Q[i].ans + q[i +1].ans-q[i +2].ans-q[i +3].ans); - } - } the}
"Bzoj 1176 2683" Mokia