Blog of RuSun

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

P3332 [ZJOI2013]K大数查询

P3332 [ZJOI2013]K大数查询

考虑树套树。注意到修改是权值单点修改,所以外层是权值线段树。感觉外层用树状数组也可以做。

查看代码
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>
#include <algorithm>
#define pb push_back
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 << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 5e4 + 10, M = 15e6 + 10;
int n, m, q, idx, rt[N << 2];
struct Op { int op, l, r; LL c; } o[N];
struct Node { int l, r, tag; LL c; } tr[M];
void insert (int &x, int l, int r, int L = 1, int R = n)
{
if (l > r || L > r || l > R) return;
if (!x) x = ++idx;
tr[x].c += min(r, R) - max(l, L) + 1;
if (L >= l && R <= r) return void(++tr[x].tag);
int mid = L + R >> 1;
insert(tr[x].l, l, r, L, mid), insert(tr[x].r, l, r, mid + 1, R);
}
LL ask (int x, int l, int r, int L = 1, int R = n)
{
if (!x) return 0;
if (l > r || L > r || l > R) return 0;
if (L >= l && R <= r) return tr[x].c;
int mid = L + R >> 1;
return tr[x].tag * (min(r, R) - max(l, L) + 1ll) + ask(tr[x].l, l, r, L, mid) + ask(tr[x].r, l, r, mid + 1, R);
}
void modify (int l, int r, int t, int L = 0, int R = m - 1, int x = 1)
{
insert(rt[x], l, r);
if (L == R) return;
int mid = L + R >> 1;
t <= mid ? modify(l, r, t, L, mid, x << 1) : modify(l, r, t, mid + 1, R, x << 1 | 1);
}
int query (int l, int r, LL k, int L = 0, int R = m - 1, int x = 1)
{
if (L == R) return L;
int mid = L + R >> 1;
LL t = ask(rt[x << 1 | 1], l, r);
return k <= t ? query(l, r, k, mid + 1, R, x << 1 | 1) : query(l, r, k - t, L, mid, x << 1);
}
int main ()
{
read(n, q);
vector <int> ws;
for (int i = 1; i <= q; ++i)
{
read(o[i].op, o[i].l, o[i].r, o[i].c);
if (o[i].op == 1) ws.pb(o[i].c);
}
sort(ws.begin(), ws.end());
m = ws.erase(unique(ws.begin(), ws.end()), ws.end()) - ws.begin();
for (int i = 1; i <= q; ++i)
if (o[i].op == 1) modify(o[i].l, o[i].r, lower_bound(ws.begin(), ws.end(), o[i].c) - ws.begin());
else write(ws[query(o[i].l, o[i].r, o[i].c)]), puts("");
return 0;
}