Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstdio>
- #include <queue>
- #include <stack>
- #include <utility>
- using namespace std;
- const int MAXN = 40485880;
- int segTree[MAXN] = {0};
- class SegmentTree{
- public:
- void update(int node, int a1, int b1, int a2, int b2, int x, int y, int A){
- if(x < a1 || x > a2 || y < b1 || y > b2){
- return;
- }
- if(x == a1 && y == b1 && x == a2 && y == b2){
- segTree[node] += A;
- return;
- }
- update(4 * node, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x, y, A);
- update(4 * node + 1, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x, y, A);
- update(4 * node + 2, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x, y, A);
- update(4 * node + 3, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x, y, A);
- segTree[node] = segTree[4 * node] +
- segTree[4 * node + 1] +
- segTree[4 * node + 2] +
- segTree[4 * node + 3];
- }
- int query(int node, int a1, int b1, int a2, int b2,
- int x, int y, int xx, int yy){
- if(a2 < x || b2 < y || a1 > xx || b1 > yy){
- return 0;
- }
- if(x <= a1 && y <= b1 && a2 <= xx && b2 <= yy){
- return segTree[node];
- }
- int q1, q2, q3, q4;
- q1 = query(4 * node, a1, b1, (a1 + a2) / 2, (b1 + b2) / 2, x, y, xx, yy);
- q2 = query(4 * node + 1, a1, (b1 + b2) / 2 + 1, (a1 + a2) / 2, b2, x, y, xx, yy);
- q3 = query(4 * node + 2, (a1 + a2) / 2 + 1, b1, a2, (b1 + b2) / 2, x, y, xx, yy);
- q4 = query(4 * node + 3, (a1 + a2) / 2 + 1, (b1 + b2) / 2 + 1, a2, b2, x, y, xx, yy);
- return q1 + q2 + q3 + q4;
- }
- };
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int op, s, x, y, A, l, b, r, t;
- //scanf("%d%d", &op, &s);
- cin >> op >> s;
- SegmentTree ST;
- while(1){
- cin >> op;
- if(op == 3) break;
- if(op == 1){
- cin >> x >> y >> A;
- ST.update(1, 0, 0, s - 1, s - 1, x, y, A);
- }else{
- cin >> l >> b >> r >> t;
- int w = ST.query(1, 0, 0, s - 1, s - 1, l, b, r, t);
- cout << w << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment