Submission #837383

# Submission time Handle Problem Language Result Execution time Memory
837383 2023-08-25T10:07:37 Z becaido Werewolf (IOI18_werewolf) C++17
100 / 100
565 ms 97436 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "werewolf.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() { cout << "\n"; }
template<typename T, typename...U>
void dout(T t, U...u) { cout << t << (sizeof...(u) ? ", " : ""), dout(u...); }
#else
#define debug(...) 7122s
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for(int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 2e5 + 5;
const int H = __lg(SIZE);

int n, m, q;
vector<int> ans, adj[SIZE];
vector<pair<int, int>> ask[SIZE];

struct DSU {
    int to[SIZE];
    DSU() = default;
    void init() {
        iota(to, to + n, 0);
    }
    int dsu(int x) {
        return x == to[x] ? x : (to[x] = dsu(to[x]));
    }
};

struct Tree {
    vector<int> adj[SIZE];
    int root, to[SIZE][H + 1], h[SIZE];
    int dfsid, in[SIZE], out[SIZE], w[SIZE];
    DSU dsu;
    Tree() = default;
    void init() {
        dfsid = -1;
        dsu.init();
    }
    void add(int a, int b) {
        a = dsu.dsu(a), b = dsu.dsu(b);
        if (a == b) return;
        adj[a].pb(b);
        dsu.to[b] = a;
        root = a;
    }
    void build() {
        auto dfs = [&](auto dfs, int pos)->void {
            in[pos] = ++dfsid;
            w[pos] = 1;
            for (int np : adj[pos]) {
                h[np] = h[pos] + 1;
                to[np][0] = pos;
                dfs(dfs, np);
                w[pos] += w[np];
            }
            out[pos] = dfsid;
        };
        to[root][0] = root;
        dfs(dfs, root);
        FOR (j, 1, H) FOR (i, 0, n - 1) to[i][j] = to[to[i][j - 1]][j - 1];
    }
    int sch(int pos, int lim, int ty) {
        for (int i = H; i >= 0; i--) if ((ty == 1 && to[pos][i] >= lim) || (ty == 2 && to[pos][i] <= lim)) pos = to[pos][i];
        return pos;
    }
} t1, t2;

int a[SIZE], bit[SIZE];
void upd(int pos, int x) {
    for (pos++; pos <= n; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
    int re = 0;
    for (pos++; pos; pos -= pos & -pos) re += bit[pos];
    return re;
}
int que(int l, int r) {
    return que(r) - que(l - 1);
}

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
    n = N, m = X.size(), q = S.size();
    ans.resize(q);
    FOR (i, 0, m - 1) {
        adj[X[i]].pb(Y[i]);
        adj[Y[i]].pb(X[i]);
    }
    t1.init(), t2.init();
    for (int i = n - 1; i >= 0; i--) for (int j : adj[i]) if (i < j) t1.add(i, j);
    FOR (i, 0, n - 1) for (int j : adj[i]) if (i > j) t2.add(i, j);
    t1.build(), t2.build();
    FOR (i, 0, n - 1) a[t1.in[i]] = i;
    FOR (i, 0, q - 1) {
        S[i] = t1.sch(S[i], L[i], 1);
        E[i] = t2.sch(E[i], R[i], 2);
        ask[t1.out[S[i]]].pb(i, 1);
        if (t1.in[S[i]]) ask[t1.in[S[i]] - 1].pb(i, -1);
    }
    FOR (i, 0, n - 1) {
        upd(t2.in[a[i]], 1);
        for (auto [id, coef] : ask[i]) ans[id] += coef * que(t2.in[E[id]], t2.out[E[id]]);
    }
    FOR (i, 0, q - 1) ans[i] = ans[i] > 0;
    return ans;
}

/*
in1
6 6 3
5 1
1 2
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4
out1
1
0
0
*/

#ifdef WAIMAI
namespace {

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

}  // namespace

int main() {
  int N = read_int();
  int M = read_int();
  int Q = read_int();
  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();
  }

  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]);
  }
  return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19156 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 14 ms 19268 KB Output is correct
