Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

整体二分模板

区间、值域问题的强大算法。

查看代码
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <cstdio>
#include <vector>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 1e5 + 10, inf = 1e9;
int n, m, w[N], ans[N], tr[N];
struct OP
{
int id, l, r, k;
};
void modify(int x, int k)
{
for (; x <= n; x += x & -x)
tr[x] += k;
}
int query(int x)
{
int res = 0;
for (; x; x -= x & -x)
res += tr[x];
return res;
}
void solve(int l, int r, vector<OP> &p)
{
if (p.empty())
return;
if (l == r)
{
for (OP i : p)
i.id > 0 && (ans[i.id] = l);
return;
}
int mid = l + r >> 1;
vector<OP> L, R;
for (OP i : p)
if (i.id > 0)
{
int t = query(i.r) - query(i.l - 1);
if (t >= i.k)
L.push_back(i);
else
{
i.k -= t;
R.push_back(i);
}
}
else
{
i.k <= mid ? L.push_back(i) : R.push_back(i);
i.k <= mid && (modify(i.l, i.id ? -1 : 1), 0);
}
for (int i = p.size() - 1; ~i; i--)
p[i].id < 1 && p[i].k <= mid && (modify(p[i].l, p[i].id ? 1 : -1), 0);
solve(l, mid, L), solve(mid + 1, r, R);
}
void chkmin(int &x, int k)
{
(x > k) && (x = k);
}
void chkmax(int &x, int k)
{
(x < k) && (x = k);
}
int main()
{
read(n), read(m);
vector<OP> p;
int l = inf, r = 0;
for (int i = 1; i <= n; i++)
{
read(w[i]);
chkmin(l, w[i]), chkmax(r, w[i]);
p.push_back((OP){0, i, i, w[i]});
}
int idx = 0;
for (char op; m; m--)
{
op = getchar();
while (op != 'Q' && op != 'C')
op = getchar();
if (op == 'Q')
{
int L, R, k;
read(L), read(R), read(k);
p.push_back((OP){++idx, L, R, k});
}
else if (op == 'C')
{
int t, k;
read(t), read(k);
chkmin(l, k), chkmax(r, k);
p.push_back((OP){-1, t, t, w[t]});
p.push_back((OP){0, t, t, w[t] = k});
}
}
solve(l, r, p);
for (int i = 1; i <= idx; i++)
write(ans[i]), puts("");
return 0;
}