#include <bits/stdc++.h>
#include "werewolf.h"
using namespace std;
using pii = pair<int, int>;
struct dsu
{
vector<int> parent;
vector<bool> flag;
dsu(int n)
{
flag.resize(n);
for(int i = 0 ; i < n ; i++)
parent.push_back(i);
}
int Find (int nod)
{
if(parent[nod] == nod)
return nod;
return parent[nod] = Find(parent[nod]);
}
bool Union (int nod_a, int nod_b, bool sol)
{
int x = Find(nod_a),
y = Find(nod_b);
if(!flag[x] or !flag[y])
return false;
if(!sol)
parent[min(x, y)] = max(x, y);
if(sol)
parent[max(x, y)] = min(x, y);
return x != y;
}
void activate (int x)
{
flag[x] = true;
}
};
vector<int> t;
void update (int x, int xend, int nod, int pos, int val)
{
if(x == xend)
return void(t[nod] = val);
int mid = (x + xend) / 2;
(pos <= mid ? update(x, mid, nod*2, pos, val) : update(mid+1, xend, nod*2+1, pos, val));
t[nod] = max(t[nod*2], t[nod*2+1]);
}
int query (int x, int xend, int nod, int l, int r)
{
if(x > r or xend < l)
return 0;
if(l <= x and xend <= r)
return t[nod];
int mid = (x + xend) / 2;
return max(query(x, mid, nod*2, l, r), query(mid+1, xend, nod*2+1, l, r));
}
std::vector<int> check_validity(int n, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
int Q = L.size(), m = X.size();
vector<vector<int>> g(n);
for(int i = 0 ; i < m ; i++)
{
g[X[i]].push_back(Y[i]);
g[Y[i]].push_back(X[i]);
}
dsu dsuL(n), dsuR(n);
vector<vector<int>> gl(n), gr(n);
for(int i = 0 ; i < n ; i++)
{
int nod = i;
dsuL.activate(i);
sort(g[i].rbegin(), g[i].rend());
for(auto j: g[i])
{
int x = dsuL.Find(j);
if(dsuL.Union(nod, x, false))
{
gl[nod].push_back(x);
gl[x].push_back(nod);
nod = dsuL.Find(nod);
}
}
}
for(int i = n - 1 ; i >= 0 ; i--)
{
int nod = i;
dsuR.activate(i);
reverse(g[i].begin(), g[i].end());
for(auto j: g[i])
{
int x = dsuR.Find(j);
if(dsuR.Union(nod, x, true))
{
gr[nod].push_back(x);
gr[x].push_back(nod);
nod = dsuR.Find(nod);
}
}
}
int cnt = 0;
vector<int> et, levelL(n), levelR(n);
vector<pii> indxL(n), indxR(n);
vector<vector<int>> parentL(n, vector<int> (18)), parentR = parentL;
function<void(int, int)> dfs1 = [&](int nod, int father)
{
indxR[nod].first = et.size();
et.push_back(nod);
for(auto i: gl[nod])
{
if(i != father)
{
levelR[i] = levelR[nod]+1;
parentR[i][0] = nod;
dfs1(i, nod);
}
}
indxR[nod].second = et.size() - 1;
};
function<void(int, int)> dfs2 = [&](int nod, int father)
{
indxL[nod].first = cnt++;
// cout << nod << ' ' ;
for(auto i: gr[nod])
{
if(i != father)
{
levelL[i] = levelL[nod] + 1;
parentL[i][0] = nod;
dfs2(i, nod);
}
}
indxL[nod].second = cnt - 1;
};
// cout << endl ;
dfs1(n-1, -1);
// for(auto i: et)
// cout << i << ' ' ;
// cout << endl ;
dfs2(0, -1);
// cout << endl << endl ;
for(int i = 1 ; i < 18 ; i++)
{
for(int nod = 0 ; nod < n ; nod++)
{
if((1<<i) <= levelR[nod])
parentR[nod][i] = parentR[parentR[nod][i-1]][i-1];
if((1<<i) <= levelL[nod])
parentL[nod][i] = parentL[parentL[nod][i-1]][i-1];
}
}
vector<pair<pair<int, bool>, int>> v;
for(int i = 0 ; i < Q ; i++)
{
int nod = E[i];
for(int j = 17 ; j >= 0 ; j--)
{
if((1<<j) <= levelR[nod] and parentR[nod][j] <= R[i])
nod = parentR[nod][j];
}
// cout << E[i] << ' ' << R[i] << ' ' << nod << ' ' << indxR[nod].first << ' ' << indxR[nod].second << endl ;
v.push_back({{indxR[nod].first, false}, i});
v.push_back({{indxR[nod].second, true}, i});
}
// cout << endl ;
t.resize(6*n);
sort(v.rbegin(), v.rend());
cnt = 0;
vector<int> mm(Q), ans(Q);
// cout << endl ;
// for(int i = v.size() - 1 ; i >= 0 ; i--)
// cout << v[i].first.first << ' ' << v[i].first.second << ' ' << v[i].second << endl ;
// cout << endl ;
for(int i = 0 ; i < n ; i++)
{
while(v.size() and v.back().first.first == i and !v.back().first.second)
{
mm[v.back().second] = ++cnt;
v.pop_back();
}
// cout << "u " << et[i] << ' ' << indxL[et[i]].first << ' ' << cnt << endl ;
update(1, n, 1, indxL[et[i]].first+1, cnt);
while(v.size() and v.back().first.first == i and v.back().first.second)
{
int nod = S[v.back().second];
for(int j = 17 ; j >= 0 ; j--)
{
if((1<<j) <= levelL[nod] and parentL[nod][j] >= L[v.back().second])
nod = parentL[nod][j];
}
// cout << nod << ' ' << L[v.back().second] << ' ' << indxL[nod].first << ' ' << indxL[nod].second << ' ' << query(1, n, 1, indxL[nod].first, indxL[nod].second) << ' ' << mm[v.back().second] << endl ;
ans[v.back().second] = (query(1, n, 1, indxL[nod].first+1, indxL[nod].second+1) >= mm[v.back().second]);
v.pop_back();
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
9 ms |
1864 KB |
Output is correct |
11 |
Correct |
9 ms |
1868 KB |
Output is correct |
12 |
Correct |
8 ms |
1740 KB |
Output is correct |
13 |
Correct |
9 ms |
2124 KB |
Output is correct |
14 |
Correct |
8 ms |
1996 KB |
Output is correct |
15 |
Correct |
10 ms |
1940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1284 ms |
101476 KB |
Output is correct |
2 |
Correct |
1395 ms |
107364 KB |
Output is correct |
3 |
Correct |
1196 ms |
103516 KB |
Output is correct |
4 |
Correct |
1116 ms |
101844 KB |
Output is correct |
5 |
Correct |
1118 ms |
101800 KB |
Output is correct |
6 |
Correct |
1202 ms |
101736 KB |
Output is correct |
7 |
Correct |
1199 ms |
101404 KB |
Output is correct |
8 |
Correct |
1259 ms |
107172 KB |
Output is correct |
9 |
Correct |
932 ms |
103412 KB |
Output is correct |
10 |
Correct |
840 ms |
101796 KB |
Output is correct |
11 |
Correct |
861 ms |
101692 KB |
Output is correct |
12 |
Correct |
953 ms |
101584 KB |
Output is correct |
13 |
Correct |
1483 ms |
110968 KB |
Output is correct |
14 |
Correct |
1396 ms |
110956 KB |
Output is correct |
15 |
Correct |
1406 ms |
110884 KB |
Output is correct |
16 |
Correct |
1459 ms |
110936 KB |
Output is correct |
17 |
Correct |
1197 ms |
101460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
9 ms |
1864 KB |
Output is correct |
11 |
Correct |
9 ms |
1868 KB |
Output is correct |
12 |
Correct |
8 ms |
1740 KB |
Output is correct |
13 |
Correct |
9 ms |
2124 KB |
Output is correct |
14 |
Correct |
8 ms |
1996 KB |
Output is correct |
15 |
Correct |
10 ms |
1940 KB |
Output is correct |
16 |
Correct |
1284 ms |
101476 KB |
Output is correct |
17 |
Correct |
1395 ms |
107364 KB |
Output is correct |
18 |
Correct |
1196 ms |
103516 KB |
Output is correct |
19 |
Correct |
1116 ms |
101844 KB |
Output is correct |
20 |
Correct |
1118 ms |
101800 KB |
Output is correct |
21 |
Correct |
1202 ms |
101736 KB |
Output is correct |
22 |
Correct |
1199 ms |
101404 KB |
Output is correct |
23 |
Correct |
1259 ms |
107172 KB |
Output is correct |
24 |
Correct |
932 ms |
103412 KB |
Output is correct |
25 |
Correct |
840 ms |
101796 KB |
Output is correct |
26 |
Correct |
861 ms |
101692 KB |
Output is correct |
27 |
Correct |
953 ms |
101584 KB |
Output is correct |
28 |
Correct |
1483 ms |
110968 KB |
Output is correct |
29 |
Correct |
1396 ms |
110956 KB |
Output is correct |
30 |
Correct |
1406 ms |
110884 KB |
Output is correct |
31 |
Correct |
1459 ms |
110936 KB |
Output is correct |
32 |
Correct |
1197 ms |
101460 KB |
Output is correct |
33 |
Correct |
1439 ms |
103492 KB |
Output is correct |
34 |
Correct |
463 ms |
26016 KB |
Output is correct |
35 |
Correct |
1611 ms |
107676 KB |
Output is correct |
36 |
Correct |
1312 ms |
102560 KB |
Output is correct |
37 |
Correct |
1487 ms |
106516 KB |
Output is correct |
38 |
Correct |
1363 ms |
103488 KB |
Output is correct |
39 |
Correct |
1291 ms |
117652 KB |
Output is correct |
40 |
Correct |
1474 ms |
109988 KB |
Output is correct |
41 |
Correct |
1217 ms |
105788 KB |
Output is correct |
42 |
Correct |
1043 ms |
102560 KB |
Output is correct |
43 |
Correct |
1593 ms |
114100 KB |
Output is correct |
44 |
Correct |
1322 ms |
108840 KB |
Output is correct |
45 |
Correct |
1089 ms |
119904 KB |
Output is correct |
46 |
Correct |
1074 ms |
119608 KB |
Output is correct |
47 |
Correct |
1363 ms |
113208 KB |
Output is correct |
48 |
Correct |
1355 ms |
113012 KB |
Output is correct |
49 |
Correct |
1344 ms |
112900 KB |
Output is correct |
50 |
Correct |
1346 ms |
112972 KB |
Output is correct |
51 |
Correct |
1384 ms |
112380 KB |
Output is correct |
52 |
Correct |
1384 ms |
112264 KB |
Output is correct |