6 Correct 10 ms 19088 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 10 ms 19184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19156 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 14 ms 19268 KB Output is correct
6 Correct 10 ms 19088 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 10 ms 19184 KB Output is correct
10 Correct 14 ms 20180 KB Output is correct
11 Correct 15 ms 20164 KB Output is correct
12 Correct 14 ms 20160 KB Output is correct
13 Correct 14 ms 20296 KB Output is correct
14 Correct 14 ms 20332 KB Output is correct
15 Correct 15 ms 20308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 88196 KB Output is correct
2 Correct 395 ms 89932 KB Output is correct
3 Correct 367 ms 87740 KB Output is correct
4 Correct 368 ms 86720 KB Output is correct
5 Correct 371 ms 86984 KB Output is correct
6 Correct 389 ms 87916 KB Output is correct
7 Correct 369 ms 86632 KB Output is correct
8 Correct 369 ms 89872 KB Output is correct
9 Correct 298 ms 86436 KB Output is correct
10 Correct 298 ms 86404 KB Output is correct
11 Correct 297 ms 86272 KB Output is correct
12 Correct 294 ms 85860 KB Output is correct
13 Correct 505 ms 95388 KB Output is correct
14 Correct 484 ms 95368 KB Output is correct
15 Correct 513 ms 95324 KB Output is correct
16 Correct 505 ms 95388 KB Output is correct
17 Correct 384 ms 86776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 11 ms 19156 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 10 ms 19156 KB Output is correct
5 Correct 14 ms 19268 KB Output is correct
6 Correct 10 ms 19088 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 10 ms 19184 KB Output is correct
10 Correct 14 ms 20180 KB Output is correct
11 Correct 15 ms 20164 KB Output is correct
12 Correct 14 ms 20160 KB Output is correct
13 Correct 14 ms 20296 KB Output is correct
14 Correct 14 ms 20332 KB Output is correct
15 Correct 15 ms 20308 KB Output is correct
16 Correct 437 ms 88196 KB Output is correct
17 Correct 395 ms 89932 KB Output is correct
18 Correct 367 ms 87740 KB Output is correct
19 Correct 368 ms 86720 KB Output is correct
20 Correct 371 ms 86984 KB Output is correct
21 Correct 389 ms 87916 KB Output is correct
22 Correct 369 ms 86632 KB Output is correct
23 Correct 369 ms 89872 KB Output is correct
24 Correct 298 ms 86436 KB Output is correct
25 Correct 298 ms 86404 KB Output is correct
26 Correct 297 ms 86272 KB Output is correct
27 Correct 294 ms 85860 KB Output is correct
28 Correct 505 ms 95388 KB Output is correct
29 Correct 484 ms 95368 KB Output is correct
30 Correct 513 ms 95324 KB Output is correct
31 Correct 505 ms 95388 KB Output is correct
32 Correct 384 ms 86776 KB Output is correct
33 Correct 450 ms 88576 KB Output is correct
34 Correct 191 ms 43512 KB Output is correct
35 Correct 548 ms 90668 KB Output is correct
36 Correct 449 ms 88876 KB Output is correct
37 Correct 541 ms 90084 KB Output is correct
38 Correct 450 ms 89172 KB Output is correct
39 Correct 565 ms 97436 KB Output is correct
40 Correct 480 ms 94008 KB Output is correct
41 Correct 409 ms 88268 KB Output is correct
42 Correct 347 ms 86212 KB Output is correct
43 Correct 457 ms 93968 KB Output is correct
44 Correct 446 ms 89412 KB Output is correct
45 Correct 331 ms 95532 KB Output is correct
46 Correct 346 ms 95828 KB Output is correct
47 Correct 480 ms 95396 KB Output is correct
48 Correct 515 ms 95348 KB Output is correct
49 Correct 490 ms 95548 KB Output is correct
50 Correct 486 ms 95292 KB Output is correct
51 Correct 467 ms 93440 KB Output is correct
52 Correct 455 ms 93592 KB Output is correct