"Tree Array" BZOJ3132 seven minutes of God-made questions

Source: Internet
Author: User

3132: God-created seven minutes time limit:20 Sec Memory limit:128 MB
submit:1004 solved:445
[Submit] [Status] [Discuss] Description

"In the first minute, X says, there is a matrix, so there is a nxm matrix full of 0."

The second minute, L said, to be able to modify, so there is the upper-left corner of (A, b), the lower-right corner is (c,d) a rectangular area of the whole number plus a value of the operation.

The third minute, K said, to be able to query, so there is a given rectangular region of all the numbers and operations.

Four minutes, Rainbow Meow said, to be based on the binary tree data structure, so there is a data range.

Five minutes, and Snow said, to be patient, so there is a time limit.

Six minutes, to eat the piano male said, to save something, so there is a guarantee in the process and the final results are not more than 32-bit signed integer type of the range of the limit.

The first seven minutes, the problem was finally finished, however, the god of the cows do not want to write the problem of the program. ”

--Seven minutes of the God-made naked question

So this sacred mission is yours.

Input

The first behavior of the input data is x N m, which represents the matrix size NXM.

The following two actions occur from the second line of the input data to each row at the end of the file:

The L a B c D delta--represents all the numbers in the rectangular region (A, B), (C,D), plus the delta.

K a B c d--represents the and of all numbers in the rectangular area of the vertex (A, a, c,d).

Please note that k is lowercase.


Output

For each k operation, output the answer on a separate line.

Sample InputX 4 4
L 1 1 3 3 2
L 2 2 4 4 1
K 2 2 3 3

Sample Output12
HINT

For 100% of data, 1≤n≤2048, 1≤m≤2048, 1≤abs (Delta) ≤500, Operation no more than 200,000, to ensure that the operation and the final results are not more than 32-bit signed integer type representation range.

Exercises

Two-dimensional tree array interval modification interval query ...

The formula is really good to push, from one-dimensional extension of the two-dimensional line ...

The prefix of v[i][j],v[i][j]*i,v[i][j]*j,v[i][j]*i*j is maintained anyway, and

Code

//by dimensionality Reduction#include <cstdio>#include<iostream>#include<cstring>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>#include<map>#include<bitset>#include<algorithm>#definell Long Longusing namespacestd;intn,m;intLowbit (intx) {returnx& (-x);}structtree{intsum[2050][2050]; voidChangeintXintYintv) { for(inti=x;i<=n;i+=lowbit (i)) for(intj=y;j<=m;j+=Lowbit (j)) Sum[i][j]+=v; }    intAskintXinty) {intans=0;  for(inti=x;i;i-=lowbit (i)) for(intj=y;j;j-=Lowbit (j)) ans+=Sum[i][j]; returnans; }}T1,T2,T3,T4;intCalcintXinty) {    returnT1.ask (x, y) * (x+1) * (y+1) +t4.ask (x, y)-t2.ask (x, y) * (y+1)-t3.ask (x, y) * (x+1);}intMain () {Charop[5]; scanf ("%s%d%d",op,&n,&m); intA,b,c,d,val;  while(~SCANF ("%s", op)) {        if(op[0]=='L') {scanf ("%d%d%d%d%d",&a,&b,&c,&d,&val);    T1.change (A,b,val); T1.change (c+1, d+1, Val); T1.change (a,d+1,-val); T1.change (c+1, b,-val); T2.change (A,b,val*A); T2.change (c+1, d+1, val* (c+1)); T2.change (a,d+1,-val*a); T2.change (c+1, b,-val* (c+1)); T3.change (A,b,val*B); T3.change (c+1, d+1, val* (d+1)); T3.change (a,d+1,-val* (d+1)); T3.change (c+1, b,-val*b); T4.change (A,b,val*A*B); T4.change (c+1, d+1, val* (c+1) * (d+1)); T4.change (a,d+1,-val*a* (d+1)); T4.change (c+1, b,-val* (c+1)*b); }Else{scanf ("%d%d%d%d",&a,&b,&c,&d); printf ("%d\n", Calc (c,d)-calc (c,b-1)-calc (A-1, d) +calc (A-1, B-1)); }    }    return 0;}

"Tree Array" BZOJ3132 seven minutes of God-made 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.