#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
#include "werewolf.h"
struct Dsu {
vector <int> p, cp;
int n;
int get(int u) {
return (u == cp[u] ? u : cp[u] = get(cp[u]));
}
void join(int u, int v) {
u = get(u);
v = get(v);
if (u != v) {
int x = n++;
p.push_back(x);
cp.push_back(x);
p[u] = p[v] = cp[u] = cp[v] = x;
}
}
Dsu(int nn) {
n = nn;
p.resize(n), cp.resize(n);
for (int i = 0; i < n; ++i) {
p[i] = i;
cp[i] = i;
}
}
};
struct Fenwick {
vector <int> f;
int n;
Fenwick(int nn) {
n = nn;
f.resize(n);
}
void modify(int i, int x) {
for (; i < n; i |= i + 1) {
f[i] += x;
}
}
int get(int r) {
int res = 0;
for (; r >= 0; r &= r + 1, --r) {
res += f[r];
}
return res;
}
int sum(int l, int r) {
return get(r) - get(l - 1);
}
};
struct Event {
int x, l, r, tp, id;
Event(int x, int l, int r, int tp, int id) : x(x), l(l), r(r), tp(tp), id(id) {}
bool operator <(const Event &other) const {
if (x != other.x) return x < other.x;
return id < other.id;
}
};
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) {
vector <vector <int>> g(n);
for (int i = 0; i < (int)x.size(); ++i) {
g[x[i]].push_back(y[i]);
g[y[i]].push_back(x[i]);
}
int q = s.size();
vector <vector <int>> who_l(n), who_r(n);
for (int i = 0; i < q; ++i) {
who_l[l[i]].push_back(i);
who_r[r[i]].push_back(i);
}
vector <int> where_st(q), where_en(q);
Dsu st(n), en(n);
for (int i = n - 1; i >= 0; --i) {
for (auto v : g[i]) {
if (v >= i) {
st.join(i, v);
}
}
for (auto id : who_l[i]) {
where_st[id] = st.get(s[id]);
}
}
for (int i = 0; i < n; ++i) {
for (auto v : g[i]) {
if (v <= i) {
en.join(i, v);
}
}
for (auto id : who_r[i]) {
where_en[id] = en.get(e[id]);
}
}
vector <vector <int>> g_st(st.n), g_en(en.n);
for (int i = 0; i < st.n; ++i) {
int p = st.p[i];
if (i != p) {
g_st[p].push_back(i);
g_st[i].push_back(p);
}
}
for (int i = 0; i < en.n; ++i) {
int p = en.p[i];
if (i != p) {
g_en[p].push_back(i);
g_en[i].push_back(p);
}
}
function <void(int, int, vector <vector <int>> &, vector <int> &, vector <int> &, vector <int>&)> dfs = [&] (int u, int p, vector <vector <int>> &g, vector <int> &l, vector <int> &r, vector <int> &e) {
e.push_back(u);
l[u] = (int)e.size() - 1;
for (auto v : g[u]) {
if (v != p) {
dfs(v, u, g, l, r, e);
}
}
r[u] = (int)e.size() - 1;
};
vector <int> l_st(st.n), r_st(st.n), e_st;
dfs(st.n - 1, -1, g_st, l_st, r_st, e_st);
vector <int> l_en(en.n), r_en(en.n), e_en;
dfs(en.n - 1, -1, g_en, l_en, r_en, e_en);
vector <Event> events;
for (int i = 0; i < q; ++i) {
events.emplace_back(l_st[where_st[i]] - 1, l_en[where_en[i]], r_en[where_en[i]], -1, i);
events.emplace_back(r_st[where_st[i]], l_en[where_en[i]], r_en[where_en[i]], 1, i);
}
for (int i = 0; i < n; ++i) {
events.emplace_back(l_st[i], l_en[i], -1, -1, -1);
}
sort(events.begin(), events.end());
Fenwick F(en.n);
vector <int> cnt(q);
for (auto e : events) {
if (e.id == -1) {
F.modify(e.l, 1);
} else {
cnt[e.id] += e.tp * F.sum(e.l, e.r);
}
}
vector <int> ans(q);
for (int i = 0; i < q; ++i) {
if (cnt[i] > 0) ans[i] = 1;
else ans[i] = 0;
}
return ans;
}
#ifdef LOCAL
namespace {
int read_int() {
int x;
cin >> x;
return x;
}
} // namespace
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int N = read_int();
int M = read_int();
int Q = read_int();
std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
for (int j = 0; j < M; ++j) {
X[j] = read_int();
Y[j] = read_int();
}
for (int i = 0; i < Q; ++i) {
S[i] = read_int();
E[i] = read_int();
L[i] = read_int();
R[i] = read_int();
}
std::vector<int> A = check_validity(N, X, Y, S, E, L, R);
for (size_t i = 0; i < A.size(); ++i) {
printf("%d\n", A[i]);
}
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
2420 KB |
Output is correct |
11 |
Correct |
9 ms |
2420 KB |
Output is correct |
12 |
Correct |
9 ms |
2292 KB |
Output is correct |
13 |
Correct |
10 ms |
2676 KB |
Output is correct |
14 |
Correct |
9 ms |
2676 KB |
Output is correct |
15 |
Correct |
10 ms |
2420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
961 ms |
119204 KB |
Output is correct |
2 |
Correct |
780 ms |
140320 KB |
Output is correct |
3 |
Correct |
994 ms |
132132 KB |
Output is correct |
4 |
Correct |
835 ms |
128544 KB |
Output is correct |
5 |
Correct |
851 ms |
128292 KB |
Output is correct |
6 |
Correct |
1118 ms |
127904 KB |
Output is correct |
7 |
Correct |
949 ms |
125612 KB |
Output is correct |
8 |
Correct |
789 ms |
140324 KB |
Output is correct |
9 |
Correct |
723 ms |
131100 KB |
Output is correct |
10 |
Correct |
720 ms |
127272 KB |
Output is correct |
11 |
Correct |
756 ms |
127376 KB |
Output is correct |
12 |
Correct |
829 ms |
127524 KB |
Output is correct |
13 |
Correct |
819 ms |
140200 KB |
Output is correct |
14 |
Correct |
736 ms |
140324 KB |
Output is correct |
15 |
Correct |
757 ms |
140280 KB |
Output is correct |
16 |
Correct |
798 ms |
140324 KB |
Output is correct |
17 |
Correct |
962 ms |
125456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
9 ms |
2420 KB |
Output is correct |
11 |
Correct |
9 ms |
2420 KB |
Output is correct |
12 |
Correct |
9 ms |
2292 KB |
Output is correct |
13 |
Correct |
10 ms |
2676 KB |
Output is correct |
14 |
Correct |
9 ms |
2676 KB |
Output is correct |
15 |
Correct |
10 ms |
2420 KB |
Output is correct |
16 |
Correct |
961 ms |
119204 KB |
Output is correct |
17 |
Correct |
780 ms |
140320 KB |
Output is correct |
18 |
Correct |
994 ms |
132132 KB |
Output is correct |
19 |
Correct |
835 ms |
128544 KB |
Output is correct |
20 |
Correct |
851 ms |
128292 KB |
Output is correct |
21 |
Correct |
1118 ms |
127904 KB |
Output is correct |
22 |
Correct |
949 ms |
125612 KB |
Output is correct |
23 |
Correct |
789 ms |
140324 KB |
Output is correct |
24 |
Correct |
723 ms |
131100 KB |
Output is correct |
25 |
Correct |
720 ms |
127272 KB |
Output is correct |
26 |
Correct |
756 ms |
127376 KB |
Output is correct |
27 |
Correct |
829 ms |
127524 KB |
Output is correct |
28 |
Correct |
819 ms |
140200 KB |
Output is correct |
29 |
Correct |
736 ms |
140324 KB |
Output is correct |
30 |
Correct |
757 ms |
140280 KB |
Output is correct |
31 |
Correct |
798 ms |
140324 KB |
Output is correct |
32 |
Correct |
962 ms |
125456 KB |
Output is correct |
33 |
Correct |
1000 ms |
131108 KB |
Output is correct |
34 |
Correct |
391 ms |
45040 KB |
Output is correct |
35 |
Correct |
931 ms |
138788 KB |
Output is correct |
36 |
Correct |
935 ms |
129652 KB |
Output is correct |
37 |
Correct |
946 ms |
136968 KB |
Output is correct |
38 |
Correct |
961 ms |
131108 KB |
Output is correct |
39 |
Correct |
880 ms |
152964 KB |
Output is correct |
40 |
Correct |
995 ms |
139156 KB |
Output is correct |
41 |
Correct |
904 ms |
135716 KB |
Output is correct |
42 |
Correct |
880 ms |
128416 KB |
Output is correct |
43 |
Correct |
936 ms |
144548 KB |
Output is correct |
44 |
Correct |
900 ms |
136996 KB |
Output is correct |
45 |
Correct |
802 ms |
152228 KB |
Output is correct |
46 |
Correct |
794 ms |
151976 KB |
Output is correct |
47 |
Correct |
798 ms |
140328 KB |
Output is correct |
48 |
Correct |
749 ms |
140196 KB |
Output is correct |
49 |
Correct |
745 ms |
140324 KB |
Output is correct |
50 |
Correct |
754 ms |
140196 KB |
Output is correct |
51 |
Correct |
909 ms |
138404 KB |
Output is correct |
52 |
Correct |
907 ms |
138532 KB |
Output is correct |