#include "werewolf.h"
#include "bits/stdc++.h"
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vp = vector<P>;
using vvp = vector<vp>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;
template<class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
} else {
return false;
}
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
} else {
return false;
}
}
// {min, max}
using S = P;
const S e = {inf, -inf};
S op(S a, S b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
class segtree {
int n, log;
vector<S> d;
void update(int p) {
assert(p < n);
d[p] = op(d[2 * p], d[2 * p + 1]);
}
public:
segtree(const vector<S> &init = {}) {
log = 0;
while (1 << log < SZ(init)) ++log;
n = 1 << log;
d.assign(2 * n, e);
rep(i, SZ(init)) d[n + i] = init[i];
for (int i = n - 1; i >= 1; --i) update(i);
}
S prod(int l, int r) {
assert(0 <= l and l <= r and r <= n);
l += n, r += n;
S res = e;
while (l < r) {
if (l & 1) res = op(res, d[l++]);
if (r & 1) res = op(res, d[--r]);
l >>= 1, r >>= 1;
}
return res;
}
};
vi check_validity(int n, vi x, vi y, vi s, vi e, vi l, vi r) {
int m = SZ(x), q = SZ(s);
vi ans(q);
vvi G(n);
rep(i, m) {
G[x[i]].pb(y[i]);
G[y[i]].pb(x[i]);
}
bool is_line = (m == n - 1);
rep(i, n) is_line &= SZ(G[i]) <= 2;
if (is_line) {
vi ls;
auto dfs = [&](auto &dfs, int u, int p) -> void {
ls.pb(u);
for (int v: G[u]) {
if (v == p) continue;
dfs(dfs, v, u);
}
};
rep(i, n) if (SZ(G[i]) == 1) {
dfs(dfs, i, -1);
break;
}
vi pos(n);
rep(i, n) pos[ls[i]] = i;
vp init(n);
rep(i, n) init[i] = {ls[i], ls[i]};
segtree st(init);
rep(i, q) {
P ok_s, ok_e;
{
int ok = pos[s[i]], ng = -1;
auto f = [&](int x) -> bool {
return st.prod(x, pos[s[i]]).first >= l[i];
};
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
ok_s.first = ok;
}
{
int ok = pos[s[i]] + 1, ng = n + 1;
auto f = [&](int x) -> bool {
return st.prod(pos[s[i]], x).first >= l[i];
};
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
ok_s.second = ok;
}
{
int ok = pos[e[i]], ng = -1;
auto f = [&](int x) -> bool {
return st.prod(x, pos[e[i]]).second <= r[i];
};
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
ok_e.first = ok;
}
{
int ok = pos[e[i]] + 1, ng = n + 1;
auto f = [&](int x) -> bool {
return st.prod(pos[e[i]], x).second <= r[i];
};
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (f(mid)) ok = mid;
else ng = mid;
}
ok_e.second = ok;
}
if (ok_s.second > ok_e.first and ok_e.second > ok_s.first) ans[i] = 1;
}
} else {
rep(i, q) {
vb ok_s(n), ok_e(n);
{
auto dfs = [&](auto &dfs, int u) -> void {
ok_s[u] = true;
for (int v: G[u]) {
if (v < l[i]) continue;
if (ok_s[v]) continue;
dfs(dfs, v);
}
};
dfs(dfs, s[i]);
}
{
auto dfs = [&](auto &dfs, int u) -> void {
ok_e[u] = true;
for (int v: G[u]) {
if (v > r[i]) continue;
if (ok_e[v]) continue;
dfs(dfs, v);
}
};
dfs(dfs, e[i]);
}
rep(j, n) if (ok_s[j] and ok_e[j]) ans[i] = 1;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
298 ms |
724 KB |
Output is correct |
11 |
Correct |
195 ms |
712 KB |
Output is correct |
12 |
Correct |
9 ms |
852 KB |
Output is correct |
13 |
Correct |
312 ms |
732 KB |
Output is correct |
14 |
Correct |
213 ms |
700 KB |
Output is correct |
15 |
Correct |
249 ms |
960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1192 ms |
43252 KB |
Output is correct |
2 |
Correct |
1723 ms |
43152 KB |
Output is correct |
3 |
Correct |
1724 ms |
43144 KB |
Output is correct |
4 |
Correct |
1578 ms |
43276 KB |
Output is correct |
5 |
Correct |
1573 ms |
43148 KB |
Output is correct |
6 |
Correct |
1344 ms |
43148 KB |
Output is correct |
7 |
Correct |
1538 ms |
43148 KB |
Output is correct |
8 |
Correct |
1664 ms |
43152 KB |
Output is correct |
9 |
Correct |
788 ms |
43164 KB |
Output is correct |
10 |
Correct |
1140 ms |
43232 KB |
Output is correct |
11 |
Correct |
1216 ms |
43136 KB |
Output is correct |
12 |
Correct |
739 ms |
43164 KB |
Output is correct |
13 |
Correct |
1634 ms |
43244 KB |
Output is correct |
14 |
Correct |
1686 ms |
43164 KB |
Output is correct |
15 |
Correct |
1644 ms |
43072 KB |
Output is correct |
16 |
Correct |
1619 ms |
43148 KB |
Output is correct |
17 |
Correct |
1562 ms |
43148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
298 ms |
724 KB |
Output is correct |
11 |
Correct |
195 ms |
712 KB |
Output is correct |
12 |
Correct |
9 ms |
852 KB |
Output is correct |
13 |
Correct |
312 ms |
732 KB |
Output is correct |
14 |
Correct |
213 ms |
700 KB |
Output is correct |
15 |
Correct |
249 ms |
960 KB |
Output is correct |
16 |
Correct |
1192 ms |
43252 KB |
Output is correct |
17 |
Correct |
1723 ms |
43152 KB |
Output is correct |
18 |
Correct |
1724 ms |
43144 KB |
Output is correct |
19 |
Correct |
1578 ms |
43276 KB |
Output is correct |
20 |
Correct |
1573 ms |
43148 KB |
Output is correct |
21 |
Correct |
1344 ms |
43148 KB |
Output is correct |
22 |
Correct |
1538 ms |
43148 KB |
Output is correct |
23 |
Correct |
1664 ms |
43152 KB |
Output is correct |
24 |
Correct |
788 ms |
43164 KB |
Output is correct |
25 |
Correct |
1140 ms |
43232 KB |
Output is correct |
26 |
Correct |
1216 ms |
43136 KB |
Output is correct |
27 |
Correct |
739 ms |
43164 KB |
Output is correct |
28 |
Correct |
1634 ms |
43244 KB |
Output is correct |
29 |
Correct |
1686 ms |
43164 KB |
Output is correct |
30 |
Correct |
1644 ms |
43072 KB |
Output is correct |
31 |
Correct |
1619 ms |
43148 KB |
Output is correct |
32 |
Correct |
1562 ms |
43148 KB |
Output is correct |
33 |
Execution timed out |
4057 ms |
30204 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |