题解:P9891 [ICPC 2018 Qingdao R] Repair the Artwork

看起来非常不可做,实则不然。


考虑没有 $a=2$。

哦对不起走错片场了。只需要计算不包含 $1$ 的区间数量。

现在考虑暴力。

考虑容斥,枚举哪些 $2$ 没有被覆盖到即可。

现在考虑正解。

考虑对于每个不包含 $1$ 的区间数量 $s$,没覆盖到的 $2$ 的个数是奇数/偶数,有多少种方案。

按 $1$ 分段,对于每段 dp。$f_{i,s,0/1}$ 表示考虑这一段的前 $i$ 个 $2$,其中最后一个一定没被覆盖到,目前的总区间个数为 $s$,没被覆盖的 $2$ 个数是奇数/偶数的方案数。

状态数 $O(n^3)$,转移只需要枚举前一个 $2$ 的位置,合并精细实现,$O(n^4)$。

rec&code

rec。

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
// 看我完全不会做,打个 O(64^n log m)
// 等等这个好像没分
// 招笑。
/*
二项式定理处理掉自环。

然后 2^n 个点,最长链 n 的 DAG。

直接 dp 即可。O(n2^n)。
*/
/*
好的,假了。

我能在 23min 内打完 sub1 吗??(x
*/
/*
sub1 提示性怎么这么强啊

容斥一下直接解决掉 sub2
*/
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

long long qpow(long long x, long long y) { long long ans = 1; do { if(y & 1) ans = ans * x % 1000000007; x = x * x % 1000000007; } while(y >>= 1); return ans; }

int a[105];
long long dp[105][10005][2];
long long dx[10005][2];
long long px[10005][2];
long long hypo[10005][2];

int main()
{
// freopen("artwork.in", "r", stdin);
// freopen("artwork.out", "w", stdout);
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
vector<vector<int>> segs;
segs.push_back({});
for(int i=1;i<=n;i++)
{
scanf("%d", a + i);
if(a[i] == 1) segs.push_back({});
else segs.back().push_back(a[i]);
}
memset(dx, 0, sizeof dx);
dx[0][0] = 1;
unsigned s1 = 0;
for(const auto &s : segs)
{
if(s.empty()) continue;
vector<int> v2pos;
for(int i=0;i<s.size();i++) if(s[i] == 2) v2pos.push_back(i);
unsigned s2 = s.size() * (s.size() + 1) / 2;
for(int k=0;k<=s1+s2;k++) px[k][0] = px[k][1] = hypo[k][0] = hypo[k][1] = 0;
for(int i=0;i<v2pos.size();i++)
{
for(int k=0;k<=s2;k++) dp[i][k][0] = dp[i][k][1] = 0;
for(int j=0;j<i;j++)
{
for(int k=0;k+(v2pos[i]-v2pos[j]-1)*(v2pos[i]-v2pos[j])/2<=s2;k++)
{
// if(!dp[j][k][0]) continue;
dp[i][k+(v2pos[i]-v2pos[j]-1)*(v2pos[i]-v2pos[j])/2][0] = (dp[i][k+(v2pos[i]-v2pos[j]-1)*(v2pos[i]-v2pos[j])/2][0] + dp[j][k][1]) % 1000000007;
dp[i][k+(v2pos[i]-v2pos[j]-1)*(v2pos[i]-v2pos[j])/2][1] = (dp[i][k+(v2pos[i]-v2pos[j]-1)*(v2pos[i]-v2pos[j])/2][1] + dp[j][k][0]) % 1000000007;
}
}
dp[i][v2pos[i]*(v2pos[i]+1)/2][1]++;
for(int k=0;k<=s2 - (s.size()-v2pos[i])*(s.size()-v2pos[i]-1)/2;k++)
{
// printf("dp[%d][%d] = {%lld, %lld}\n", i, k, dp[i][k][0], dp[i][k][1]);
px[k+(s.size()-v2pos[i])*(s.size()-v2pos[i]-1)/2][0] = (px[k+(s.size()-v2pos[i])*(s.size()-v2pos[i]-1)/2][0] + dp[i][k][0]) % 1000000007;
px[k+(s.size()-v2pos[i])*(s.size()-v2pos[i]-1)/2][1] = (px[k+(s.size()-v2pos[i])*(s.size()-v2pos[i]-1)/2][1] + dp[i][k][1]) % 1000000007;
}
}
px[s.size()*(s.size()+1)/2][0]++;
for(int x=0;x<=s1;x++)
{
// printf("! px[%d] = {%lld, %lld}\n", x, px[x][0], px[x][1]);
for(int y=0;y<=s2;y++)
{
hypo[x+y][0] = (hypo[x+y][0] + dx[x][0] * px[y][0] + dx[x][1] * px[y][1]) % 1000000007;
hypo[x+y][1] = (hypo[x+y][1] + dx[x][1] * px[y][0] + dx[x][0] * px[y][1]) % 1000000007;
}
}
s1 += s2;
for(int k=0;k<=s1;k++)
{
dx[k][0] = hypo[k][0];
dx[k][1] = hypo[k][1];
}
}
long long ans = 0;
for(int i=0;i<=s1;i++)
{
// printf("dx[%d] = {%lld, %lld}\n", i, dx[i][0], dx[i][1]);
ans = (ans + dx[i][0] * qpow(i, m) % 1000000007 + 1000000006 * dx[i][1] % 1000000007 * qpow(i, m)) % 1000000007;
}
printf("%lld\n", ans);
}
return 0;
}
/*
How to use Sublime Text:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
*/

题解:P15508 [CEOI 2012] 网络 / Network

快读题。

这又是什么题目分类啊。


首先第一问可以直接 Tarjan……这样还是太邪恶了,考虑一下图的性质。

引理 $1$:图中每个环都没有弦。证明显然。

性质 $1$:图中每条边至多属于一个有向环,换句话说图是有向仙人掌。由引理 $1$ 显然。

性质 $2$:图按照强连通分量缩点后是树。这是显然的。

现在第一问直接统计子树权值和(一个节点权值就是强连通分量大小)即可。

考虑第二问。

观察样例,猜测只能从后代向祖先连边。

证明显然,如果连接横叉边,则 LCA 到终点会有两条简单路径。

考虑目标图满足什么性质。显然整个图强连通,换句话说每条边属于且仅属于一个简单环。

考虑对于所有非根节点,它和父节点连边一定是合法的。然而观察样例发现这不够优秀,实际上可以继续往上连。

那么考虑在树上 dfs,对于每个节点先遍历所有子节点,如果它到父节点的边还没有被覆盖(还不在环上),则一直往上只到遇到不能继续往上的情况(即 $x$ 到父节点 $\text{fa}_x$ 的边在原图中的终点 $v$ 和 $\text{fa}x$ 到 $\text{fa}{\text{fa}_x}$ 的边在原图上的起点 $u$ 不同,或者 $x$ 到 $\text{fa}_x$ 已被覆盖)。

正确性是显然的。

时间复杂度 $O(n)$。

rec&code

rec。

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
// 2h 才过掉 T1,这也太拉了
// 鉴定为:有向仙人掌。
/*
XS-X Party of Spirits
*/
#include <stack>
#include <cstdio>
#include <vector>
#include <bitset>
#include <cassert>
#include <algorithm>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

vector<int> web[100005];
int dfn[100005], low[100005];
int sccnt;
vector<int> scc[100005];
int sccid[100005];
bitset<100005> instk;
class iedge { public: int u, v, xu, xv; };
vector<iedge> ch[100005];
bitset<100005> NightWarden; // Warden: ??? / NW-X ADOFAI vs. Minecraft
iedge fa[100005];
int sz[100005];

char ibf[100000005];
unsigned li = 0;
unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }

int main()
{
fread(ibf, 1, sizeof ibf, stdin);
int n, m, r;
// scanf("%d%d%d", &n, &m, &r);
n = qread(); m = qread(); r = qread();
for(int i=1;i<=m;i++)
{
int u, v;
// scanf("%d%d", &u, &v);
u = qread(); v = qread();
web[u].push_back(v);
}
int ddfn = 0;
stack<int> stk;
U([&](auto &&self, int u) -> void
{
dfn[u] = low[u] = ++ddfn;
instk[u] = true;
stk.push(u);
for(int v : web[u])
{
if(!dfn[v])
{
self(self, v);
low[u] = min(low[u], low[v]);
}
else if(instk[v]) low[u] = min(low[u], dfn[v]);
}
if(dfn[u] == low[u])
{
sccnt++;
int x;
do
{
x = stk.top();
stk.pop();
instk[x] = false;
scc[sccnt].push_back(x);
sccid[x] = sccnt;
} while(x != u);
}
})(r);
assert(stk.empty());
for(int i=1;i<=n;i++)
{
for(int j : web[i])
{
if(sccid[i] != sccid[j]) ch[sccid[i]].push_back({sccid[i], sccid[j], i, j});
}
}
int met = 0;
U([&](auto &&self, int u) -> void { met++; sz[u] = scc[u].size(); for(const auto &e : ch[u]) { fa[e.v] = {e.v, e.u, e.xv, e.xu}; self(self, e.v); sz[u] += sz[e.v]; } })(sccid[r]);
assert(met == sccnt);
for(int i=1;i<=n;i++) printf("%d%c", sz[sccid[i]], " \n"[i == n]); // 40pts
vector<pair<int, int>> ans;
U([&](auto &&self, int u) -> void
{
for(const auto &e : ch[u])
{
self(self, e.v);
}
// if(u == 7916) __debugbreak();
if(u != sccid[r] && !NightWarden[u])
{
int lst = fa[u].xu, x = fa[u].xv;
while(sccid[x] != sccid[r] && fa[sccid[x]].xu == x && !NightWarden[sccid[x]])
{
NightWarden[sccid[x]] = true;
lst = x;
x = fa[sccid[x]].xv;
}
ans.push_back({fa[u].xu, x});
}
})(sccid[r]);
// printf("0\n"); return 0;
printf("%d\n", (int)ans.size());
for(const auto &[u, v] : ans) printf("%d %d\n", u, v);
return 0;
}

