Submission #32346


ソースコード

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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
using F = function< Monoid(Monoid, Monoid) >;
using G = function< Monoid(Monoid, OperatorMonoid, int) >;
using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
int sz;
vector< Monoid > data;
vector< OperatorMonoid > lazy;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazySegmentTree(int n, const F f, const G g, const H h,
const Monoid &M1, const OperatorMonoid OM0)
: f(f), g(g), h(h), M1(M1), OM0(OM0) {
sz = 1;
while(sz < n) sz <<= 1;
data.assign(2 * sz, M1);
lazy.assign(2 * sz, OM0);
}
void set(int k, const Monoid &x) {
data[k + sz] = x;
}
void build() {
for(int k = sz - 1; k > 0; k--) {
data[k] = f(data[2 * k + 0], data[2 * k + 1]);
}
}
void propagate(int k, int len) {
if(lazy[k] != OM0) {
if(k < sz) {
lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
}
data[k] = g(data[k], lazy[k], len);
lazy[k] = OM0;
}
}
Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) {
propagate(k, r - l);
if(r <= a || b <= l) {
return data[k];
} else if(a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
} else {
return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid update(int a, int b, const OperatorMonoid &x) {
return update(a, b, x, 1, 0, sz);
}
Monoid query(int a, int b, int k, int l, int r) {
propagate(k, r - l);
if(r <= a || b <= l) {
return M1;
} else if(a <= l && r <= b) {
return data[k];
} else {
return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
query(a, b, 2 * k + 1, (l + r) >> 1, r));
}
}
Monoid query(int a, int b) {
return query(a, b, 1, 0, sz);
}
Monoid operator[](const int &k) {
return query(k, k + 1);
}
};
template< typename G >
struct CentroidPathDecomposition {
struct Centroid {
int ParIndex, ParDepth, Deep;
vector< int > node;
Centroid(int idx, int dep, int deep) : ParIndex(idx), ParDepth(dep), Deep(deep) {}
inline size_t size() {
return (node.size());
}
inline int &operator[](int k) {
return (node[k]);
}
inline pair< int, int > Up() {
return (make_pair(ParIndex, ParDepth));
}
};
const G &graph;
vector< int > SubTreeSize, NextPath;
vector< int > TreeIndex, TreeDepth;
vector< Centroid > Centroids;
void BuildSubTreeSize() {
stack< pair< int, int > > s;
s.emplace(0, -1);
while(!s.empty()) {
auto p = s.top();
s.pop();
if(~SubTreeSize[p.first]) {
NextPath[p.first] = -1;
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
SubTreeSize[p.first] += SubTreeSize[to];
if(NextPath[p.first] == -1 || SubTreeSize[NextPath[p.first]] < SubTreeSize[to]) {
NextPath[p.first] = to;
}
}
} else {
s.push(p);
SubTreeSize[p.first] = 1;
for(auto &to : graph[p.first]) {
if(p.second != to) s.emplace(to, p.first);
}
}
}
}
void BuildPath() {
stack< pair< int, int > > s;
Centroids.emplace_back(-1, -1, 0);
s.emplace(0, -1);
TreeIndex[0] = 0;
while(!s.empty()) {
auto p = s.top();
s.pop();
TreeDepth[p.first] = (int) Centroids[TreeIndex[p.first]].size();
for(auto &to : graph[p.first]) {
if(p.second == to) continue;
if(to == NextPath[p.first]) { // Centroid-Path
TreeIndex[to] = TreeIndex[p.first];
} else { // Not Centroid-Path
TreeIndex[to] = (int) Centroids.size();
Centroids.emplace_back(TreeIndex[p.first], TreeDepth[p.first], Centroids[TreeIndex[p.first]].Deep + 1);
}
s.emplace(to, p.first);
}
Centroids[TreeIndex[p.first]].node.emplace_back(p.first);
}
}
virtual void Build() {
BuildSubTreeSize();
BuildPath();
}
inline size_t size() {
return (Centroids.size());
}
inline pair< int, int > Information(int idx) {
return (make_pair(TreeIndex[idx], TreeDepth[idx]));
}
inline Centroid &operator[](int k) {
return (Centroids[k]);
}
inline int LCA(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
return (Centroids[TreeIdxA][TreeDepthA]);
}
inline virtual void query(int a, int b, const function< void(int, int, int) > &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = Information(a);
tie(TreeIdxB, TreeDepthB) = Information(b);
while(TreeIdxA != TreeIdxB) {
if(Centroids[TreeIdxA].Deep > Centroids[TreeIdxB].Deep) {
f(TreeIdxA, 0, TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = Centroids[TreeIdxA].Up();
} else {
f(TreeIdxB, 0, TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(TreeIdxA, TreeDepthA, TreeDepthB + 1);
}
CentroidPathDecomposition(const G &g) : graph(g) {
int SZ = g.size();
SubTreeSize.assign(SZ, -1);
NextPath.resize(SZ);
TreeIndex.resize(SZ);
TreeDepth.resize(SZ);
}
};
template< typename G >
struct TreeArray : CentroidPathDecomposition< G > {
using CPD = CentroidPathDecomposition< G >;
TreeArray(const G &g) : CPD(g) {}
vector< int > index;
void Build() {
CPD::Build();
int ptr = 0;
for(auto ¢roid : CPD::Centroids) {
index.emplace_back(ptr);
ptr += centroid.size();
}
}
inline int get(int a) {
auto p = CPD::Information(a);
return (index[p.first] + p.second);
}
inline void query(int a, int b, const function< void(int, int) > &f) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
while(TreeIdxA != TreeIdxB) {
if(CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
f(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
f(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB);
f(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1);
}
inline vector< tuple< int, int, bool > > get_path(int a, int b) {
int TreeIdxA, TreeDepthA, TreeIdxB, TreeDepthB;
tie(TreeIdxA, TreeDepthA) = CPD::Information(a);
tie(TreeIdxB, TreeDepthB) = CPD::Information(b);
vector< tuple< int, int, bool > > vs, ws;
while(TreeIdxA != TreeIdxB) {
if(CPD::Centroids[TreeIdxA].Deep > CPD::Centroids[TreeIdxB].Deep) {
vs.emplace_back(index[TreeIdxA], index[TreeIdxA] + TreeDepthA + 1, true);
tie(TreeIdxA, TreeDepthA) = CPD::Centroids[TreeIdxA].Up();
} else {
ws.emplace_back(index[TreeIdxB], index[TreeIdxB] + TreeDepthB + 1, false);
tie(TreeIdxB, TreeDepthB) = CPD::Centroids[TreeIdxB].Up();
}
}
bool rev = true;
if(TreeDepthA > TreeDepthB) swap(TreeDepthA, TreeDepthB), rev = false;
vs.emplace_back(index[TreeIdxA] + TreeDepthA, index[TreeIdxA] + TreeDepthB + 1, !rev);
reverse(begin(ws), end(ws));
copy(begin(ws), end(ws), back_inserter(vs));
return (vs);
}
};
template< typename T >
struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;
const int INF = 1 << 29;
struct node {
int left, right, ans, len;
};
int main() {
int N, Q, S[200000];
scanf("%d %d", &N, &Q);
UnWeightedGraph g(N);
for(int i = 1; i < N; i++) {
int p;
scanf("%d", &p);
g[p].emplace_back(i);
g[i].emplace_back(p);
}
TreeArray< UnWeightedGraph > cpd(g);
cpd.Build();
node np = (node) {0, 0, 0, 0};
auto f = [](const node &x, const node &y) {
node z;
z.len = x.len + y.len;
z.left = x.left;
if(x.left == x.len) z.left += y.left;
z.right = y.right;
if(y.right == y.len) z.right += x.right;
z.ans = max(x.right + y.left, max(x.ans, y.ans));
z.ans = max(z.ans, max(z.left, z.right));
return z;
};
auto gg = [](const node &x, int y, int len) {
node z = x;
if(y == -1) {
z.ans = z.left = z.right = 0;
} else {
z.ans = z.left = z.right = len;
}
return z;
};
auto h = [&](int x, int y) {
return y;
};
LazySegmentTree< node, int > seg(N, f, gg, h, np, 0);
for(int i = 0; i < N; i++) {
seg.set(cpd.get(i), (node) {0, 0, 0, 1});
}
seg.build();
for(int i = 0; i < Q; i++) {
int t, x, y;
scanf("%d %d %d", &t, &x, &y);
if(t <= 1) {
cpd.query(x, y, [&](int a, int b) {
seg.update(a, b, t == 0 ? 1 : -1);
});
} else {
auto l = np, r = np;
for(auto &p : cpd.get_path(x, y)) {
int a, b;
bool c;
tie(a, b, c) = p;
if(c) r = f(seg.query(a, b), r);
else l = f(l, seg.query(a, b));
}
swap(l.left, l.right);
printf("%d\n", f(l, r).ans);
}
}
}

ステータス

項目 データ
問題 0571 - 木
ユーザー名 ei1333
投稿日時 2018-03-21 01:29:42
言語 C++17
状態 Compile Error
得点 0
ソースコード長 10776 Byte
最大実行時間 -
最大メモリ使用量

コンパイルメッセージ

./Main.cpp:237:14: error: stray ‘\302’ in program
     for(auto ¢roid : CPD::Centroids) {
              ^
./Main.cpp:237:15: error: stray ‘\242’ in program
     for(auto ¢roid : CPD::Centroids) {
               ^
./Main.cpp: In member function ‘void TreeArray<G>::Build()’:
./Main.cpp:239:14: error: ‘centroid’ was not declared in this scope
       ptr += centroid.size();
              ^~~~~~~~
./Main.cpp:239:14: note: suggested alternative: ‘cuserid’
       ptr += centroid.size();
              ^~~~~~~~
              cuserid

セット

セット 得点 Cases

テストケース

ファイル名 状態 実行時間 メモリ使用量 #