-
Notifications
You must be signed in to change notification settings - Fork 3
/
zkw Seg.cpp
65 lines (64 loc) · 1.24 KB
/
zkw 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
61
62
63
64
65
#ifndef __zkw__
#define __zkw__
#define MAX_N 100005
#define ll long long
int x;
struct zkw
{
int M,T;
int tr[MAX_N];
int min(int x,int y){return x < y ? x : y;}
int max(int x,int y){return x > y ? x : y;}
void build(int x)
{
for (T = 1;T <= x + 1;T <<= 1);
for (int i = T + 1;i <= T + x;++i) //--read;
for (int i = T - 1;i >= 1;--i){
tr[i] = min(tr[i << 1],tr[i << 1 | 1]);
tr[i << 1] -= tr[i];
tr[i << 1 | 1] -= tr[i];
}
}
int Querynum(int l)//-单点查询
{
int res = 0;
while(l){
res += tr[l];
x >>= 1;
}
return res;
}
int Querysum(int l,int r)//--区间求和
{
int ans = 0;
l += M - 1,r += M - 1;
ans += tr[l] + tr[r];
if (l == r) return ans - tr[l];
for (;l ^ r ^ 1;l >>= 1,r >>= 1)
{
if (~l&1) ans += tr[l ^ 1];
if (r&1) ans += tr[r ^ 1];
}
return ans;
}
int Querymax(int l,int r)//--区间最大值
{
int L,R;
int ans = 0;
for (l = l + M - 1,r = r + M + 1;l ^ r ^ 1;l >>= 1,r >>= 1){
L += tr[l],R += tr[r];
if (~l&1) L = max(L,tr[l ^ 1]);
if (r&1) R = max(R,tr[r ^ 1]);
}
ans = max(L,R);
while(l) ans += tr[l >>= 1];
}
void change(int x,int v)
{
tr[x = M + x - 1] += v;
while(x) tr[x >>= 1] = tr[x << 1] + tr[x << 1 | 1];
}
};
#undef MAX_N
#undef ll
#endif