Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 9263 | Accepted: 4154 |
Description
Input
Output
Sample Input
0 4 1 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3
Sample Output
3 4
#include <cstdlib> #include <cstdio> #include <cstring> #define LSON p << 1 #define RSON p << 1 | 1 #define MID( x, y ) (x) + (y) >> 1 #define TWO( x, y ) (x) + (y) << 1 using namespace std; void getint( int &t ) { char c; while( c = getchar(), c < '0' || c > '9' ); t = c - '0'; while( c = getchar(), c >= '0' && c <= '9' ) t = t * 10 + c - '0'; } struct node { int l, r, sum; }n[2600][2600]; struct Node { int l ,r; }N[2600]; void y_build( node *rt, int p, int l, int r ) { rt[p].l = l, rt[p].r = r, rt[p].sum = 0; if( r - l == 1 ) return; int m = MID( l, r ); y_build( rt, LSON, l, m ); y_build( rt, RSON, m, r ); } void build( int p, int l, int r, int l2, int r2 ) { N[p].l = l, N[p].r = r; y_build( n[p], 1, l2, r2 ); // 该步操作与上部极其相似了 if( r - l == 1 ) return; int m = MID( l, r ); build( LSON, l, m, l2, r2 ); build( RSON, m, r, l2, r2 ); } void y_modify( node *rt, int p, int add, int l, int r ) { rt[p].sum += add; if( rt[p].r - rt[p].l == 1 ) return; // 更新到最下面,没有用到Lzay操作 int m = MID( rt[p].l, rt[p].r ); if( m >= r ) y_modify( rt, LSON, add, l, r ); else if( m <= l ) y_modify( rt, RSON, add, l, r ); } void modify( int p, int add, int l, int r, int l2, int r2 ) { y_modify( n[p], 1, add, l2, r2 ); if( N[p].r - N[p].l == 1 ) return; // 一维是无法Lzay的,因为Y轴是变化的 int m = MID( N[p].l, N[p].r ); if( m >= r ) modify( LSON, add, l, r, l2, r2 ); else if( m <= l ) modify( RSON, add, l, r, l2, r2 ); } int y_cal( node *rt, int p, int l, int r ) { if( rt[p].l == l && rt[p].r == r ) { return rt[p].sum; } int m = MID( rt[p].l, rt[p].r ); if( m >= r ) return y_cal( rt, LSON, l, r ); else if( m <= l ) return y_cal( rt, RSON, l, r ); else { return y_cal( rt, LSON, l, m ) + y_cal( rt, RSON, m, r ); } } int cal( int p, int l, int r, int l2, int r2 ) { if( N[p].l == l && N[p].r == r ) { return y_cal( n[p], 1, l2, r2 ); } int m = MID( N[p].l, N[p].r ); if( m >= r ) return cal( LSON, l, r, l2, r2 ); else if( m <= l ) return cal( RSON, l, r, l2, r2 ); else { return cal( LSON, l, m, l2, r2 ) + cal( RSON, m, r, l2, r2 ); } } int main() { int op; while( getint( op ), op != 3 ) { int M, x1, y1, x2, y2, add; switch( op ) { case 0 : { scanf( "%d", &M ); build( 1, 1, M + 1, 1, M + 1 ); break; } case 1 : { scanf( "%d %d %d", &x1, &y1, &add ); modify( 1, add, x1 + 1, x1 + 2, y1 + 1, y1 + 2 ); break; } case 2: { scanf( "%d %d %d %d", &x1, &y1, &x2, &y2 ); printf( "%d\n", cal( 1, x1 + 1, x2 + 2, y1 + 1, y2 + 2 ) ); break; } case 3: break; } } return 0; }