题解:P13342 [EGOI 2025] Wind Turbines / 风力涡轮机

$O(n\sqrt q\log n)$ 卡过了,纪念一下。

首先如果 $r=l$,就是 MST。

如果 $r=l+1$,就是去掉 $l,r$ 路径上最大权边。

对于其它情况就是 $r=l+1$ 的扩展,$r=l+1$ 本质上就是 Kruskal 重构树上的 LCA,而一般情况就是将 $[l,r]$ 按照 dfn 排序(Kruskal 重构树上,下同),然后相邻求 LCA,所有不同的 LCA 的点权之和。换句话说就是虚树。

那么配上莫队可以做到 $O(n\sqrt n\log n)$。$O(n\log n)-O(1)$ LCA 是简单的(dfn ST 表),瓶颈在于求前驱后继。

接下来就是卡常(没想到吧不是回滚莫队而是卡常)。

  • 快读。
  • 用 set 肯定是爆爆爆的。考虑一个 bitset 优化的 01-Trie,堆式存储,每个节点存储子树中有没有存在的数字。bitset 只是为了稍微 cache 友好一点。然后使用位运算顺手加速一下插入删除前驱后继。然后这个 01-Trie 其实比较快了,和 LCA 差别不大,所以 LCA 的优化也比较重要。
  • 莫队奇偶优化。
  • 考虑到我们可以记忆化,对于每个存在的数字存储其前驱后继,和它和它的后继的 LCA,这样可以节省一半的 Trie 操作和 LCA 计算。
  • 考虑到每次求 LCA 都是对于两个不同的叶子,可以对叶子不记录 dfn 序,这样相当于只有 $n-1$ 个点,ST 表可以加速一点点,bitset 也是,同时所有叶子节点按照父节点 dfn 排序;然后 LCA 和权值也相应优化一下,尽量减少数组访问次数,比较复杂,详见代码。
  • ST 表的经典卡常小技巧:小的一维放前面,cache。
  • 交两遍,完全相同的代码一个 $>4.2\mathrm{s}$ 一个 $3.5\mathrm{s}$ 成功气笑我。洛谷的所有评测机真的是一个配置吗。
  • 善用 perf 啊,不能盲目优化,对于这个 $300+$ 行的代码瞪眼优化法真的很无从下手。

当然这个东西用 van Emde Boas 可以优化到 $O(n\sqrt q\log \log n)$,但我觉得直接写并不能过,毕竟 vEB 常数不小。

rec&code

rec。

下面的代码经过适当简化,主要删除了注释掉的调试语句。

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <cmath>
#include <cstdio>
#include <vector>
#include <bitset>
#include <chrono>
#include <utility>
#include <cassert>
#include <algorithm>
#ifdef LB_IS_DEBUGGING
#include <signal.h>
#define __debugbreak() raise(SIGTRAP)
#else
#define __debugbreak()
#endif

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

class dsu
{
public:
unsigned fa[100005];
unsigned rk[100005];
unsigned getfa(unsigned x) { while(x != fa[x]) x = fa[x] = fa[fa[x]]; return x; }
void merge(unsigned x, unsigned y) { if((x = getfa(x)) == (y = getfa(y))) return; if(rk[x] < rk[y]) fa[x] = y; else if(rk[x] > rk[y]) fa[y] = x; else { fa[x] = y; rk[y]++; } }
} ds;

class v2ray // trieeeeeeeeeeeeeee
{
public:
bitset<262144> st;
void insert(unsigned p)
{
p |= 131072;
do
{
if(st[p]) break;
st[p] = true;
} while(p >>= 1);
}
void erase(unsigned p)
{
p |= 131072;
st[p] = false;
while(p >>= 1)
{
st[p] = st[p * 2] | st[p * 2 + 1];
}
}
unsigned pred(unsigned p)
{
if(!st[1]) return 0;
p |= 131072;
while(p && (((~p)&1) || !st[p & 0xfffffffeu])) p >>= 1;
if(p == 0) return 0;
p &= 0xfffffffeu;
while(!(p >> 17))
{
if(st[2 * p + 1]) p = 2 * p + 1;
else p = 2 * p;
}
return p ^ 131072;
}
unsigned succ(unsigned p)
{
if(!st[1]) return 0;
p |= 131072;
while(p && (p&1 || !st[p | 1])) p >>= 1;
if(p == 0) return 0;
p |= 1;
while(!(p >> 17))
{
if(st[2 * p]) p = 2 * p;
else p = 2 * p + 1;
}
return p ^ 131072;
}
} ray;

unsigned ht[100005], qg[100005], rv[100005];
unsigned rdfn[200005], vla[200005];
unsigned msk[200005];

class msedge { public: unsigned u, v, w; } es[100005];
class jury { public: unsigned l, r, id; long long ans; } qry[200005];

unsigned val[200005], fa[200005], ch[200005][2];
unsigned dfn[200005], vx[200005], dep[200005], fr[200005];
unsigned stmin[25][100005];
unsigned arc[100005], hpr[100005], archpr[100005];

char ibf[100000005];
unsigned li = 0;
unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }

int main()
{
fread(ibf, 1, sizeof ibf, stdin);
int n, m, q;
n = qread(); m = qread(); q = qread();
for(int i=1;i<=m;i++)
{
es[i].u = qread(); es[i].v = qread(); es[i].w = qread();
es[i].u++; es[i].v++;
}
sort(es + 1, es + m + 1, [](const auto &x, const auto &y) { return x.w < y.w; });
for(int i=1;i<=n;i++)
{
ds.fa[i] = ht[i] = i;
ds.rk[i] = 0;
}
long long rs = 0;
int cur = n;
for(int i=1;i<=m;i++)
{
auto [u, v, w] = es[i];
if(ds.getfa(u) == ds.getfa(v)) continue;
rs += w;
vla[++cur] = w;
ch[cur][0] = ht[ds.getfa(u)]; ch[cur][1] = ht[ds.getfa(v)];
fa[ht[ds.getfa(u)]] = fa[ht[ds.getfa(v)]] = cur;
ds.merge(u, v);
ht[ds.getfa(u)] = cur;
}
int ddfn = 0;
unsigned cp = 0;
U([&](auto &&self, int u) -> void
{
rdfn[dfn[u] = ++ddfn] = u;
vx[ddfn] = dep[u];
if(ch[u][0])
{
dep[ch[u][0]] = dep[ch[u][1]] = dep[u] + 1;
if(!ch[ch[u][0]][0]) qg[ch[u][0]] = ++cp;
if(!ch[ch[u][1]][1]) qg[ch[u][1]] = ++cp;
if(ch[ch[u][0]][0]) self(self, ch[u][0]);
if(ch[ch[u][1]][1]) self(self, ch[u][1]);
}
})(cur);
assert(ddfn == n - 1);
for(int i=1;i<=n-1;i++)
{
stmin[0][i] = i;
for(int j=1;(1<<j)<=i;j++)
{
stmin[j][i] = min(stmin[j-1][i], stmin[j-1][i-(1<<(j-1))], [](int x, int y) { return vx[x] < vx[y]; });
}
}
for(int i=1;i<=2*n-1;i++)
{
fr[i] = dfn[fa[rdfn[i]]];
if(i <= n) rv[qg[i]] = dfn[fa[i]];
if(dfn[i]) val[dfn[i]] = vla[i];
}
auto qmin = [](int l, int r) -> int { int llen = __lg(r - l + 1); return min(stmin[llen][l+(1<<llen)-1], stmin[llen][r], [](int x, int y) { return vx[x] < vx[y]; }); };
auto qLCArdfn = [&](int dx, int dy) { return rv[dx] == rv[dy] ? rv[dx] : fr[qmin(rv[dx] + 1, rv[dy])]; };
int B = n / sqrt(q) + 1;
for(int i=1;i<=q;i++)
{
qry[i].l = qread(); qry[i].r = qread();
qry[i].l++; qry[i].r++;
qry[i].id = i;
}
sort(qry + 1, qry + q + 1, [&](const auto &x, const auto &y) { return x.l / B < y.l / B || x.l / B == y.l / B && x.r != y.r && ((x.r < y.r) ^ ((x.l / B) & 1)); });
int l = 1, r = 0;
for(int i=1;i<=q;i++)
{
while(l > qry[i].l)
{
l--;
unsigned qq = qg[l];
int vl = ray.pred(qq);
int vr = ray.succ(qq);
arc[qq] = vl; hpr[qq] = vr; archpr[qq] = 0;
unsigned kv = archpr[vl];
if(vl)
{
int v = qLCArdfn(vl, qq);
archpr[vl] = v;
if(!(msk[v]++))
{
rs -= val[v];
}
}
if(vr)
{
int v = qLCArdfn(qq, vr);
archpr[qq] = v;
if(!(msk[v]++))
{
rs -= val[v];
}
}
if(vl && vr)
{
int v = kv;
if(!--msk[v]) rs -= val[v];
}
if(vl) hpr[vl] = qq;
if(vr) arc[vr] = qq;
ray.insert(qq);
}
while(r < qry[i].r)
{
r++;
unsigned qq = qg[r];
int vl = ray.pred(qq);
int vr = ray.succ(qq);
arc[qq] = vl; hpr[qq] = vr; archpr[qq] = 0;
unsigned kv = archpr[vl];
if(vl)
{
int v = qLCArdfn(vl, qq);
archpr[vl] = v;
if(!(msk[v]++))
{
rs -= val[v];
}
}
if(vr)
{
int v = qLCArdfn(qq, vr);
archpr[qq] = v;
if(!(msk[v]++))
{
rs -= val[v];
}
}
if(vl && vr)
{
int v = kv;
if(!--msk[v]) rs -= val[v];
}
if(vl) hpr[vl] = qq;
if(vr) arc[vr] = qq;
ray.insert(qq);
}
while(l < qry[i].l)
{
unsigned qq = qg[l];
ray.erase(qq);
int vl = arc[qq];
int vr = hpr[qq];
if(vl) hpr[vl] = vr;
if(vr) arc[vr] = vl;
unsigned kv = archpr[vl];
if(vl && vr)
{
int v = qLCArdfn(vl, vr);
archpr[vl] = v;
if(!(msk[v]++)) rs += val[v];
}
else if(vl) archpr[vl] = 0;
if(vl)
{
int v = kv;
if(!--msk[v])
{
rs += val[v];
}
}
if(vr)
{
int v = archpr[qq];
if(!--msk[v])
{
rs += val[v];
}
}
archpr[qq] = 0;
arc[qq] = hpr[qq] = 0;
l++;
}
while(r > qry[i].r)
{
unsigned qq = qg[r];
ray.erase(qq);
int vl = arc[qq];
int vr = hpr[qq];
if(vl) hpr[vl] = vr;
if(vr) arc[vr] = vl;
unsigned kv = archpr[vl];
if(vl && vr)
{
int v = qLCArdfn(vl, vr);
archpr[vl] = v;
if(!(msk[v]++)) rs += val[v];
}
else if(vl) archpr[vl] = 0;
if(vl)
{
int v = kv;
if(!--msk[v])
{
rs += val[v];
}
}
if(vr)
{
int v = archpr[qq];
if(!--msk[v])
{
rs += val[v];
}
}
archpr[qq] = 0;
arc[qq] = hpr[qq] = 0;
r--;
}
qry[i].ans = rs;
}
sort(qry + 1, qry + q + 1, [](const auto &x, const auto &y) { return x.id < y.id; });
for(int i=1;i<=q;i++)
{
printf("%lld\n", qry[i].ans);
}
return 0;
}

壮观吧。

题解:P14375 [JOISC 2018] 图书馆 / Library

考虑先随机打乱必然不劣。

那么我们考虑一个前缀,我们已知它是若干个连续段。考虑如果插入一个数字之后能正确合并连续段,最终就可以还原整个序列。

考虑插入之后如果连续段数量增加 $1$,就说明和原本的连续段没有相邻;如果不变,说明有一个相邻;如果减少 $1$ 说明同时和两个相邻。

考虑怎么找到和哪个相邻,这个二分即可。

那么三个 case 的代价分别是 $1,\log n+2,2\log n+3$,考虑到这里的 $n$ 实际上是当前连通块数量且我们随机打乱了,所以这个做法非常优秀,实际上只有大约 $8200$ 的操作次数,简直比 $n\log_2 n$ 还优秀。

rec&code

rec。

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
/*
来乱搞一发

考虑加入后的连续段个数(总代价 n):

- 增加 1:直接加入(代价 0)。
- 不变:和恰好一个相邻,那么分治找到相邻点(代价 1 + log n)。
- 减少 1:和两个同时相邻,分治(代价 2 + 2 log n)。

看起来很棒。

随机打乱一下。

实测约 8200 次,非常优秀。
*/
#include <vector>
int Query(const std::vector<int>& M);
void Answer(const std::vector<int>& res);

#include <vector>
#include <random>
#include <cassert>
#include <algorithm>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

void Solve(int n)
{
vector<int> vt;
for(int i=1;i<=n;i++) vt.push_back(i);
shuffle(vt.begin(), vt.end(), mt19937_64(random_device{}()));
vector<vector<int>> wsx;
int vlst = 0;
for(int x : vt)
{
// printf("hey x = %d\n", x);
auto qr = [&](int l, int r) -> int { vector<int> msk(n); for(int i=l;i<=r;i++) for(int g : wsx[i]) msk[g-1] = true; msk[x-1] = true; return Query(msk); };
vector<int> vr(n);
for(const auto &v : wsx)
{
for(int w : v) vr[w-1] = true;
}
vr[x-1] = true;
int vp = Query(vr);
if(vp == vlst)
{
int l = 0, r = wsx.size()-1;
while(l < r)
{
int mid = (l + r) / 2;
if(qr(l, mid) == mid - l + 1) r = mid;
else l = mid + 1;
}
if(wsx[l].size() == 1) wsx[l].push_back(x);
else
{
vector<int> msk(n);
msk[x-1] = true;
msk[wsx[l][0]-1] = true;
if(Query(msk) == 2) wsx[l].push_back(x);
else
{
reverse(wsx[l].begin(), wsx[l].end());
wsx[l].push_back(x);
}
}
}
else if(vp == vlst - 1)
{
vector<int> rs;
U([&](auto &&self, int l, int r, bool kflag) -> void
{
if(l == r)
{
rs.push_back(l);
return;
}
int mid = (l + r) / 2;
int ree = qr(l, mid);
if(kflag)
{
if(ree == mid - l) self(self, l, mid, true);
else if(ree == mid - l + 1)
{
self(self, l, mid, false);
self(self, mid + 1, r, false);
}
else self(self, mid + 1, r, true);
}
else
{
if(ree == mid - l + 1) self(self, l, mid, false);
else self(self, mid + 1, r, false);
}
})(0, wsx.size()-1, true);
assert(rs.size() == 2);
int u = rs[0], v = rs[1];
// printf("u = %d, v = %d\n", u, v);
if(wsx[u].size() < wsx[v].size()) swap(u, v);
if(wsx[u].size() == 1)
{
wsx[u].push_back(x);
wsx[u].push_back(wsx[v][0]);
wsx.erase(wsx.begin() + v);
}
else if(wsx[v].size() == 1)
{
vector<int> msk1(n);
msk1[wsx[u][0] - 1] = true;
msk1[x-1] = true;
if(Query(msk1) == 1) reverse(wsx[u].begin(), wsx[u].end());
wsx[u].push_back(x);
wsx[u].push_back(wsx[v][0]);
wsx.erase(wsx.begin() + v);
}
else
{
vector<int> msk1(n), msk2(n);
msk1[wsx[u][0] - 1] = true; msk2[wsx[v][0] - 1] = true;
msk1[x-1] = true; msk2[x-1] = true;
if(Query(msk1) == 1) reverse(wsx[u].begin(), wsx[u].end());
if(Query(msk2) == 2) reverse(wsx[v].begin(), wsx[v].end());
wsx[u].push_back(x);
for(int g : wsx[v]) wsx[u].push_back(g);
wsx.erase(wsx.begin() + v);
}
}
else wsx.push_back({x});
vlst = vp;
assert(vp == wsx.size());
// printf("now connected: "); for(const auto &p : wsx) { printf("["); for(int g : p) printf("%d ", g); printf("] "); } printf("\n");
}
assert(wsx.size() == 1 && wsx[0].size() == n);
Answer(wsx[0]);
}

题解:P14348 [JOISC 2019] 穿越时空 Bitaro / Bitaro, who Leaps through Time

分块块简单题。场切了,我真棒。


不妨假设只能往右。平移一下把时间流逝去了。考虑多次操作之后会发生什么。

首先注意到若干次操作之后得到的可能值一定是个区间。

并且若这个区间在某个时刻变为了单点,则以后都是单点;而如果不是,则其之间的所有操作复合起来就相当于适配到最终可能区间,对于变化量也是如此。

那么就可以得到一个做法:首先找到这个位置往后第一个变为单点的位置,计算其贡献,然后通过某种方式预处理之后的贡献。

这个直接暴力做是很差的,考虑分块优化一下,对于每个块处理变为单点的位置(可能不存在),$O(n\sqrt n)$ 做完了。

rec&code

rec。

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
笑点解析:这个区第一次以为是个图,第二次以为是个树。

原来是个链啊.jpg

那是不是就好做了。

