-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_seg.cpp
60 lines (50 loc) · 1.21 KB
/
lazy_seg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, a) for(int i = 0; i < a; i++)
struct Node {
int l, r;
Node *lc, *rc;
int sum;
Node(int qL, int qR){
l = qL; r = qR;
if(qL == qR - 1){
sum = 0;
lc = rc = nullptr;
} else {
int qM = (qL + qR) / 2;
lc = new Node(qL, qM);
rc = new Node(qM, qR);
sum = 0;
}
}
void add(int x, int v){
if(l == r - 1) sum += v;
else {
int m = (l + r) / 2;
if(x < m) lc->add(x, v);
else rc->add(x,v);
sum = lc->sum + rc->sum;
}
}
int qry(int qL, int qR){
if(r <= qL || l >= qR) return 0;
if(qL <= l && r >= qR) return sum;
int m = (l + r) / 2;
return lc->qry(qL, qR) + rc->qry(qL, qR);
}
~Node(){
delete lc;
delete rc;
}
};
int main (){
std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(10);
Node* seg = new Node(1, 5);
seg->add(2,2);
seg->add(3,-1);
cout << seg->qry(3,5) << "\n";
delete seg;
return 0;
}