Codeforces Gym 103627I Streetlights

显然地,所有数对可以构成一棵树。

按时间分治,设当前在处理 \([s,e]\) 内的修改,我们注意到每个修改会导致树上一条祖先后代链的数对被删除。
假设每次是从一个点 \(v\) 往上删到一点 \(u\),我们不妨将所有 \(u\) 的父亲和所有 \(v\) 拿出来建出虚树,其余的边都可以扔掉。
途中需要用线段树来维护新增的数对。
直接实现就是 \(O((N+Q)\log^2(N+Q))\) 的。

然而事实上并不需要显式建树,注意到可以用单调栈来在按 DFS 序遍历的时候(也就是按左端点升序遍历)维护每个点的所有祖先。
可能会好写一点(大概)。

另,zkw 线段树在这题上很有用。


代码(从某份 std 那里抄来了 zkw):

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <bits/stdc++.h>

using namespace std;

const int inf = 0x3f3f3f3f;

const int N = 1e5;
const int Q = 2.5e5;

int n, q;
int a[N + 5], las[N + 5];
pair<int, int> upd[N + Q + 5];
int pre[N + Q + 5], nxt[N + Q + 5];

struct SegmentTree {
static const int S = 1 << 17;
int seg[(S << 1) + 5];
void insert(int u, int k) {
seg[u += S] = k;
for (u >>= 1; u; u >>= 1)
seg[u] = max(seg[u << 1], seg[u << 1 | 1]);
}
int queryL(int u, int k) {
for (u += S; seg[u] < k && (u & (u - 1)); u = (u - 1) >> 1);
if (seg[u] < k) return 0;
for (; u < S; u |= seg[u | 1] >= k) u <<= 1;
return seg[u] == k ? u - S : 0;
}
int queryR(int u, int k) {
for (u += S; seg[u] < k && (u & (u + 1)); u = (u + 1) >> 1);
if (seg[u] < k) return n + 1;
for (; u < S; u |= seg[u] < k) u <<= 1;
return seg[u] == k ? u - S : n + 1;
}
} seg;

struct node {
int l, r, h, w;
node(int l = 0, int r = 0, int h = 0, int w = 0): l(l), r(r), h(h), w(w) {}
bool operator<(const node &o) const { return l < o.l; }
bool operator==(const node &o) const { return l == o.l; }
};

pair<vector<node>, int> build(vector<int> newStatic, vector<pair<int, int>> upd, vector<node> tr) {
sort(newStatic.begin(), newStatic.end()), sort(upd.begin(), upd.end());
static int anc[N + 5];
static node newTr[N * 2 + 5];
int j = 0, top = 0, tot = 0;
for (auto [i, _]: upd) seg.insert(i, 0);
for (int i: newStatic) {
seg.insert(i, a[i]);
for (; j < tr.size() && tr[j].l <= i; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top) newTr[tot++] = tr[anc[top]];
anc[++top] = j;
}
for (; top && tr[anc[top]].r < i; --top) newTr[tot++] = tr[anc[top]];
for (; top && tr[anc[top]].h <= a[i]; --top);
}
for (; top; --top) newTr[tot++] = tr[anc[top]];
for (; j < tr.size(); ++j) newTr[tot++] = tr[j];
for (int i: newStatic) {
int l = seg.queryL(i - 1, a[i]);
if (l) newTr[tot++] = node(l, i, a[i], 1);
int r = seg.queryR(i + 1, a[i]);
if (r <= n) newTr[tot++] = node(i, r, a[i], 1);
}
tr = vector<node>(newTr, newTr + tot), sort(tr.begin(), tr.end()), tr.erase(unique(tr.begin(), tr.end()), tr.end());
vector<int> sum(tr.size());
int cnt = 0;
for (int i = 0; i < tr.size(); ++i) {
auto [l, r, h, w] = tr[i];
for (; top && tr[anc[top]].r <= r; --top);
if (top) sum[i] = sum[anc[top]];
sum[i] += w, cnt += w;
anc[++top] = i;
}
vector<int> crit(1, 0);
crit.reserve(upd.size() + 1);
j = top = 0;
for (auto [i, h]: upd) {
for (; j < tr.size() && tr[j].l <= i; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top);
anc[++top] = j;
}
for (; top && tr[anc[top]].r < i; --top);
crit.push_back(anc[top]);
int l = 1, r = top, mid, res = 1;
while (l <= r) {
mid = (l + r) >> 1;
if (tr[anc[mid]].h > h) l = mid + 1, res = mid;
else r = mid - 1;
}
crit.push_back(anc[res]);
}
sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
crit.reserve(crit.size() * 2);
j = top = 0;
for (int i = 0, siz = crit.size(); i + 1 < siz; ++i) {
for (; j <= crit[i]; ++j) {
auto [l, r, h, w] = tr[j];
for (; top && tr[anc[top]].r <= r; --top);
anc[++top] = j;
}
for (; top && tr[anc[top]].r <= tr[crit[i + 1]].r; --top);
crit.push_back(anc[top]);
}
sort(crit.begin(), crit.end()), crit.erase(unique(crit.begin(), crit.end()), crit.end());
top = 0;
for (int i = 0; i < crit.size(); ++i) {
auto [l, r, h, w] = tr[crit[i]];
for (; top && tr[anc[top]].r <= r; --top);
w = sum[crit[i]];
if (top) w -= sum[anc[top]];
cnt -= w;
anc[++top] = crit[i];
newTr[i] = node(l, r, h, w);
}
return {vector<node>(newTr, newTr + crit.size()), cnt};
}