分块一下。对于每个块维护收敛位置和从收敛开始的总代价。当然还有最终的位置。需要特判不收敛的情况。

正着和反者各维护一个。

时间复杂度 O(n sqrt n),完全胜利。
*/
#include <cmath>
#include <cstdio>
#include <algorithm>
#ifdef LB_IS_TIMING
#include <chrono>
#endif

using namespace std;

const unsigned B = 512;
long long l1[300005], l2[300005], r1[300005], r2[300005];
long long c1pos[300005 / B + 5], c1sum[300005 / B + 5], c2pos[300005 / B + 5], c2sum[300005 / B + 5];
long long r1nl[300005], r1nr[300005], r2nl[300005], r2nr[300005];

int main()
{
#ifdef LB_IS_TIMING
auto bgn = chrono::high_resolution_clock::now();
#endif
int n, q;
scanf("%d%d", &n, &q);
n--;
for(int i=1;i<=n;i++)
{
long long l, r;
scanf("%lld%lld", &l, &r);
l1[i] = l - i;
r1[i] = r - i - 1;
l2[i] = l + i;
r2[i] = r + i - 1;
}
auto setrng = [](long long l, long long r, long long v) { return min(r, max(l, v)); };
auto buildblk = [&](int id) -> void // build block lol
{
long long vl = -2000000000, vr = 2000000000;
c1pos[id] = c2pos[id] = -1;
c1sum[id] = c2sum[id] = 0;
for(int i=(id-1)*B+1;i<=min((unsigned)n, id*B);i++)
{
long long rvl = vl, rvr = vr;
vl = setrng(l1[i], r1[i], vl);
vr = setrng(l1[i], r1[i], vr);
r1nl[i] = vl; r1nr[i] = vr;
if(vl == vr && c1pos[id] == -1) c1pos[id] = i;
if(rvl == rvr) c1sum[id] += max(0ll, rvl - vl);
}
vl = -2000000000; vr = 2000000000;
for(int i=min((unsigned)n, id*B);i>=(id-1)*B+1;i--)
{
long long rvl = vl, rvr = vr;
vl = setrng(l2[i], r2[i], vl);
vr = setrng(l2[i], r2[i], vr);
r2nl[i] = vl; r2nr[i] = vr;
if(vl == vr && c2pos[id] == -1) c2pos[id] = i;
if(rvl == rvr) c2sum[id] += max(0ll, rvl - vl);
}
};
for(int i=1;i<=(n+B-1)/B;i++) buildblk(i);
while(q--)
{
int t;
scanf("%d", &t);
if(t == 1) // modify
{
int p, s, t;
scanf("%d%d%d", &p, &s, &t);
l1[p] = s - p;
r1[p] = t - p - 1;
l2[p] = s + p;
r2[p] = t + p - 1;
buildblk((p+B-1)/B);
}
else // query
{
int l, r;
long long s, t;
scanf("%d%lld%d%lld", &l, &s, &r, &t);
if(l == r)
{
printf("%lld\n", max(0ll, s-t));
continue;
}
if(l < r)
{
t -= r;
s -= l;
r--;
int lid = (l + B - 1) / B, rid = (r + B - 1) / B;
if(lid == rid)
{
long long ps = 0;
for(int i=l;i<=r;i++)
{
long long lst = s;
s = setrng(l1[i], r1[i], s);
ps += max(0ll, lst - s);
}
printf("%lld\n", ps + max(0ll, s - t));
continue;
}
long long ps = 0;
for(int i=l;i<=lid*B;i++)
{
long long lst = s;
s = setrng(l1[i], r1[i], s);
ps += max(0ll, lst - s);
}
for(int i=lid+1;i<rid;i++)
{
if(c1pos[i] == -1)
{
long long tgt = setrng(r1nl[i*B], r1nr[i*B], s);
ps += max(0ll, s - tgt);
s = tgt;
}
else
{
long long tg1 = (c1pos[i] == (i-1)*B+1 ? s : setrng(r1nl[c1pos[i]-1], r1nr[c1pos[i]-1], s));
ps += max(0ll, s - tg1);
long long tg2 = setrng(r1nl[c1pos[i]], r1nr[c1pos[i]], tg1);
ps += max(0ll, tg1 - tg2);
ps += c1sum[i];
s = r1nl[i*B];
}
}
for(int i=(rid-1)*B+1;i<=r;i++)
{
long long lst = s;
s = setrng(l1[i], r1[i], s);
ps += max(0ll, lst - s);
}
printf("%lld\n", ps + max(0ll, s - t));
}
else
{
s += l - 1;
t += r - 1;
l--;
int lid = (l + B - 1) / B, rid = (r + B - 1) / B;
if(lid == rid)
{
long long ps = 0;
for(int i=l;i>=r;i--)
{
long long lst = s;
s = setrng(l2[i], r2[i], s);
ps += max(0ll, lst - s);
}
printf("%lld\n", ps + max(0ll, s - t));
continue;
}
long long ps = 0;
for(int i=l;i>=(lid-1)*B+1;i--)
{
long long lst = s;
s = setrng(l2[i], r2[i], s);
ps += max(0ll, lst - s);
}
for(int i=lid-1;i>rid;i--)
{
if(c2pos[i] == -1)
{
long long tgt = setrng(r2nl[(i-1)*B+1], r2nr[(i-1)*B+1], s);
ps += max(0ll, s - tgt);
s = tgt;
}
else
{
long long tg1 = (c2pos[i] == i*B ? s : setrng(r2nl[c2pos[i]+1], r2nr[c2pos[i]+1], s));
ps += max(0ll, s - tg1);
long long tg2 = setrng(r2nl[c2pos[i]], r2nr[c2pos[i]], tg1);
ps += max(0ll, tg1 - tg2);
ps += c2sum[i];
s = r2nl[(i-1)*B+1];
}
}
for(int i=rid*B;i>=r;i--)
{
long long lst = s;
s = setrng(l2[i], r2[i], s);
ps += max(0ll, lst - s);
}
printf("%lld\n", ps + max(0ll, s - t));
}
}
}
#ifdef LB_IS_TIMING
fprintf(stderr, "Time: %llu ms\n", chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - bgn).count());
#endif
return 0;
}

P7803 [JOI Open 2021] 杂交 / Crossing 不用哈希

怎么 lihaochen 哈希被卡了,那我不用哈希你不炸了!!


考虑这个运算是什么。

将字符串视为模三意义下的序列,那么 $x,y$ 进行操作后即为 $(x,y)\equiv -x-y\pmod 3$。

考虑只有三个串 $x,y,z$,且 $(x,y)$ 是关于 $x,y$ 的线性组合,同时结果模 $3$,那么说明最多有 $3^3=27$ 种结果。同时注意到系数之和必然为 $1$(在模 $3$ 意义下),所以系数只能是下面九种($\texttt +$ 代表 $1$,$\texttt -$ 代表 $-1$,$\texttt 0$ 代表 $0$):

  • $\texttt{+±}$;
  • $\texttt{±+}$;
  • $\texttt{-++}$;
  • $\texttt{00+}$;
  • $\texttt{0+0}$;
  • $\texttt{+00}$;
  • $\texttt{–0}$;
  • $\texttt{-0-}$;
  • $\texttt{0–}$;

经过验证都是可以取到的。

同时每一位都是独立的,所以我们可以对于每一位,处理出可能是上面九种变换中的哪些;只要存在一种变换使得对于每一位都是合法的,最终就有解。

于是可以使用位掩码处理这个,使用数据结构维护九位的二进制数,每一位分别代表一种变换是否合法,只需要维护全局与就可以了。

但是发现这样并不能直接区间修改,因为 $s_A,s_B,s_C$ 对应的位可能不同,此时即使 $T$ 相同对应的位掩码也不同。所以注意到 $s_A,s_B,s_C$ 在某一位中最多有 $3^3=27$ 种不同可能性,那么开 $27$ 个数据结构即可。

数据结构可以用线段树或者 ODT,复杂度均为 $O(n\log n)$,带 $3^3=27$ 的常数,好处是不会被卡哈希。

rec&code

rec。

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
170
171
172
173
174
175
/*
|0 1 2
-+-----
0|0 2 1
1|2 1 0
2|1 0 2

∴ (x,y) -> -x-y

结合律?

((a,b),c) = -(-a-b)-c = a+b-c
(a,(b,c)) = -a-(-b-c) = -a+b+c

并没有结合律。

只有交换律没有结合律,这是什么【】【】【】【】东西

考虑到这个一定是线性的。

将数字 k 描述为操作 +k。

That's Linear!

但是这不能表述为复合因为没有结合律。

【再次【】【】日本出题人】

((a,b),(b,c)) = -(-a-b)-(-b-c) = a+c-b = ((a,c),b)

欸?

好像很牛。

((a,b),((b,c),d)) = a+b-(b+c-d) = a+c-d = ((a,c),d)

欸??

太有意思了。

每个位置的系数难道不是,(-1)^(所在的括号深度) 吗

那这个很牛了。

转为括号问题?

不妨钦定可以任意赋权。

1010
1102
0100

为什么无法转变为 0000.

考虑第一个行系数一定为 0(因为第三列),然后第二行(因为第二列)一定为 0,然后爆炸。

等等好像也不能任意赋权。

需要保证和为 1.

(如果能凑出全 0 就好了)

哦初始只有三个串串吗

/bangbangt

可行的系数就是:

- - 0
- 0 -
0 - -
- + +
+ - +
+ + -
+ 0 0
0 + 0
0 0 +

有且仅有这些是可行的

……是不是做完了?

考虑是区间赋值,上个 ODT,然后就做完了。

甚至不用 ODT。对于每个位置可以用一个 bitmask 表示可能的系数,然后全局 & 即可。

区间赋值,全局 &。

哦还有小细节就是 a, b, c 若对应位置不同则处理方法也不同,那就开 3 * 3 * 3 个线段树。
*/
#include <cstdio>
#include <string>
#include <iostream>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
const auto getr = [](int l, int r, int id) { return id + ((l + r) / 2 - l + 1) * 2; };

