Poj 1195 two-dimensional tree array and two-dimensional tree array template

Source: Internet
Author: User

Http://poj.org/problem? Id = 1195

When the matrix and are obtained, the subscript is wrong once...

Calculates the sum of the rectangle (x1, Y1) (X2, Y2 ).
| Sum = sum (X2, Y2)-sum (x1-1, Y2)-sum (X2, y1-1) + sum (x1-1, y1-1)

Two-dimensional tree array explanation: http://blog.csdn.net/u011026968/article/details/38532117

Two-dimensional tree array template:

/* ===================================================== ============ * | Two-dimensional tree array | 1. Initialization: init: C [] [] is set to 0; row, Col must assign initial values | the array subscript must start from 1 !!!!!!!!! | 2. sum (I, j) of the j column before the I row, update (I, j, num) | convert (I, j) add num | 3, calculate the sum of the rectangle (x1, Y1) (X2, Y2) | sum = sum (X2, Y2)-sum (x1-1, Y2)-sum (X2, y1-1) + sum (x1-1, y1-1) | when sum and C are required, use long \ * ========================== ======================================= */const int n = 10000; int C [N] [N]; int row, Col; inline int lowbit (const Int & X) {// x> 0 return X & (-x );} int sum (int I, Int J) {int tempj, sum = 0; while (I> 0) {tempj = J; while (tempj> 0) {sum + = C [I] [tempj]; tempj-= lowbit (tempj);} I-= lowbit (I);} return sum;} void Update (int I, int J, int num) {int tempj; while (I <= row) {tempj = J; while (tempj <= col) {c [I] [tempj] + = num; tempj + = lowbit (tempj);} I + = lowbit (I );}}

Poj 1195 code

#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const int MAXN = 1024 +100;ll c[MAXN][MAXN];int row,col;inline int lowbit(int i){return i&(-i);}ll sum(int i, int j){    int tmpj;    ll ret=0;    while(i>0)    {        tmpj=j;        while(tmpj>0)        {            ret+=c[i][tmpj];            tmpj-=lowbit(tmpj);        }        i-=lowbit(i);    }    return ret;}void update(int i, int j, int v){    int tmpj;    while(i<=row)    {        tmpj=j;        while(tmpj<=col)        {            c[i][tmpj]+=v;            tmpj+=lowbit(tmpj);        }        i+=lowbit(i);    }}int main(){    //IN("poj1195.txt");    int op,n,x,y,v;    int l,b,r,t;    while(~scanf("%d%d",&op,&n))    {        col=row=n;        CL(c,0);        while(~scanf("%d",&op))        {            if(op == 3)break;            if(op == 1)            {                scanf("%d%d%d",&x,&y,&v);                update(x+1,y+1,v);            }            if(2 == op)            {                scanf("%d%d%d%d",&l,&b,&r,&t);                printf("%lld\n",sum(r+1,t+1)+sum(l,b)-sum(l,t+1)-sum(r+1,b));            }        }    }    return 0;}


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.