답안 #1044691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044691 2024-08-05T12:28:33 Z VMaksimoski008 늑대인간 (IOI18_werewolf) C++17
49 / 100
4000 ms 89424 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 6e5 + 5;
vector<int> graph[maxn];
int vis[maxn][2], val[maxn], timer = 1, pos[maxn], log_dp[maxn];
pair<int, int> T[maxn+5][21];

void dfs(int u, int p) {
    pos[u] = timer; val[timer++] = u;
    for(int &v : graph[u]) if(v != p) dfs(v, u);
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
    return { min(a.first, b.first), max(a.second, b.second) };
}

pair<int, int> query(int l, int r) {
    int k = log_dp[r-l+1];
    return merge(T[l][k], T[r-(1<<k)+1][k]);
}

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
    int Q = S.size(), M = X.size();
    vector<int> ans(Q);

    for(int i=0; i<M; i++) {
        X[i]++; Y[i]++;
        graph[X[i]].push_back(Y[i]);
        graph[Y[i]].push_back(X[i]);
    }

    int mx = 0;
    for(int i=1; i<=N; i++) mx = max(mx, (int)graph[i].size());

    if(M == N - 1 && mx <= 2) {
        for(int i=1; i<=N; i++) {
            if(graph[i].size() == 1) {
                dfs(i, i);
                break;
            }
        }

        for(int i=2; i<=N; i++) log_dp[i] = log_dp[i/2] + 1;
        for(int i=1; i<=N; i++) T[i][0] = { val[i], val[i] };

        for(int j=1; j<21; j++)
            for(int i=1; i+(1<<j)-1<=N; i++) T[i][j] = merge(T[i][j-1], T[i+(1<<(j-1))][j-1]);

        for(int i=0; i<Q; i++) {
            if(S[i] < L[i]) continue;
            if(E[i] > R[i]) continue;
            S[i]++; E[i]++; L[i]++; R[i]++;
            pair<int, int> A = { pos[S[i]], pos[S[i]] };
            pair<int, int> B = { pos[E[i]], pos[E[i]] };

            int l=pos[S[i]], r=N;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(query(pos[S[i]], mid).first >= L[i]) A.second = mid, l = mid + 1;
                else r = mid - 1;
            }

            l=1, r=pos[S[i]];
            while(l <= r) {
                int mid = (l + r) / 2;
                if(query(mid, pos[S[i]]).first >= L[i]) A.first = mid, r = mid - 1;
                else l = mid + 1;
            }

            l=pos[E[i]], r=N;
            while(l <= r) {
                int mid = (l + r) / 2;
                if(query(pos[E[i]], mid).second <= R[i]) B.second = mid, l = mid + 1;
                else r = mid - 1;
            }

            l=1, r=pos[E[i]];
            while(l <= r) {
                int mid = (l + r) / 2;
                if(query(mid, pos[E[i]]).second <= R[i]) B.first = mid, r = mid - 1;
                else l = mid + 1;
            }
            
            if(min(A.second, B.second) - max(A.first, B.first) >= 0) ans[i] = 1;
        }

        return ans;
    }

    for(int i=0; i<Q; i++) {
        S[i]++; E[i]++; L[i]++; R[i]++;
        queue<pair<int, int> > q;
        memset(vis, 0, sizeof(vis));
        q.push({ S[i], 0 });
        vis[S[i]][0] = 1;

        while(!q.empty()) {
            auto [u, t] = q.front(); q.pop();

            if(t == 0) {
                if(u >= L[i] && u <= R[i] && !vis[u][1]) {
                    vis[u][1] = 1;
                    q.push({ u, 1 });
                }
            }

            for(int &v : graph[u]) {
                if(vis[v][t]) continue;
                if(t == 0 && v < L[i]) continue;
                if(t == 1 && v > R[i]) continue;
                vis[v][t] = 1; q.push({ v, t });
            }
        }

        ans[i] = vis[E[i]][1];
    }

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20060 KB Output is correct
2 Correct 10 ms 20060 KB Output is correct
3 Correct 10 ms 20060 KB Output is correct
4 Correct 10 ms 20076 KB Output is correct
5 Correct 10 ms 20076 KB Output is correct
6 Correct 10 ms 20076 KB Output is correct
7 Correct 10 ms 20060 KB Output is correct
8 Correct 3 ms 20060 KB Output is correct
9 Correct 10 ms 20024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20060 KB Output is correct
2 Correct 10 ms 20060 KB Output is correct
3 Correct 10 ms 20060 KB Output is correct
4 Correct 10 ms 20076 KB Output is correct
5 Correct 10 ms 20076 KB Output is correct
6 Correct 10 ms 20076 KB Output is correct
7 Correct 10 ms 20060 KB Output is correct
8 Correct 3 ms 20060 KB Output is correct
9 Correct 10 ms 20024 KB Output is correct
10 Correct 480 ms 20312 KB Output is correct
11 Correct 358 ms 20316 KB Output is correct
12 Correct 5 ms 22620 KB Output is correct
13 Correct 428 ms 20316 KB Output is correct
14 Correct 341 ms 20316 KB Output is correct
15 Correct 387 ms 20388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 80980 KB Output is correct
2 Correct 335 ms 89192 KB Output is correct
3 Correct 323 ms 89424 KB Output is correct
4 Correct 368 ms 89420 KB Output is correct
5 Correct 345 ms 89172 KB Output is correct
6 Correct 385 ms 89424 KB Output is correct
7 Correct 357 ms 89172 KB Output is correct
8 Correct 340 ms 89420 KB Output is correct
9 Correct 213 ms 89392 KB Output is correct
10 Correct 233 ms 89172 KB Output is correct
11 Correct 247 ms 89168 KB Output is correct
12 Correct 213 ms 89424 KB Output is correct
13 Correct 331 ms 89408 KB Output is correct
14 Correct 344 ms 89388 KB Output is correct
15 Correct 327 ms 89196 KB Output is correct
16 Correct 346 ms 89424 KB Output is correct
17 Correct 347 ms 89408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 20060 KB Output is correct
2 Correct 10 ms 20060 KB Output is correct
3 Correct 10 ms 20060 KB Output is correct
4 Correct 10 ms 20076 KB Output is correct
5 Correct 10 ms 20076 KB Output is correct
6 Correct 10 ms 20076 KB Output is correct
7 Correct 10 ms 20060 KB Output is correct
8 Correct 3 ms 20060 KB Output is correct
9 Correct 10 ms 20024 KB Output is correct
10 Correct 480 ms 20312 KB Output is correct
11 Correct 358 ms 20316 KB Output is correct
12 Correct 5 ms 22620 KB Output is correct
13 Correct 428 ms 20316 KB Output is correct
14 Correct 341 ms 20316 KB Output is correct
15 Correct 387 ms 20388 KB Output is correct
16 Correct 344 ms 80980 KB Output is correct
17 Correct 335 ms 89192 KB Output is correct
18 Correct 323 ms 89424 KB Output is correct
19 Correct 368 ms 89420 KB Output is correct
20 Correct 345 ms 89172 KB Output is correct
21 Correct 385 ms 89424 KB Output is correct
22 Correct 357 ms 89172 KB Output is correct
23 Correct 340 ms 89420 KB Output is correct
24 Correct 213 ms 89392 KB Output is correct
25 Correct 233 ms 89172 KB Output is correct
26 Correct 247 ms 89168 KB Output is correct
27 Correct 213 ms 89424 KB Output is correct
28 Correct 331 ms 89408 KB Output is correct
29 Correct 344 ms 89388 KB Output is correct
30 Correct 327 ms 89196 KB Output is correct
31 Correct 346 ms 89424 KB Output is correct
32 Correct 347 ms 89408 KB Output is correct
33 Execution timed out 4091 ms 45556 KB Time limit exceeded
34 Halted 0 ms 0 KB -