class segtree
{
public:
class node { public: unsigned vset, zand; bool pexist; } nodes[400005];
void pushdown(int l, int r, int id) { if(nodes[id].vset != 114514) { nodes[id + 1].vset = nodes[id + 1].zand = (nodes[id + 1].pexist ? nodes[id].vset : 0x1ff); nodes[getr(l, r, id)].vset = nodes[getr(l, r, id)].zand = (nodes[getr(l, r, id)].pexist ? nodes[id].vset : 0x1ff); nodes[id].vset = 114514; } }
void pushup(int l, int r, int id) { nodes[id].zand = nodes[id + 1].zand & nodes[getr(l, r, id)].zand; nodes[id].pexist = nodes[id + 1].pexist || nodes[getr(l, r, id)].pexist; if(!nodes[id].pexist) nodes[id].zand = 0x1ff; }
void build(int n, const string &x, const string &y, const string &z, char px, char py, char pz) { U([&](auto &&self, int l, int r, int id) -> void { if(l == r) { nodes[id] = {114514, 0x1ff, x[l - 1] == px && y[l - 1] == py && z[l - 1] == pz}; return; } self(self, l, (l + r) / 2, id + 1); self(self, (l + r) / 2 + 1, r, getr(l, r, id)); pushup(l, r, id); })(1, n, 1); }
void vset(int n, int L, int R, int val) { U([&](auto &&self, int l, int r, int vl, int vr, int id) -> void { if(l == vl && r == vr || !nodes[id].pexist) { nodes[id].vset = nodes[id].zand = (nodes[id].pexist ? val : 0x1ff); return; } pushdown(l, r, id); if(vl <= (l + r) / 2) self(self, l, (l + r) / 2, vl, min(vr, (l + r) / 2), id + 1); if(vr > (l + r) / 2) self(self, (l + r) / 2 + 1, r, max(vl, (l + r) / 2 + 1), vr, getr(l, r, id)); pushup(l, r, id); })(1, n, L, R, 1); }
unsigned qaand() { return nodes[1].pexist ? nodes[1].zand : 0x1ff; }
} segs[30]; // 91MB

int main()
{
int n;
scanf("%d", &n);
string sa, sb, sc;
cin >> sa >> sb >> sc;
auto cmask = [](char a, char b, char c, char t) -> unsigned
{
int va = (a == 'J' ? 0 : (a == 'O' ? 1 : 2));
int vb = (b == 'J' ? 0 : (b == 'O' ? 1 : 2));
int vc = (c == 'J' ? 0 : (c == 'O' ? 1 : 2));
int vt = (t == 'J' ? 0 : (t == 'O' ? 1 : 2));
unsigned res = 0;
if((va + vb + vt) % 3 == 0) res |= 1 << 0;
if((va + vc + vt) % 3 == 0) res |= 1 << 1;
if((vb + vc + vt) % 3 == 0) res |= 1 << 2;
if((vc + vt) % 3 == (va + vb) % 3) res |= 1 << 3;
if((vb + vt) % 3 == (va + vc) % 3) res |= 1 << 4;
if((va + vt) % 3 == (vb + vc) % 3) res |= 1 << 5;
if(va == vt) res |= 1 << 6;
if(vb == vt) res |= 1 << 7;
if(vc == vt) res |= 1 << 8;
// printf("returned %u\n", res);
return res;
};
auto qid = [](char a, char b, char c) -> unsigned { return (a == 'J' ? 0 : (a == 'O' ? 1 : 2)) * 9 + (b == 'J' ? 0 : (b == 'O' ? 1 : 2)) * 3 + (c == 'J' ? 0 : (c == 'O' ? 1 : 2)); };
for(char x : {'J', 'O', 'I'})
{
for(char y : {'J', 'O', 'I'})
{
for(char z : {'J', 'O', 'I'})
{
segs[qid(x, y, z)].build(n, sa, sb, sc, x, y, z);
}
}
}
int q;
scanf("%d", &q);
string t;
cin >> t;
for(int i=1;i<=n;i++)
{
segs[qid(sa[i-1], sb[i-1], sc[i-1])].vset(n, i, i, cmask(sa[i-1], sb[i-1], sc[i-1], t[i-1]));
}
auto qvalid = []() -> bool { unsigned vm = 0x1ff; for(int i=0;i<27;i++) if(!(vm &= segs[i].qaand())) return false; return true; };
printf(qvalid() ? "Yes\n" : "No\n");
while(q--)
{
int l, r;
char c;
scanf("%d%d %c", &l, &r, &c);
for(char x : {'J', 'O', 'I'})
{
for(char y : {'J', 'O', 'I'})
{
for(char z : {'J', 'O', 'I'})
{
segs[qid(x, y, z)].vset(n, l, r, cmask(x, y, z, c));
}
}
}
printf(qvalid() ? "Yes\n" : "No\n");
}
return 0;
}

题解:P14351 [JOISC 2019] 矿物 / Minerals

卡了一万年常,题解以记之。


考虑 Subtask $2$ 的特殊性质。

考虑分治。每次取出左边的一半,对于右边依次判断是否属于左边,然后分治下去。操作次数 $T(n)=2T\left(\dfrac{n}{2}\right)+3n$,先不急着优化。

考虑一般情况,此时可以扫一遍知道每个数是第一次还是第二次出现,分为两组,然后直接用上面的分治。可以用 $2n$ 次操作解决。

期望得分 $40\mathrm{pts}$,考虑优化。

首先分治并不要求所有数字都初始不在集合中,于是可以去除一部分的清空。此时还是不错的,左边和右边都是要么初始全选,要么初始全不选。

然后在划分到左右边的时候,如果左边或右边满了,那么剩下所有数字都分到另一边。

考虑上面两个优化一起使用会出现奇奇怪怪的问题,具体来讲就是右边不一定全在或全不在集合中,于是判断在和不在的哪个多。

考虑两边都两个元素的时候分讨用最多两次操作解决掉。

考虑划分并不平衡,经过计算(具体可以看其它题解)最优划分点是 $\dfrac{3-\sqrt 5}{2}$,换句话说划分的时候应该让左边的操作次数在 $\alpha=\dfrac{3-\sqrt 5}{2}N$,$N$ 是左边元素个数。

不妨假设左边左 $\alpha$ 部分选中,右 $1-\alpha$ 部分不选。的考虑在统一右边状态的时候,一个位置 $k$ 如果改变状态后改变了不同元素数量,则它一定不在左半边的左 $\alpha$ 部分。

然后就过了,比较极限,shuffle 一下操作次数在 $999000$ 以内。

rec&code

rec。

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// 还剩 55min,我能 AK 吗?
/*
subtask 2: 分治。每次在左边一半中选一半,右边挨个判断,然后递归。总操作次数 T(n) = 3/4n + 2T(n/2),非常正确……还要清空啊。先不急。

一般情况:still? 感觉很困难。考虑分治。通过 n 次操作可以判断每个 mineral 的另一半在左边还是右边。

左左/右右:分治。左右:用上面的方法。

哦不这个过不了
*/
int Query(int x);
void Answer(int x, int y);
#include <vector>
#include <random>
#include <cassert>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

