#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
struct fen
{
int n;
vector<int> table;
fen(int in) : n(in), table(in + 1) {}
fen() {}
int query(int r)
{
int s = 0;
for (; r; r -= r & (-r)) s += table[r];
return s;
}
void update(int k, int v)
{
k++;
for (; k <= n; k += k & (-k)) table[k] += v;
}
};
struct tree
{
vector<vector<pair<int,int>>> adj;
vector<int> lvl;
vector<int> ssz;
vector<int> qans;
vector<int> qvs;
vector<int> qts;
vector<vector<int>> q1s;
vector<vector<int>> q2s;
int subdfs(const int& curr, const int& par, const int& l)
{
if (lvl[curr] <= l) return 0;
ssz[curr] = 1;
for (auto np : adj[curr])
{
if (np.first == par) continue;
ssz[curr] += subdfs(np.first, curr, l);
}
return ssz[curr];
}
void find(const int& curr, const int& par, const int& l, const int& sz)
{
if (lvl[curr] <= l) return;
for (auto np : adj[curr])
{
if (np.first == par) continue;
if (lvl[np.first] > l && ssz[np.first] > sz / 2) return find(np.first, curr, l, sz);
}
lvl[curr] = l + 1;
recurse(curr, l + 1);
}
vector<int> pis;
fen f;
void collect(const int& curr, const int& mw, const int& l)
{
if (lvl[curr] <= l) return;
pis.push_back(mw);
for (auto np : adj[curr])
{
if (np.second <= mw) continue;
collect(np.first, np.second, l);
}
}
vector<int> lproc;
vector<int> arrw;
void add(const int& curr, const int& mw, const int& l, const int& rt)
{
if (lvl[curr] <= l) return;
int ind = lower_bound(pis.begin(), pis.end(), mw) - pis.begin();
f.update(ind, 1);
lproc[curr] = rt;
arrw[curr] = mw;
for (auto np : adj[curr])
{
if (np.second <= mw) continue;
add(np.first, np.second, l, rt);
}
}
void process(const int& curr, const int& mw, const int& l, const int& rt)
{
if (lvl[curr] <= l) return;
for (auto i : q1s[curr])
{
int v = qts[i];
if (lproc[v] == rt) qans[i] = arrw[v];
}
for (auto i : q2s[curr])
{
int mind = lower_bound(pis.begin(), pis.end(), qvs[i]) - pis.begin();
qans[i] += f.query(mind);
if (qvs[i] > arrw[rt]) qans[i]++;
}
for (auto np : adj[curr])
{
if (np.second >= mw) continue;
process(np.first, np.second, l, rt);
}
}
void recurse(const int& curr, const int& l)
{
sort(adj[curr].begin(), adj[curr].end(), [&](const pair<int,int>& lhs, const pair<int,int>& rhs)
{
return lhs.second > rhs.second;
});
pis.clear();
for (auto np : adj[curr]) collect(np.first, np.second, l);
sort(pis.begin(), pis.end());
pis.erase(unique(pis.begin(), pis.end()), pis.end());
int m = pis.size();
f = fen(m);
lproc[curr] = curr;
for (auto np : adj[curr])
{
arrw[curr] = np.second;
process(np.first, np.second, l, curr);
add(np.first, np.second, l, curr);
}
arrw[curr] = -100;
process(curr, -100, l - 1, curr);
for (auto np : adj[curr])
{
subdfs(np.first, curr, l);
find(np.first, curr, l, ssz[np.first]);
}
}
tree(int in, int ik) : adj(in), lvl(in, in + 1), ssz(in, 0), qans(ik), qvs(ik), qts(ik, -1), q1s(in), q2s(in), lproc(in, in), arrw(in, 1e9) {}
};
signed main()
{
int n, k;
cin >> n >> k;
tree t(n, k);
int ct = 0;
for (int i = 0; i < n + k - 1; i++)
{
char m;
cin >> m;
if (m == 'S')
{
int a, b;
cin >> a >> b;
a--; b--;
t.adj[a].emplace_back(b, ct);
t.adj[b].emplace_back(a, ct);
ct++;
}
if (m == 'Q')
{
int a, tg;
cin >> tg >> a;
a--; tg--;
t.q1s[a].push_back(i - ct);
t.qts[i - ct] = tg;
t.qvs[i - ct] = ct;
t.qans[i - ct] = n + 100;
}
if (m == 'C')
{
int a;
cin >> a; a--;
t.q2s[a].push_back(i - ct);
t.qvs[i - ct] = ct;
}
}
t.subdfs(0, -1, -1);
t.find(0, -1, -1, t.ssz[0]);
for (int i = 0; i < k; i++)
{
if (t.qts[i] == -1)
{
cout << t.qans[i] << "\n";
}
else
{
if (t.qans[i] < t.qvs[i])
{
cout << "yes\n";
}
else
{
cout << "no\n";
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5060 KB |
Output is correct |
2 |
Correct |
92 ms |
5848 KB |
Output is correct |
3 |
Correct |
93 ms |
6940 KB |
Output is correct |
4 |
Correct |
97 ms |
7200 KB |
Output is correct |
5 |
Correct |
103 ms |
7576 KB |
Output is correct |
6 |
Correct |
94 ms |
7208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5060 KB |
Output is correct |
2 |
Correct |
92 ms |
5848 KB |
Output is correct |
3 |
Correct |
93 ms |
6940 KB |
Output is correct |
4 |
Correct |
97 ms |
7200 KB |
Output is correct |
5 |
Correct |
103 ms |
7576 KB |
Output is correct |
6 |
Correct |
94 ms |
7208 KB |
Output is correct |
7 |
Correct |
64 ms |
5752 KB |
Output is correct |
8 |
Correct |
90 ms |
6864 KB |
Output is correct |
9 |
Correct |
82 ms |
6456 KB |
Output is correct |
10 |
Correct |
88 ms |
6956 KB |
Output is correct |
11 |
Correct |
85 ms |
7360 KB |
Output is correct |
12 |
Correct |
88 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5100 KB |
Output is correct |
2 |
Correct |
312 ms |
25716 KB |
Output is correct |
3 |
Correct |
326 ms |
25632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5100 KB |
Output is correct |
2 |
Correct |
312 ms |
25716 KB |
Output is correct |
3 |
Correct |
326 ms |
25632 KB |
Output is correct |
4 |
Correct |
65 ms |
5060 KB |
Output is correct |
5 |
Correct |
296 ms |
28148 KB |
Output is correct |
6 |
Correct |
213 ms |
26164 KB |
Output is correct |
7 |
Correct |
241 ms |
26472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5040 KB |
Output is correct |
2 |
Correct |
447 ms |
37872 KB |
Output is correct |
3 |
Correct |
419 ms |
37844 KB |
Output is correct |
4 |
Correct |
511 ms |
37928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5040 KB |
Output is correct |
2 |
Correct |
447 ms |
37872 KB |
Output is correct |
3 |
Correct |
419 ms |
37844 KB |
Output is correct |
4 |
Correct |
511 ms |
37928 KB |
Output is correct |
5 |
Correct |
80 ms |
4984 KB |
Output is correct |
6 |
Correct |
430 ms |
40964 KB |
Output is correct |
7 |
Correct |
543 ms |
41008 KB |
Output is correct |
8 |
Correct |
473 ms |
40376 KB |
Output is correct |
9 |
Correct |
448 ms |
40372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
5112 KB |
Output is correct |
2 |
Correct |
471 ms |
26208 KB |
Output is correct |
3 |
Correct |
383 ms |
25340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
5112 KB |
Output is correct |
2 |
Correct |
471 ms |
26208 KB |
Output is correct |
3 |
Correct |
383 ms |
25340 KB |
Output is correct |
4 |
Correct |
64 ms |
4944 KB |
Output is correct |
5 |
Correct |
450 ms |
29388 KB |
Output is correct |
6 |
Correct |
385 ms |
28364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5064 KB |
Output is correct |
2 |
Correct |
432 ms |
38040 KB |
Output is correct |
3 |
Correct |
436 ms |
38080 KB |
Output is correct |
4 |
Correct |
483 ms |
38056 KB |
Output is correct |
5 |
Correct |
64 ms |
5188 KB |
Output is correct |
6 |
Correct |
443 ms |
26288 KB |
Output is correct |
7 |
Correct |
346 ms |
25424 KB |
Output is correct |
8 |
Correct |
382 ms |
26208 KB |
Output is correct |
9 |
Correct |
400 ms |
26072 KB |
Output is correct |
10 |
Correct |
570 ms |
31396 KB |
Output is correct |
11 |
Correct |
541 ms |
30060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5064 KB |
Output is correct |
2 |
Correct |
432 ms |
38040 KB |
Output is correct |
3 |
Correct |
436 ms |
38080 KB |
Output is correct |
4 |
Correct |
483 ms |
38056 KB |
Output is correct |
5 |
Correct |
64 ms |
5188 KB |
Output is correct |
6 |
Correct |
443 ms |
26288 KB |
Output is correct |
7 |
Correct |
346 ms |
25424 KB |
Output is correct |
8 |
Correct |
382 ms |
26208 KB |
Output is correct |
9 |
Correct |
400 ms |
26072 KB |
Output is correct |
10 |
Correct |
570 ms |
31396 KB |
Output is correct |
11 |
Correct |
541 ms |
30060 KB |
Output is correct |
12 |
Correct |
65 ms |
5080 KB |
Output is correct |
13 |
Correct |
406 ms |
40964 KB |
Output is correct |
14 |
Correct |
496 ms |
41176 KB |
Output is correct |
15 |
Correct |
391 ms |
40456 KB |
Output is correct |
16 |
Correct |
399 ms |
40416 KB |
Output is correct |
17 |
Correct |
64 ms |
5828 KB |
Output is correct |
18 |
Correct |
430 ms |
29376 KB |
Output is correct |
19 |
Correct |
358 ms |
28412 KB |
Output is correct |
20 |
Correct |
402 ms |
29012 KB |
Output is correct |
21 |
Correct |
417 ms |
29252 KB |
Output is correct |
22 |
Correct |
628 ms |
32904 KB |
Output is correct |
23 |
Correct |
723 ms |
34740 KB |
Output is correct |
24 |
Correct |
677 ms |
33860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
5088 KB |
Output is correct |
2 |
Correct |
90 ms |
5828 KB |
Output is correct |
3 |
Correct |
89 ms |
6880 KB |
Output is correct |
4 |
Correct |
94 ms |
7236 KB |
Output is correct |
5 |
Correct |
92 ms |
7548 KB |
Output is correct |
6 |
Correct |
91 ms |
7204 KB |
Output is correct |
7 |
Correct |
65 ms |
5956 KB |
Output is correct |
8 |
Correct |
309 ms |
28396 KB |
Output is correct |
9 |
Correct |
300 ms |
28344 KB |
Output is correct |
10 |
Correct |
67 ms |
5896 KB |
Output is correct |
11 |
Correct |
405 ms |
40900 KB |
Output is correct |
12 |
Correct |
414 ms |
40872 KB |
Output is correct |
13 |
Correct |
462 ms |
40696 KB |
Output is correct |
14 |
Correct |
63 ms |
5956 KB |
Output is correct |
15 |
Correct |
408 ms |
29100 KB |
Output is correct |
16 |
Correct |
359 ms |
28192 KB |
Output is correct |
17 |
Correct |
444 ms |
28960 KB |
Output is correct |
18 |
Correct |
450 ms |
29016 KB |
Output is correct |
19 |
Correct |
615 ms |
34320 KB |
Output is correct |
20 |
Correct |
609 ms |
32964 KB |
Output is correct |
21 |
Correct |
386 ms |
28300 KB |
Output is correct |
22 |
Correct |
359 ms |
28024 KB |
Output is correct |
23 |
Correct |
389 ms |
27880 KB |
Output is correct |
24 |
Correct |
385 ms |
27896 KB |
Output is correct |
25 |
Correct |
487 ms |
37304 KB |
Output is correct |
26 |
Correct |
448 ms |
27076 KB |
Output is correct |
27 |
Correct |
425 ms |
26944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
5088 KB |
Output is correct |
2 |
Correct |
90 ms |
5828 KB |
Output is correct |
3 |
Correct |
89 ms |
6880 KB |
Output is correct |
4 |
Correct |
94 ms |
7236 KB |
Output is correct |
5 |
Correct |
92 ms |
7548 KB |
Output is correct |
6 |
Correct |
91 ms |
7204 KB |
Output is correct |
7 |
Correct |
65 ms |
5956 KB |
Output is correct |
8 |
Correct |
309 ms |
28396 KB |
Output is correct |
9 |
Correct |
300 ms |
28344 KB |
Output is correct |
10 |
Correct |
67 ms |
5896 KB |
Output is correct |
11 |
Correct |
405 ms |
40900 KB |
Output is correct |
12 |
Correct |
414 ms |
40872 KB |
Output is correct |
13 |
Correct |
462 ms |
40696 KB |
Output is correct |
14 |
Correct |
63 ms |
5956 KB |
Output is correct |
15 |
Correct |
408 ms |
29100 KB |
Output is correct |
16 |
Correct |
359 ms |
28192 KB |
Output is correct |
17 |
Correct |
444 ms |
28960 KB |
Output is correct |
18 |
Correct |
450 ms |
29016 KB |
Output is correct |
19 |
Correct |
615 ms |
34320 KB |
Output is correct |
20 |
Correct |
609 ms |
32964 KB |
Output is correct |
21 |
Correct |
386 ms |
28300 KB |
Output is correct |
22 |
Correct |
359 ms |
28024 KB |
Output is correct |
23 |
Correct |
389 ms |
27880 KB |
Output is correct |
24 |
Correct |
385 ms |
27896 KB |
Output is correct |
25 |
Correct |
487 ms |
37304 KB |
Output is correct |
26 |
Correct |
448 ms |
27076 KB |
Output is correct |
27 |
Correct |
425 ms |
26944 KB |
Output is correct |
28 |
Correct |
66 ms |
5752 KB |
Output is correct |
29 |
Correct |
87 ms |
6896 KB |
Output is correct |
30 |
Correct |
83 ms |
6640 KB |
Output is correct |
31 |
Correct |
91 ms |
6960 KB |
Output is correct |
32 |
Correct |
84 ms |
7208 KB |
Output is correct |
33 |
Correct |
85 ms |
6852 KB |
Output is correct |
34 |
Correct |
64 ms |
5736 KB |
Output is correct |
35 |
Correct |
304 ms |
28228 KB |
Output is correct |
36 |
Correct |
221 ms |
26292 KB |
Output is correct |
37 |
Correct |
226 ms |
26408 KB |
Output is correct |
38 |
Correct |
63 ms |
5832 KB |
Output is correct |
39 |
Correct |
424 ms |
41056 KB |
Output is correct |
40 |
Correct |
505 ms |
41148 KB |
Output is correct |
41 |
Correct |
432 ms |
40336 KB |
Output is correct |
42 |
Correct |
397 ms |
40456 KB |
Output is correct |
43 |
Correct |
64 ms |
5752 KB |
Output is correct |
44 |
Correct |
433 ms |
29420 KB |
Output is correct |
45 |
Correct |
359 ms |
28348 KB |
Output is correct |
46 |
Correct |
398 ms |
28976 KB |
Output is correct |
47 |
Correct |
427 ms |
29288 KB |
Output is correct |
48 |
Correct |
650 ms |
32964 KB |
Output is correct |
49 |
Correct |
753 ms |
34720 KB |
Output is correct |
50 |
Correct |
698 ms |
33948 KB |
Output is correct |
51 |
Correct |
305 ms |
26960 KB |
Output is correct |
52 |
Correct |
256 ms |
27284 KB |
Output is correct |
53 |
Correct |
241 ms |
26932 KB |
Output is correct |
54 |
Correct |
256 ms |
27556 KB |
Output is correct |
55 |
Correct |
261 ms |
26144 KB |
Output is correct |
56 |
Correct |
358 ms |
27020 KB |
Output is correct |
57 |
Correct |
394 ms |
35776 KB |
Output is correct |
58 |
Correct |
488 ms |
27388 KB |
Output is correct |
59 |
Correct |
429 ms |
27076 KB |
Output is correct |