답안 #1067048

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067048 2024-08-20T10:11:31 Z TheQuantiX 늑대인간 (IOI18_werewolf) C++17
15 / 100
730 ms 41808 KB
#include "werewolf.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll INF = 100000000000000000LL;

ll n, m, q, k, x, y, a, b, c;
vector<int> G[200000];
int to[200000];
int from[200000];

int bfs(ll s, ll e, ll l, ll r) {
    queue< pair<ll, bool> > q;
    q.push({s, 0});
    vector< vector<ll> > vis(n, vector<ll> (2, 0));
    vis[s][0] = 1;
    while (!q.empty()) {
        auto [x, w] = q.front();
        q.pop();
        // cout << x << ' ' << w << '\n';
        if (!vis[x][1] && x <= r) {
            q.push({x, 1});
            vis[x][1] = 1;
        }
        for (int i : G[x]) {
            if (!vis[i][w] && (w == 1 || i >= l) && (w == 0 || i <= r)) {
                q.push({i, w});
                vis[i][w] = 1;
            }
        }
    }
    // cout << '\n';
    return vis[e][1];
}

void dfs(ll x, ll p, ll num) {
    to[x] = num;
    from[num] = x;
    for (auto i : G[x]) {
        if (i != p) {
            dfs(i, x, num + 1);
        }
    }
}

struct segtree {
    ll n;
    vector<ll> mintr;
    vector<ll> maxtr;

    void build(ll x, ll l, ll r) {
        if (l == r) {
            mintr[x] = from[l];
            maxtr[x] = from[r];
            return;
        }
        ll mid = (r + l) / 2;
        build(x * 2 + 1, l, mid);
        build(x * 2 + 2, mid + 1, r);
        mintr[x] = min(mintr[x * 2 + 1], mintr[x * 2 + 2]);
        maxtr[x] = min(maxtr[x * 2 + 1], maxtr[x * 2 + 2]);
    }

    segtree(ll N) : n(N) {
        mintr.resize(4 * n);
        maxtr.resize(4 * n);
        build(0, 0, n - 1);
    }

    ll getmx(ll x, ll l, ll r, ll L, ll R) {
        if (r < l) {
            return -INF;
        }
        if (r < L) {
            return -INF;
        }
        if (R < l) {
            return -INF;
        }
        if (L <= l && r <= R) {
            return maxtr[x];
        }
        ll mid = (r + l) / 2;
        return max(getmx(x * 2 + 1, l, mid, L, R), getmx(x * 2 + 2, mid + 1, r, L, R));
    }

    ll getmn(ll x, ll l, ll r, ll L, ll R) {
        if (r < l) {
            return INF;
        }
        if (r < L) {
            return INF;
        }
        if (R < l) {
            return INF;
        }
        if (L <= l && r <= R) {
            return mintr[x];
        }
        ll mid = (r + l) / 2;
        return min(getmn(x * 2 + 1, l, mid, L, R), getmn(x * 2 + 2, mid + 1, r, L, R));
    }
};

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();
    for (int i = 0; i < m; i++) {
        G[X[i]].push_back(Y[i]);
        G[Y[i]].push_back(X[i]);
    }
    vector<int> ans;
    if (n <= 3000 && m <= 6000 && q <= 3000) {
        for (int i = 0; i < q; i++) {
            ans.push_back(bfs(S[i], E[i], L[i], R[i]));
        }
    }
    else {
        ans.resize(q);
        ll x = 0;
        for (int i = 0; i < n; i++) {
            if (G[i].size() == 1) {
                x = i;
                break;
            }
        }
        dfs(x, -1, 0);
        // cout << (*from.rbegin()).first << endl;
        segtree sg(n);
        for (int i = 0; i < q; i++) {
            ll x = to[S[i]];
            ll y = to[E[i]];
            // cout << x << ' ' << y << '\n';
            if (x < y) {
                ll l = x, r = y + 1;
                while (r > l) {
                    ll mid = (r + l) / 2;
                    if (sg.getmn(0, 0, n - 1, l, mid) < L[i]) {
                        r = mid;
                    }
                    else {
                        l = mid + 1;
                    }
                }
                ans[i] = 1;
                if (l == x) {
                    ans[i] = 0;
                }
                if (from[l - 1] > R[i]) {
                    ans[i] = 0;
                }
                if (from[y] > R[i]) {
                    ans[i] = 0;
                }
                if (sg.getmx(0, 0, n - 1, l, y) > R[i]) {
                    ans[i] = 0;
                }
            }
            else {
                swap(x, y);
                ll l = x, r = y + 1;
                while (r > l) {
                    ll mid = (r + l) / 2;
                    // cout << mid << endl;
                    if (sg.getmx(0, 0, n - 1, l, mid) > R[i]) {
                        r = mid;
                    }
                    else {
                        l = mid + 1;
                    }
                }
                // cout << "DEBUG" << endl;
                ans[i] = 1;
                if (l == x) {
                    ans[i] = 0;
                }
                if (from[l - 1] < L[i]) {
                    ans[i] = 0;
                }
                if (from[y] < L[i]) {
                    ans[i] = 0;
                }
                if (sg.getmn(0, 0, n - 1, l, y) < L[i]) {
                    ans[i] = 0;
                }
            }
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6312 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6312 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 617 ms 6744 KB Output is correct
11 Correct 527 ms 6940 KB Output is correct
12 Correct 302 ms 6936 KB Output is correct
13 Correct 570 ms 6960 KB Output is correct
14 Correct 464 ms 6956 KB Output is correct
15 Correct 513 ms 7008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 730 ms 41808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6312 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6492 KB Output is correct
10 Correct 617 ms 6744 KB Output is correct
11 Correct 527 ms 6940 KB Output is correct
12 Correct 302 ms 6936 KB Output is correct
13 Correct 570 ms 6960 KB Output is correct
14 Correct 464 ms 6956 KB Output is correct
15 Correct 513 ms 7008 KB Output is correct
16 Incorrect 730 ms 41808 KB Output isn't correct
17 Halted 0 ms 0 KB -