void Solve(int n)
{
vector<int> vt;
for(int i=1;i<=2*n;i++) vt.push_back(i);
mt19937_64 mt(random_device{}());
shuffle(vt.begin(), vt.end(), mt);
unsigned long long s1 = 0, s2 = 0, s3 = 0;
vector<unsigned char> exis(2*n+5);
vector<int> pl(2*n+5);
int lstans = 0;
int kks = 0;
auto q1 = [&](int x) { s1++; kks -= 2 * exis[x] - 1; exis[x] ^= 1; return lstans = Query(x); };
auto q2 = [&](int x) { s2++; kks -= 2 * exis[x] - 1; exis[x] ^= 1; return lstans = Query(x); };
auto solr = U([&](auto &&self, vector<int> l, vector<int> r, bool fg) -> void
{
assert(l.size() == r.size());
if(l.size() == 0) return;
if(l.size() == 1)
{
Answer(l[0], r[0]);
return;
}
if(l.size() == 2)
{
int c1 = exis[l[0]] + exis[l[1]], c2 = exis[r[0]] + exis[r[1]];
if(c1 > c2)
{
swap(l, r);
swap(c1, c2);
}
if(c1 == 0 && c2 == 0)
{
q1(l[0]);
int ll = lstans;
if(q1(r[0]) != ll) swap(r[0], r[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
if(c1 == 0 && c2 == 1)
{
if(exis[r[1]]) swap(r[0], r[1]);
int ll = lstans;
if(q1(l[0]) != ll) swap(r[0], r[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
if(c1 == 0 && c2 == 2)
{
q1(r[1]);
int ll = lstans;
if(q1(l[0]) != ll) swap(r[0], r[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
if(c1 == 1 && c2 == 1)
{
int ll = lstans;
if(exis[r[1]]) swap(r[0], r[1]);
if(exis[l[1]]) swap(l[0], l[1]);
if(q1(l[0]) != ll) swap(l[0], l[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
if(c1 == 1 && c2 == 2)
{
if(exis[l[1]]) swap(l[0], l[1]);
int ll = lstans;
if(q1(r[0]) != ll) swap(r[0], r[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
if(c1 == 2 && c2 == 2)
{
q1(l[1]);
int ll = lstans;
if(q1(r[0]) != ll) swap(r[0], r[1]);
Answer(l[0], r[0]);
Answer(l[1], r[1]);
return;
}
return;
}
vector<int> al, ar, bl, br;
int cnt = 0;
for(int v : r) if(exis[v]) cnt++; else cnt--;
bool ftarget = cnt > 0;
int vcnt = 0;
long double beta = 0.3819660112501051;
long double alpha = (exis[l[0]] ^ ftarget) ? 1 - beta : beta;
unsigned spl = max(1, int(l.size() * alpha));
for(int i=0;i<l.size();i++)
{
assert(exis[l[i]] == exis[l.back()]);
if(i < spl)
{
al.push_back(l[i]);
if(!(exis[l[i]] ^ ftarget))
{
q1(l[i]);
vcnt++;
}
}
else
{
bl.push_back(l[i]);
if(exis[l[i]] ^ ftarget)
{
q1(l[i]);
vcnt++;
}
}
}
// int bsum = kks;
// for(int v : r) bsum -= exis[v];
// if(spl + bsum != lstans)
// {
for(int i=r.size()-1;i>=0;i--)
{
int v = r[i];
if(exis[v] ^ ftarget)
{
int lls = lstans;
int lx = q1(v);
if((lx != lls) ^ ftarget)
{
s3++;
pl[v] = -1;
}
else pl[v] = 0;
}
else pl[v] = 0;
}
// }
// else
// {
// for(int v : r)
// {
// if(exis[v] ^ ftarget)
// {
// s3++;
// pl[v] = 1;
// }
// else pl[v] = 0;
// }
// }
// assert(exis[l[0]] == exis[r[0]]);
assert(abs(vcnt - int(l.size() * beta)) <= 2);
for(int i=0;i<r.size();i++)
{
if(ar.size() == al.size())
{
br.push_back(r[i]);
if(pl[r[i]] == -1) s3--;
continue;
}
if(br.size() == bl.size())
{
ar.push_back(r[i]);
if(pl[r[i]] == -1) s3--;
continue;
}
bool ffg = false;
if(pl[r[i]] == 1) ffg = true;
else if(pl[r[i]] == -1) ffg = false;
else
{
int lls = lstans;
int lx = q1(r[i]);
ffg = (lx == lls) ^ ftarget;
}
if(ffg) ar.push_back(r[i]);
else br.push_back(r[i]);
}
self(self, al, ar, false);
self(self, bl, br, true);
});
U([&](auto &&self, vector<int> v) -> void
{
vector<int> ql, qr;
for(int x : v)
{
// if(lstans == n)
// {
// qr.push_back(x);
// continue;
// }
int ll = lstans;
q2(x);
if(lstans == ll)
{
// q2(x);
qr.push_back(x);
}
else ql.push_back(x);
}
solr(ql, qr, true);
})(vt);
fprintf(stderr, "s1 = %llu, s2 = %llu, saved (approximately) %llu\n", s1, s2, s3);
}

题解:CF1864F Exotic Queries

Because the chosen segments cannot intersect, there is a trivial greedy algorithm: Find the minimum value $v$ in the whole sequence, then subtract $v$ from all elements, split the sequence at $0$s, then repeat the process above until all elements are $0$. The optimality of the greedy algorithm is trivial.

Consider the cost (number of operations) of the algorithm above. For each position $i$, let its predecessor (the largest position where the value is the same as $a_i$; if it does not exist, let it be $0$) be $t_i$. Trivially, if $t_i\neq 0$ and $\min\limits_{k\in [t_i,i]}a_k=a_i$, then the elements at positions $i$ and $t_i$ become $0$ in the same operation.

So, trivially, the total cost is:

$$ \sum_{i=1}^n [t_i=0\lor \exists k \text{ s.t. } a_k<a_i,k\in [t_i,i]] $$

It is trivial that this problem allows an offline algorithm. Thus we can trivially enumerate $l$ from $n$ to $1$, and let $v_i$ be the total contribution of all $j$ where $a_j=i$, and trivially use a trivial Fenwick tree to answer all queries with left endpoint $l$ because $\text{Ans}=\displaystyle\sum_{l\le i\le r}v_i$ (and because we only considered $i$ such that $a_i\ge l$, all $v_j$ with $j<l$ are trivially zero).

For each $i$, we trivially call the segment $[t_i,i]$ alive if and only if there is no $k$ satisfying $l\le a_k<a_i$ and $k\in[t_i,i]$.

What happens to the values $v_i$ when $l$ decreases by $1$? Since $l$ is the minimum value among the currently considered values, for each $i$ with $a_i=l$, just trivially traverse all alive segments $[x,y]$ containing $i$, and mark them as dead (then trivially increase $v_{a_x}$ by $1$). Then set $v_l=[\exists i\text{ s.t. }a_i=l]$, and for each adjacent pair of occurrences $x<y$ with $a_x=a_y=l$, mark the segment $[x,y]$ as alive. The total number of segments processed in this procedure is trivially $O(n)$. The proof is trivial; leave it as an exercise.

So the only problem is to trivially implement a data structure that supports:

  1. Inserting a segment. It is trivial that (in this problem) all inserted segments have distinct left endpoints.
  2. Finding a segment containing a position $x$, then remove the segment.

This can be solved trivially by using a segment tree maintaining the right endpoint associated with each left endpoint and their range maxima. We can trivially process the second query by performing binary search on the segment tree in $O(\log n)$ time.

Thus, the total time complexity is $O(n\log n)$ (and the trivial complexity is $O(\text{BB}(n))$).

rec&code

rec。

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
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

int a[1000005];
vector<int> gval[1000005];

class segtree
{
public:
constexpr int getr(int l, int r, int id) { return id + ((l + r) / 2 - l + 1) * 2; }
int maxn[2000005];
void pushup(int l, int r, int id) { maxn[id] = max(maxn[id + 1], maxn[getr(l, r, id)]); }
void build(int n) { U([&](auto &&self, int l, int r, int id) -> void { if(l == r) { maxn[id] = 0; return; } self(self, l, (l + r) / 2, id + 1); self(self, (l + r) / 2 + 1, r, getr(l, r, id)); pushup(l, r, id); })(1, n, 1); }
void vset(int n, int pos, int val) { U([&](auto &&self, int l, int r, int id) -> void { if(l == r) { maxn[id] = val; return; } if(pos <= (l + r) / 2) self(self, l, (l + r) / 2, id + 1); else self(self, (l + r) / 2 + 1, r, getr(l, r, id)); pushup(l, r, id); })(1, n, 1); }
int qval(int n, int pos) { return U([&](auto &&self, int l, int r, int id) { if(l == r) return maxn[id]; if(pos <= (l + r) / 2) return self(self, l, (l + r) / 2, id + 1); else return self(self, (l + r) / 2 + 1, r, getr(l, r, id)); })(1, n, 1); }
int qsearch(int n, int pos) { return U([&](auto &&self, int l, int r, int id) -> int { if(l == r) return maxn[id] >= pos ? l : -1; if(pos <= (l + r) / 2) return self(self, l, (l + r) / 2, id + 1); if(maxn[id + 1] >= pos) return self(self, l, (l + r) / 2, id + 1); else return self(self, (l + r) / 2 + 1, r, getr(l, r, id)); })(1, n, 1); }
} seg;

class fenwick
{
public:
int st[1000005];
void vadd(int pos, int val, int n) { pos++; n++; do { st[pos] += val; } while((pos += pos & -pos) <= n); }
int qsum(int pos) { pos++; int sum = 0; do { sum += st[pos]; } while(pos -= pos & -pos); return sum; }
} fw;

class qry { public: int l, r, id; } qrys[1000005];
int ans[1000005];

int main()
{
int n, q;
scanf("%d%d", &n, &q);
for(int i=1;i<=n;i++)
{
scanf("%d", a + i);
gval[a[i]].push_back(i);
}
for(int i=1;i<=q;i++)
{
scanf("%d%d", &qrys[i].l, &qrys[i].r);
qrys[i].id = i;
}
sort(qrys + 1, qrys + q + 1, [](const auto &x, const auto &y) { return x.l > y.l; });
int cur = 1;
seg.build(n);
for(int i=n;i>=1;i--)
{
for(int x : gval[i])
{
int res;
while((res = seg.qsearch(n, x)) != -1)
{
fw.vadd(a[res], 1, n);
// printf("i = %d, x = %d, found [%d, %d], level %d --\n", i, x, res, seg.qval(n, res), a[res]);
seg.vset(n, res, 0);
}
}
for(int j=0;j+1<gval[i].size();j++)
{
seg.vset(n, gval[i][j], gval[i][j+1]);
}
if(!gval[i].empty()) fw.vadd(i, 1, n);
while(cur <= q && qrys[cur].l == i)
{
ans[qrys[cur].id] = fw.qsum(qrys[cur].r);
cur++;
}
}
for(int i=1;i<=q;i++)
{
printf("%d\n", ans[i]);
}
return 0;
}

题解:P6662 [POI 2019/2020 R1] Przedszkole / 幼儿园

This is the graph coloring counting problem, a classical #P-hard problem. There are three subtasks (excluding brute force), and three solutions.

Subtask $2$

$n \le 15$.

Obviously, at most $n$ colors are used, which is much smaller than $k\le 10^9$. Let $f_c$ be the number of ways of using exactly $c$ colors (all used); then the contribution of such colorings to the final answer is $f_c\dbinom{k}{c}$. Thus we only need to calculate $f_{1\dots n}$.

A coloring is valid if and only if, for each color $c$, the vertices of color $c$ form an independent set. Let $g_{c,S}$ be the number of ways to color the vertex set $S$ with exactly $c$ colors (all used). Then $g_{c,S}=\displaystyle\sum_{T\subsetneq S} [S\setminus T\text{ is an independent set}]g_{c-1,T}$.

This can be calculated directly in $O(n3^n+m2^n)$ time, with $O(n)$ per query, so the total time complexity is $O(n3^n+m2^n+qn)$, fast enough. And we can use set power series to optimize it to $O(n^3 2^n+qn)$, not essential.

Subtask $3$

We can use the inclusion-exclusion principle to solve this subtask. Let $f_{k,S}$ be the number of colorings that use exactly $k$ colors (all used) and in which every edge in $S$ has endpoints of the same color ($S\subseteq E$). Then the number of valid colorings with exactly $k$ colors is $\displaystyle\sum_{S\subseteq E} (-1)^{\lvert S\rvert} f_{k,S}$.

Enumerate $S$. For each edge set $S$, let $p$ be the number of connected components when considering only the edges in $S$. It seemed like $f_{k,S}=k^p$? Actually no, because we may use only $k-1$ or fewer colors, so we define $g_{k,S}=k^p$ which is similar to $f$ but uses at most $k$ colors. Converting $g$ to $f$ is trivial - just use binomial inversion.

This can be solved in $O(m2^m\alpha(n)+qm)$ time. This is not very fast, so several optimizations are needed; please refer to the code.

Subtask $4$

This is very simple. The whole graph consists of several cycles. Obviously, the number of $k$-colorings of a single cycle with $n$ vertices is $(k-1)^n+(-1)^n(k-1)$. So we can solve this in $O(nq)$, fast enough.

rec&code

rec.

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <cstdio>
#include <vector>
#include <bitset>
#include <chrono>
#include <algorithm>
#ifdef LB_IS_DEBUGGING
#include <signal.h>
#define __debugbreak() raise(SIGTRAP)
#endif

using namespace std;

vector<unsigned> web[100005];
unsigned deg[100005];
unsigned gid[100005];
unsigned sz[100005];
bitset<100005> vis;
unsigned long long pans[100005][30];
unsigned long long pq[100005][30];
unsigned long long kv[(1<<15)+5], kg[(1<<15)+5], tp[(1<<15)+5];
unsigned bk[30];
unsigned long long fact[100005], ifact[100005];
unsigned rv[100005];

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };

class dsu
{
public:
int fa[100005], rk[100005];
int getfa(int x) { while(x != fa[x]) x = fa[x] = fa[fa[x]]; return x; }
unsigned merge(int x, int y) { if((x = getfa(x)) == (y = getfa(y))) return 0; if(rk[x] < rk[y]) fa[x] = y; else if(rk[x] > rk[y]) fa[y] = x; else { fa[x] = y; rk[y]++; } return 1; }
} ds;

char ibf[100000005];
unsigned li = 0;
unsigned qread() { unsigned res = 0; char ch; while((ch = ibf[li++]) < '0' || ch > '9'); do { res = res * 10 + ch - '0'; } while((ch = ibf[li++]) >= '0' && ch <= '9'); return res; }

int main()
{
auto bgn = chrono::high_resolution_clock::now();
// freopen("prz.in", "r", stdin);
// freopen("prz.out", "w", stdout);
fread(ibf, 1, sizeof ibf, stdin);
int n, m, q;
n = qread(); m = qread(); q = qread();
// scanf("%d%d%d", &n, &m, &q);
auto qpow = [](unsigned long long x, unsigned long long y) -> unsigned long long { unsigned long long ans = 1; do { if(y & 1) ans = ans * x % 1000000007; x = x * x % 1000000007; } while(y >>= 1); return ans; };
fact[0] = 1;
for(int i=1;i<=n;i++) fact[i] = fact[i-1] * i % 1000000007;
ifact[n] = qpow(fact[n], 1000000005);
for(int i=n-1;i>=0;i--) ifact[i] = ifact[i+1] * (i+1) % 1000000007;
auto comb = [](int x, int y) { return x < y || y < 0 ? 0 : fact[x] * ifact[y] % 1000000007 * ifact[x-y] % 1000000007; };
for(int i=1;i<=m;i++)
{
int u, v;
// scanf("%d%d", &u, &v);
u = qread(); v = qread();
deg[u]++;
deg[v]++;
web[u].push_back(v);
web[v].push_back(u);
}
int fg = true;
for(int i=1;i<=n;i++) if(deg[i] != 2) fg = false;
int cur = 0;
unsigned pc1 = 0;
#ifndef TEST3
if(fg)
{
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
vector<int> vt;
U([&](auto &&self, int u) -> void
{
vis[u] = true; vt.push_back(u);
for(int v : web[u])
{
if(!vis[v])
{
self(self, v);
}
}
})(i);
sz[++cur] = vt.size();
}
while(q--)
{
int c;
// scanf("%d", &c);
c = qread();
unsigned long long mul = 1;
for(int i=1;i<=cur;i++)
{
mul = mul * (qpow(c-1, sz[i]) + qpow(1000000006, sz[i]) * (c - 1) % 1000000007) % 1000000007;
}
printf("%llu\n", mul);
}
// fprintf(stderr, "Time: %llu ms\n", chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - bgn).count());
return 0;
}
if(n <= 15)
{
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
vector<int> vt;
int cttt = 0;
U([&](auto &&self, int u) -> void
{
vis[u] = true; vt.push_back(u);
for(int v : web[u])
{
cttt++;
if(!vis[v])
{
self(self, v);
}
}
})(i);
if(vt.size() == 1)
{
pc1++;
continue;
}
cur++;
sz[cur] = vt.size();
if(cttt == 2 * sz[cur] - 2)
{
pans[cur][0] = 1;
// for(unsigned j=1;j<=24;j++)
// {
// pans[cur][j] = j * qpow(j-1, sz[cur]-1) % 1000000007;
// }
continue;
}
for(int i=0;i<vt.size();i++)
{
gid[vt[i]] = i;
bk[i] = 0;
}
for(int x : vt)
{
for(int y : web[x])
{
bk[gid[x]] |= (1u << gid[y]);
}
}
for(unsigned msk=0;msk<(1<<vt.size());msk++)
{
bool fg = true;
for(int j=0;j<vt.size();j++)
{
if((msk & (1 << j)) && (msk & bk[j])) fg = false;
}
kv[msk] = (int)fg;
kg[msk] = 0;
}
kg[0] = 1;
for(unsigned j=1;j<=16;j++)
{
for(unsigned x=0;x<(1<<vt.size());x++)
{
tp[x] = 0;
for(unsigned y=x;y;y=(y-1)&x)
{
tp[x] = (tp[x] + kv[y] * kg[x^y]) % 1000000007;
}
}
for(unsigned x=0;x<(1<<vt.size());x++)
{
kg[x] = tp[x];
}
pans[cur][j] = kg[(1<<vt.size()) - 1];
}
}
}
else
#endif
{
for(unsigned y = 1; y <= 24; y++)
{
for(unsigned x = 1; x <= 24; x++)
{
pq[y][x] = qpow(x, y);
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
vector<int> vt;
vector<pair<int, int>> es;
int cttt = 0;
U([&](auto &&self, int u) -> void
{
vis[u] = true; vt.push_back(u);
for(int v : web[u])
{
cttt++;
if(u < v) es.push_back({u, v});
if(!vis[v])
{
self(self, v);
}
}
})(i);
if(vt.size() == 1)
{
pc1++;
continue;
}
cur++;
sz[cur] = vt.size();
// printf("cur = %d, sz = %d, cttt = %d\n", cur, sz[cur], cttt);
if(cttt == 2 * sz[cur] - 2)
{
pans[cur][0] = 1;
continue;
}
for(int i=0;i<vt.size();i++)
{
gid[vt[i]] = i;
}
for(auto &[x, y] : es)
{
x = gid[x];
y = gid[y];
}
for(int ms=0;ms<(1<<es.size());ms++)
{
for(int j=0;j<vt.size();j++)
{
ds.fa[j] = j;
ds.rk[j] = 0;
}
int cp = vt.size();
for(int j=es.size()-1;j>=0;j--)
{
if(ms&(1<<j))
{
int x = ds.getfa(es[j].first), y = ds.getfa(es[j].second);
if(ds.merge(x, y)) cp--;
}
}
unsigned long long kv = (__builtin_popcount(ms) & 1) ? 1000000006 : 1;
for(int j=1;j<=24;j++)
{
// if(pq[cp][j] && j == 2) __debugbreak();
pans[cur][j] = (pans[cur][j] + kv * pq[cp][j]) % 1000000007;
}
}
for(int j=24;j>=1;j--)
{
for(int k=1;k<j;k++)
{
pans[cur][j] = (pans[cur][j] + (j % 2 == k % 2 ? 1 : 1000000006) * comb(j, k) % 1000000007 * pans[cur][k]) % 1000000007;
}
}
// for(int j=1;j<=24;j++)
// {
// printf("pans[%d][%d] = %llu\n", cur, j, pans[cur][j]);
// }
}
// fprintf(stderr, "finished.\n");
fprintf(stderr, "Time: %lu ms\n", chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - bgn).count());
}
for(unsigned i=1;i<=n;i++) rv[i] = qpow(i, 1000000005);
while(q--)
{
int c;
// scanf("%d", &c);
c = qread();
unsigned long long mul = qpow(c, pc1);
for(int i=1;i<=cur;i++)
{
// if(sz[i] == 1) continue;
if(pans[i][0])
{
mul = mul * c % 1000000007 * qpow(c-1, sz[i] - 1) % 1000000007;
continue;
}
unsigned long long sum = 0;
unsigned long long pvz = 1;
for(int j=1;j<=sz[i] && j<=c;j++)
{
pvz = pvz * (c - j + 1) % 1000000007 * rv[j] % 1000000007;
// printf("pans[%d][%d] = %llu, comb(%d, %d) = %llu\n", i, j, pans[i][j], c, j, pvz);
sum += pans[i][j] * pvz % 1000000007;
}
mul = mul * (sum % 1000000007) % 1000000007;
}
printf("%llu\n", mul);
}
fprintf(stderr, "Total time: %lu ms\n", chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - bgn).count());
return 0;
}
// 来点私货:不会有哪个审核员要求我加入中文翻译吧。:)

题解:P5204 [USACO19JAN] Train Tracking 2 P

继续暴力过题,依旧均摊正确。


$O(n\log p)$ 做法,忽略快速幂之后 $O(n)$。

考虑如果对于某个 $i$ 有 $c_i<c_{i+1}$ 则说明 $a_i=c_i$。

考虑我们可以把区间整个倒过来,使得如果 $c$ 不全相等则必然有 $c_i<c_{i+1}$。

考虑确定一个 $a_i$ 之后怎么做。由于一定有解,所以可以不加检查地把区间分为两段,这就划分为了两个子问题。这样的正确性是显然的,读者自证不难。

如果 $c$ 全相等则可以直接 dp 求解,即任意一个长度为 $k$ 的区间内都有至少一个和 $c$ 相等的数字,$f_i$ 代表考虑 $1\sim i$ 且 $a_i=c$,前缀和优化做到 $O(n\log p)$(忽略快速幂则 $O(n)$)。

那么考虑分治,对于一个序列 $c$,首先正着扫描找到第一个 $c_i<c_{i+1}$ 的 $i$,如果有就分治;否则 $c$ 翻转再扫一遍;如果都没有则跑上面的 dp。注意一个细节,如果划分为的子问题中有长度 $<k$ 的,则不存在对应的 $c$,实际限制为 $\ge c_i$。

过了??

考虑时间复杂度。

考虑第一个 $c_i<c_{i+1}$ 的位置,其左边的 $c$ 单调不增。我们将会证明对于单调不增的 $c$,处理的时间复杂度为 $O(n\log p)$(忽略快速幂 $O(n)$)。

Proof

首先会消耗 $O(n)$ 的时间扫描并翻转。

然后对于每个 $c_i<c_{i+1}$,左边都是 $O(\text{len}\log p)$ dp 解决掉的;右边根本没有扫描到,只是递归了下去。这样每个元素最多被扫一遍,$O(n)$。

拓展上面的证明,左边是 $O(\text{len}_l)$,右边是 $O(\text{len}_r)$,总共 $O(\text{len})$。$\square$

顺带一提,如果分治的时候把 $c$ 复制过去会让时空复杂度退化,但是还是有 $95\mathrm{pts}$。

rec&code

rec。

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
/*
先按某种方式离散化,先认为值域是 O(n) 的。
*/
// bsgm 大样例怎么除了最后一个都不是从官方数据里选的,太邪恶了,还我分来
// 欸,USACO 题哪来的子任务
/*
考虑如果 c[i] < c[i+1] 就可以确定 a[i],如果 c[i] > c[i+1] 就可以确定 a[i+k].如果 c[i] = c[i+1] 则无法确定任何信息。

而如果确定了一个数,就可以……

感觉上是消掉这个数。但是怎么消啊。
*/
// sub1: brute force
// sub2: ???
// sub3: simple dp
// sub4: ??????
/*
统计一下,现在可以拿到 100+35+67=202 分,略高于 200.

如果能过掉 T2 或 T3 就好了。
*/
/*
可以直接砍成两段,分别求解吗。

如果是 c[i] < c[i+1]. 那么 c[i] 本身是一定被满足的(如果确定了 a[i] 并且 c[i+1] 满足)。但是对于左右两边的呢?

右边显然没有任何影响。

然后如果我们钦定 i 是第一个 c[i] < c[i+1] 的位置是不是就行了……喵?

不一定哦喵。

哦不对是一定的,因为必然有解。

那就……做完了?

就可以得到一个 O(n^2) 做法,先验一下。
*/
// 验完了,是对的。并且有一个比较牛的方法直接变成正解。可以有均摊。
#include <cstdio>
#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

const auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
unsigned long long dp[100005], dps[100005];
int c[100005];

int main()
{
int n, k;
scanf("%d%d", &n, &k);
vector<int> cc;
for(int i=1;i<=n-k+1;i++) scanf("%d", c + i);
auto qpow = [](unsigned long long x, unsigned long long y) -> unsigned long long { unsigned long long ans = 1; do { if(y & 1) ans = ans * x % 1000000007; x = x * x % 1000000007; } while(y >>= 1); return ans; };
auto solveae = [&](int len, int x)
{
if(x == 1000000000) return 1ull;
x = 1000000001 - x;
dp[0] = dps[0] = 1;
for(int i=1;i<=len;i++)
{
dp[i] = (dps[i-1] - (i>=k+1 ? dps[i-k-1] : 0) + 1000000007) * qpow(x-1, i-1) % 1000000007;
dps[i] = (dps[i-1] + dp[i] * qpow(x-1, 1000000005ll * i % 1000000006)) % 1000000007;
}
unsigned long long ps = 0;
for(int i=1;i<=len;i++)
{
if(i+k-1 >= len) ps = (ps + dp[i] * qpow(x-1, len-i)) % 1000000007;
}
return ps;
};
auto qsame = [](const vector<int> &x) { for(int i=0;i+1<x.size();i++) if(x[i] != x[i+1]) return false; return true; };
auto solve = U([&](auto &&self, int m, int l, int r, int g) -> unsigned long long
{
assert(r - l + 1 == m - k + 1);
if(l > r) return qpow(1000000000-g+1, m);
// if(qsame(c)) return solveae(m, c[0]);
auto splitter = [&](int p, int v) { return self(self, p-l, l, p-k, v) * self(self, m-(p-l)-1, p+1, r, v) % 1000000007; };
// auto rsplitter = [&](int p, int v) { return self(self, p-l, l, p-k, v) * self(self, m-(p-l)-1, p+1, r, v) % 1000000007; };
for(int i=l;i<r;i++)
{
if(c[i] < c[i+1]) return splitter(i, c[i]);
// if(c[i] > c[i+1]) return rsplitter(i+k, c[i+k]);
// if(c[r-i+l-1] < c[r-i+l]) return splitter(r-i+l-1, c[r-i+l-1]);
// if(c[r-i+l-1] > c[r-i+l]) return rsplitter(r-i+l-1+k, c[r-i+l-1+k]);
}
reverse(c + l, c + r + 1);
for(int i=l;i<r;i++)
{
if(c[i] < c[i+1]) return splitter(i, c[i]);
// if(c[i] > c[i+1]) return rsplitter(i+k, c[i+k]);
// if(c[r-i+l-1] < c[r-i+l]) return splitter(r-i+l-1, c[r-i+l-1]);
// if(c[r-i+l-1] > c[r-i+l]) return rsplitter(r-i+l-1+k, c[r-i+l-1+k]);
}
return solveae(m, c[l]);
#ifdef LB_IS_HERE
__builtin_unreachable();
#endif
});
printf("%llu\n", solve(n, 1, n-k+1, 1));
return 0;
}
// 验证完毕,挑战成功。
0%