#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using vi = vector<int>;
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb push_back
#define endl '\n'
#define fst first
#define scn second
const int N = 2e5;
const int K = 18;
vi adj[N];
struct dsu{
int s[N];
int in[N];
vector<array<int,K>> p;
vector<vi> dch;
int n;
dsu(int _n) {
n = _n;
p.resize(n);
dch.resize(n);
for (int i = 0; i < n; ++i) {
p[i][0] = i;
p[i][1] = i;
s[i] = 1;
}
}
int
getp(int x)
{
return p[x][1] = (x == p[x][1]) ? x : getp(p[x][1]) ;
}
void
conc(int a, int b)
{
b = getp(b);
if (a == b) return;
dch[a].pb(b);
p[b][0] = a;
p[b][1] = a;
s[a] += s[b];
return;
}
int
dfs(int x, int cnt)
{
in[x] = cnt++;
for(int u : dch[x])
cnt = dfs(u, cnt);
return cnt;
}
void
gen()
{
dfs(n-1,0);
for (int j = 1; j < K; ++j) {
for (int i = 0; i < n; ++i) {
p[i][j] = p[p[i][j-1]][j-1];
}
}
return;
}
void
query(int& x1, int& x2, int st, int v)
{
++v;
for (int j = K-1; j >= 0; --j) {
if (p[st][j] < v) st = p[st][j];
}
x1 = in[st];
x2 = x1 + s[st]-1;
return;
}
};
struct aib{
vi tr;
int n;
aib(int _n) {
n = _n;
tr.assign(n,0);
}
void
add(int x, int v)
{
for(; x < n; x |= (x+1))
tr[x] += v;
return;
}
int
query(int x)
{
int res = 0;
for (; x >= 0; x = (x&(x+1))-1)
res += tr[x];
return res;
}
int
query(int l, int r)
{
return query(r) - query(l-1);
}
};
vector<int>
check_validity(int n, vector<int> x, vector<int> y,
vector<int> b, vector<int> e, vector<int> l, vector<int> r) {
for (int i = 0; i < sz(x); ++i) {
adj[x[i]].pb(y[i]);
adj[y[i]].pb(x[i]);
}
dsu up(n), dn(n);
for (int i = 0; i < n; ++i) {
for (int u : adj[i])
if (u < i) up.conc(i, u);
}
for (int i = n-1; i >= 0; --i) {
for (int u : adj[i])
if (u > i) dn.conc(n-1-i, n-1-u);
}
up.gen();
dn.gen();
int q = sz(l);
vi ans(q,0);
deque<pair<int,int>> in, out;
vi y1(q) ,y2(q), pos(n);
for (int i = 0; i < q; ++i) {
int x1, x2, x3, x4;
up.query(x1,x2, e[i], r[i]);
dn.query(x3,x4, n-1-b[i], n-1-l[i]);
in.pb({x1,i});
out.pb({x2,i});
y1[i] = x3;
y2[i] = x4;
}
sort(in.rbegin(), in.rend());
sort(out.rbegin(), out.rend());
for (int i = 0; i < n; ++i) pos[up.in[i]] = n-i-1;
int pnt;
aib tr(n);
for (int i = 0; i < n; ++i) {
while(sz(in)) {
if (i < in.back().fst) break;
pnt = in.back().scn;
in.pop_back();
ans[pnt] -= tr.query(y1[pnt], y2[pnt]);
}
tr.add(dn.in[pos[i]],1);
while(sz(out)) {
if (i < out.back().fst) break;
pnt = out.back().scn;
out.pop_back();
ans[pnt] += tr.query(y1[pnt], y2[pnt]);
}
}
for (int i = 0; i < q; ++i)
if (ans[i]) ans[i] = 1;
return ans;
}
#ifdef ONPC
void
solve()
{
int n, m, q;
cin >> n >> m >> q;
vector<int> x(m), y(m), s(q), e(q), l(q), r(q);
for (int i = 0; i < m; ++i) cin >> x[i];
for (int i = 0; i < m; ++i) cin >> y[i];
for (int i = 0; i < q; ++i) cin >> s[i];
for (int i = 0; i < q; ++i) cin >> e[i];
for (int i = 0; i < q; ++i) cin >> l[i];
for (int i = 0; i < q; ++i) cin >> r[i];
vector<int> ans = check_validity(n,x,y,s,e,l,r);
for (int a : ans) {
cout << a << ' ';
}
cout << endl;
}
int32_t
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
freopen("in", "r", stdin);
int t = 1;
/* cin >> t; */
while(t--)
solve();
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8068 KB |
Output is correct |
4 |
Correct |
5 ms |
8020 KB |
Output is correct |
5 |
Correct |
6 ms |
8164 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8068 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8068 KB |
Output is correct |
4 |
Correct |
5 ms |
8020 KB |
Output is correct |
5 |
Correct |
6 ms |
8164 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8068 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
9 ms |
9172 KB |
Output is correct |
11 |
Correct |
9 ms |
9212 KB |
Output is correct |
12 |
Correct |
9 ms |
9172 KB |
Output is correct |
13 |
Correct |
9 ms |
9244 KB |
Output is correct |
14 |
Correct |
8 ms |
9264 KB |
Output is correct |
15 |
Correct |
10 ms |
9300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
603 ms |
77284 KB |
Output is correct |
2 |
Correct |
585 ms |
78692 KB |
Output is correct |
3 |
Correct |
527 ms |
77668 KB |
Output is correct |
4 |
Correct |
574 ms |
77392 KB |
Output is correct |
5 |
Correct |
561 ms |
77156 KB |
Output is correct |
6 |
Correct |
542 ms |
77212 KB |
Output is correct |
7 |
Correct |
559 ms |
77132 KB |
Output is correct |
8 |
Correct |
575 ms |
78656 KB |
Output is correct |
9 |
Correct |
491 ms |
77672 KB |
Output is correct |
10 |
Correct |
551 ms |
77296 KB |
Output is correct |
11 |
Correct |
524 ms |
77388 KB |
Output is correct |
12 |
Correct |
536 ms |
77500 KB |
Output is correct |
13 |
Correct |
691 ms |
83472 KB |
Output is correct |
14 |
Correct |
688 ms |
83452 KB |
Output is correct |
15 |
Correct |
727 ms |
83500 KB |
Output is correct |
16 |
Correct |
684 ms |
83536 KB |
Output is correct |
17 |
Correct |
657 ms |
77468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8068 KB |
Output is correct |
4 |
Correct |
5 ms |
8020 KB |
Output is correct |
5 |
Correct |
6 ms |
8164 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8068 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
9 ms |
9172 KB |
Output is correct |
11 |
Correct |
9 ms |
9212 KB |
Output is correct |
12 |
Correct |
9 ms |
9172 KB |
Output is correct |
13 |
Correct |
9 ms |
9244 KB |
Output is correct |
14 |
Correct |
8 ms |
9264 KB |
Output is correct |
15 |
Correct |
10 ms |
9300 KB |
Output is correct |
16 |
Correct |
603 ms |
77284 KB |
Output is correct |
17 |
Correct |
585 ms |
78692 KB |
Output is correct |
18 |
Correct |
527 ms |
77668 KB |
Output is correct |
19 |
Correct |
574 ms |
77392 KB |
Output is correct |
20 |
Correct |
561 ms |
77156 KB |
Output is correct |
21 |
Correct |
542 ms |
77212 KB |
Output is correct |
22 |
Correct |
559 ms |
77132 KB |
Output is correct |
23 |
Correct |
575 ms |
78656 KB |
Output is correct |
24 |
Correct |
491 ms |
77672 KB |
Output is correct |
25 |
Correct |
551 ms |
77296 KB |
Output is correct |
26 |
Correct |
524 ms |
77388 KB |
Output is correct |
27 |
Correct |
536 ms |
77500 KB |
Output is correct |
28 |
Correct |
691 ms |
83472 KB |
Output is correct |
29 |
Correct |
688 ms |
83452 KB |
Output is correct |
30 |
Correct |
727 ms |
83500 KB |
Output is correct |
31 |
Correct |
684 ms |
83536 KB |
Output is correct |
32 |
Correct |
657 ms |
77468 KB |
Output is correct |
33 |
Correct |
764 ms |
77444 KB |
Output is correct |
34 |
Correct |
332 ms |
32820 KB |
Output is correct |
35 |
Correct |
835 ms |
78640 KB |
Output is correct |
36 |
Correct |
718 ms |
77656 KB |
Output is correct |
37 |
Correct |
737 ms |
78104 KB |
Output is correct |
38 |
Correct |
736 ms |
77944 KB |
Output is correct |
39 |
Correct |
690 ms |
82636 KB |
Output is correct |
40 |
Correct |
852 ms |
83820 KB |
Output is correct |
41 |
Correct |
638 ms |
77964 KB |
Output is correct |
42 |
Correct |
559 ms |
77584 KB |
Output is correct |
43 |
Correct |
794 ms |
81656 KB |
Output is correct |
44 |
Correct |
615 ms |
78180 KB |
Output is correct |
45 |
Correct |
564 ms |
82764 KB |
Output is correct |
46 |
Correct |
627 ms |
82764 KB |
Output is correct |
47 |
Correct |
614 ms |
83516 KB |
Output is correct |
48 |
Correct |
598 ms |
83532 KB |
Output is correct |
49 |
Correct |
640 ms |
83500 KB |
Output is correct |
50 |
Correct |
634 ms |
83628 KB |
Output is correct |
51 |
Correct |
773 ms |
83644 KB |
Output is correct |
52 |
Correct |
778 ms |
83656 KB |
Output is correct |