Topic Links:
https://vjudge.net/problem/POJ-1195
Main topic:
Direct maintenance of two-dimensional tree arrays
Note that the horizontal ordinate all needs to add 1, because the tree array starts at (
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <string>6#include <cmath>7#include <Set>8#include <queue>9#include <map>Ten#include <stack> One#include <vector> A#include <list> -#include <deque> -#include <sstream> the#include <cctype> - #defineREP (i, n) for (int i = 0; I < (n); i++) - #defineFor (I, S, T) for (int i = (s); I < (t); i++) - using namespacestd; +typedefLong Longll; - intT, N, m, cases; + intnum[1048][1048]; A intLowbit (intx) at { - returnX & (-x); - } - ///Modify Num[x][y] = d - voidAddintXintYintd) - { in for(inti = x; I <= N; i + =lowbit (i)) - { to for(intj = y; J <= N; J + =Lowbit (j)) +NUM[I][J] + =D; - } the } * ///calculates the sum of the vertices as (1, 1) (x, y) $ intSumintXinty)Panax Notoginseng { - intAns =0; the for(inti = x; i >0; I-=lowbit (i)) + { A for(intj = y; J >0; J-=Lowbit (j)) theAns + =Num[i][j]; + } - returnans; $ } $ intMain () - { - intT, x1, y1, x2, y2, D; the while(Cin >> T >>N) - {Wuyimemset (NUM,0,sizeof(num)); the while(Cin >>t) - { Wu if(T = =3) Break; - Else if(T = =1) About { $scanf"%d%d%d", &x1, &y1, &d); -x1++, y1++; - Add (x1, y1, d); - } A Else if(T = =2) + { thescanf"%d%d%d%d", &x1, &y1, &X2, &y2); -x1++, y1++, x2++, y2++; $cout<< (sum (x2, y2)-sum (x2, y1-1)-SUM (x1-1, y2) + SUM (x1-1, Y1-1)) <<Endl; the } the } the } the return 0; -}
POJ-1195 Mobile Phones---bare two-dimensional tree array (note subscript starting from 1, 1)