ZOJ2112

这道题就是带修改的求区间第k大
非常经典的整体二分的题,因为满足查询的答案具有二分性,修改对答案的贡献相互独立,且可离线
具体过程就是通过二分总体的答案,把最后答案小于mid的询问和值小于mid操作扔进一组,把其它的扔进另一组,使总体复杂度为$nlog^{2}n$

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
//created by missever
#include<bits/stdc++.h>
#define MAX 1000000007
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
struct node {
int x,y,tp,id,k;
node(int _x = 0,int _y = 0,int r = 0,int z = 0,int d = 0): x(_x),y(_y),tp(r),id(z),k(d) {};
} f[maxn],q1[maxn],q2[maxn];
int tot,num,n;
int ans[maxn],a[maxn],g[maxn];
int lowbit(int x) {
return x & (-x);
}
void add(int x,int d) {
while(x <= n) {
g[x] += d;
x += lowbit(x);
}
}
int ff(int x) {
int s = 0;
while(x > 0) {
s += g[x];
x -= lowbit(x);
}
return s;
}
void solve(int ql,int qr,int l,int r) {
if(ql > qr) return;
if(l == r) {
for(int i = ql; i <= qr; i++) {
if(f[i].tp == 3) ans[f[i].id] = l;
}
return;
}
int mid = (l + r) >> 1;
int t1 = 0,t2 = 0;
for(int i = ql; i <= qr; i++) {
if(f[i].tp == 1) {
if(f[i].x <= mid) {
add(f[i].id,1);
q1[t1++] = f[i];
} else q2[t2++] = f[i];
} else if(f[i].tp == 2) {
if(f[i].x <= mid) {
add(f[i].id,-1);
q1[t1++] = f[i];
} else q2[t2++] = f[i];
} else {
int m = ff(f[i].y) - ff(f[i].x - 1);
if(m >= f[i].k) q1[t1++] = f[i];
else {
f[i].k -= m;
q2[t2++] = f[i];
}
}
}
for(int i = ql; i <= qr; i++) {
if(f[i].tp == 1) {
if(f[i].x <= mid) add(f[i].id,-1);
} else if(f[i].tp == 2) {
if(f[i].x <= mid) add(f[i].id,1);
}
}
memcpy(f + ql, q1, t1 * sizeof(node));
memcpy(f + ql + t1, q2, t2 * sizeof(node));
solve(ql,ql + t1 - 1,l,mid);
solve(ql + t1,qr,mid + 1,r);
}
int main() {
int t,m,i,x,l,r;
char e;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
tot = num = 0;
for(i = 1; i <= n; i++) {
scanf("%d",&a[i]);
f[++tot] = node(a[i],0,1,i,0);
}
for(i = 0; i < m; i++) {
scanf(" %c",&e);
//cout<<i<<endl;
if(e == 'Q') {
scanf("%d%d%d",&l,&r,&x);
f[++tot] = node(l,r,3,++num,x);
} else {
scanf("%d%d",&l,&x);
f[++tot] = node(a[l],0,2,l,0);
f[++tot] = node(x,0,1,l,0);
a[l] = x;
}
}
//cout<<"----"<<endl;
solve(1,tot,1,1000000000);
for(i = 1; i <= num; i++) printf("%d\n",ans[i]);
}
return 0;
}