-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practicesegmenttree4e-earthquakes.cpp
78 lines (65 loc) · 2.51 KB
/
Practicesegmenttree4e-earthquakes.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cstdio>
#include <vector>
typedef long long ll;
struct data{
ll mn, mx, cnt;
void reset(){mn = 100; mx = 0LL; cnt = 0;}
};
struct segtree{
ll sz; std::vector<data> f;
void init(ll num){
sz = 1; while(sz < num){sz *= 2;}
data init; init.reset();
f.assign(2 * sz, init);
}
void set(ll idx, ll val){set(idx, val, 0, 0, sz);}
void set(ll idx, ll val, ll node, ll lv, ll rv){
if(rv == lv + 1){f[node].mn = f[node].mx = val; f[node].cnt = 1; return;}
if(f[node].cnt == 0){data cur; cur.reset(); f[2*node + 1] = f[2*node+2] = cur;}
ll mid = (lv + rv) / 2;
if(idx < mid){set(idx, val, 2 * node + 1, lv, mid);}
else{set(idx, val, 2 * node + 2, mid, rv);}
f[node].mn = (f[2 * node + 1].mn < f[2 * node + 2].mn) ? f[2 * node + 1].mn : f[2 * node + 2].mn;
f[node].mx = (f[2 * node + 1].mx > f[2 * node + 2].mx) ? f[2 * node + 1].mx : f[2 * node + 2].mx;
f[node].cnt = f[2 * node + 1].cnt + f[2 * node + 2].cnt;
}
ll getRem(ll lq, ll rq, ll val){return getRem(lq, rq, val, 0, 0, sz);}
ll getRem(ll lq, ll rq, ll val, ll node, ll lv, ll rv){
if(rq <= lv || rv <= lq){return 0LL;}
else if(lq <= lv && rv <= rq && f[node].mn > val){return 0LL;}
else{
ll ret(0LL);
if(lq <= lv && rv <= rq && f[node].mx <= val){ret = f[node].cnt; f[node].reset();}
else{
ll mid = (lv + rv) / 2;
ll leftRem = getRem(lq, rq, val, 2 * node + 1, lv, mid);
ll rightRem = getRem(lq, rq, val, 2 * node + 2, mid, rv);
ret =(leftRem + rightRem);
}
if(f[node].cnt){
f[node].mn = (f[2 * node + 1].mn < f[2 * node + 2].mn) ? f[2 * node + 1].mn : f[2 * node + 2].mn;
f[node].mx = (f[2 * node + 1].mx > f[2 * node + 2].mx) ? f[2 * node + 1].mx : f[2 * node + 2].mx;
f[node].cnt = f[2 * node + 1].cnt + f[2 * node + 2].cnt;
}
return ret;
}
return 0LL;
}
};
int main(){
ll n, m; scanf("%lld %lld", &n, &m);
segtree st; st.init(n);
while(m--){
int op; scanf("%d", &op);
if(op == 1){
ll x, y; scanf("%lld %lld", &x, &y);
st.set(x, y);
}
else if(op == 2){
ll lb, rb, strength; scanf("%lld %lld %lld", &lb, &rb, &strength);
ll removed = st.getRem(lb, rb, strength);
printf("%lld\n", removed);
}
}
return 0;
}