int ans[N + Q + 5];

void solve(int l, int r, vector<node> tr) {
if (l == r) {
a[upd[l].first] = upd[l].second;
int tag = build({upd[l].first}, {}, tr).second;
ans[l] += tag, ans[l + 1] -= tag;
return ;
}
int mid = (l + r) >> 1;
vector<int> tmp;
vector<pair<int, int>> updTmp;
static bool vis[N + 5];
if (mid >= n) {
for (int i = mid + 1; i <= r; ++i) if (pre[i] < l) tmp.push_back(upd[i].first), vis[upd[i].first] = 1;
updTmp = vector<pair<int, int>>(upd + l, upd + mid + 1);
for (int i = l; i <= mid; ++i) if (pre[i] && pre[i] < l && !vis[upd[pre[i]].first]) updTmp.push_back(upd[pre[i]]);
for (int i: tmp) vis[i] = 0;
auto [L, lTag] = build(tmp, updTmp, tr);
ans[l] += lTag, ans[mid + 1] -= lTag;
vector<int>().swap(tmp), vector<pair<int, int>>().swap(updTmp);
solve(l, mid, L);
}
for (int i = l; i <= mid; ++i) if (nxt[i] > r) tmp.push_back(upd[i].first), vis[upd[i].first] = 1;
updTmp = vector<pair<int, int>>(upd + mid + 1, upd + r + 1);
for (int i = mid + 1; i <= r; ++i) if (pre[i] && pre[i] <= mid && !vis[upd[pre[i]].first]) updTmp.push_back(upd[pre[i]]);
for (int i: tmp) vis[i] = 0;
auto [R, rTag] = build(tmp, updTmp, tr);
ans[mid + 1] += rTag, ans[r + 1] -= rTag;
solve(mid + 1, r, R);
}

int main() {
scanf("%d%d", &n, &q);
a[0] = a[n + 1] = inf;
for (int i = 1; i <= n; ++i)
scanf("%d", a + i),
upd[i] = make_pair(i, a[i]), las[i] = i;
for (int i = n + 1; i <= n + q; ++i)
scanf("%d%d", &upd[i].first, &upd[i].second),
pre[i] = las[upd[i].first], las[upd[i].first] = i;
fill(las + 1, las + n + 1, n + q + 1);
for (int i = n + q; i; --i) nxt[i] = las[upd[i].first], las[upd[i].first] = i;
solve(1, n + q, {node(0, n + 1, inf, 0)});
for (int i = 1; i <= n + q; ++i) ans[i] += ans[i - 1];
for (int i = n; i <= n + q; ++i) printf("%d\n", ans[i]);
}