답안 #758771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758771 2023-06-15T09:20:29 Z alexander707070 늑대인간 (IOI18_werewolf) C++14
100 / 100
1503 ms 127500 KB
#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;

struct event{
    int l,r,id;
};

int n,m,q,tt,num[MAXN],to[MAXN],res[MAXN];
vector<int> v[MAXN],mint[MAXN],maxt[MAXN],ans,bro;
int dsu[MAXN],euler[MAXN],from[MAXN],len[MAXN];
int up[MAXN],up2[MAXN];
vector<event> qr[MAXN];

int root(int x){
    if(dsu[x]==x)return x;
    dsu[x]=root(dsu[x]);
    return dsu[x];
}

void setdsu(){
    for(int i=1;i<=n;i++){
        dsu[i]=i;
    }
}

void dfs(int x,int p){
    tt++; num[x]=to[x]=tt; up[x]=p;
    for(int i=0;i<maxt[x].size();i++){
        dfs(maxt[x][i],x);
    }
    to[x]=tt;
}

void dfs2(int x,int p){
    tt++; euler[tt]=num[x];
    from[x]=tt; up2[x]=p;
    for(int i=0;i<mint[x].size();i++){
        dfs2(mint[x][i],x);
    }
    len[x]=tt;
}

int dp[MAXN][25],dp2[MAXN][25];
bool li[MAXN][25],li2[MAXN][25];

int ff(int x,int p){
    if(up[x]==0)return 0;
    if(p==0)return up[x];

    if(li[x][p])return dp[x][p];
    li[x][p]=true;

    dp[x][p]=ff(ff(x,p-1),p-1);
    return dp[x][p];
}

int ff2(int x,int p){
    if(up2[x]==0)return 0;
    if(p==0)return up2[x];

    if(li2[x][p])return dp2[x][p];
    li2[x][p]=true;

    dp2[x][p]=ff2(ff2(x,p-1),p-1);
    return dp2[x][p];
}

int fenwick[MAXN];

void update(int x){
    while(x<=n){
        fenwick[x]++;
        x+=(x & (-x));
    }
}

int sum(int x){
    int res=0;
    while(x>=1){
        res+=fenwick[x];
        x-=(x & (-x));
    }
    return res;
}

void check(int x,int y,int id){
    qr[from[y]-1].push_back({num[x],to[x],id});
    qr[len[y]].push_back({num[x],to[x],id});
}

vector<int> check_validity(int N,vector<int> X,vector<int> Y,vector<int> S,vector<int> T,vector<int> L,vector<int> R){
    n=N; m=int(X.size()); q=int(S.size());

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

    setdsu();
    for(int i=n;i>=1;i--){
        for(int f=0;f<v[i].size();f++){
            if(root(v[i][f])>i){
                maxt[i].push_back(root(v[i][f]));
                dsu[root(v[i][f])]=i;
            }
        }
    }
    dfs(1,0);

    setdsu();
    for(int i=1;i<=n;i++){
        for(int f=0;f<v[i].size();f++){
            if(root(v[i][f])<i){
                mint[i].push_back(root(v[i][f]));
                dsu[root(v[i][f])]=i;
            }
        }
    }
    tt=0;
    dfs2(n,0);

    for(int i=0;i<q;i++){
        S[i]++; T[i]++; L[i]++; R[i]++;

        for(int f=24;f>=0;f--){
            if(ff(S[i],f)!=0 and ff(S[i],f)>=L[i]){
                S[i]=ff(S[i],f);
            }
        }

        for(int f=24;f>=0;f--){
            if(ff2(T[i],f)!=0 and ff2(T[i],f)<=R[i]){
                T[i]=ff2(T[i],f);
            }
        }

        check(S[i],T[i],i);
        res[i]=-1;
    }
    
    for(int i=0;i<=n;i++){
        if(i!=0)update(euler[i]);

        for(int f=0;f<qr[i].size();f++){
            if(res[qr[i][f].id]==-1){
                res[qr[i][f].id]=sum(qr[i][f].r)-sum(qr[i][f].l-1);
            }else{
                res[qr[i][f].id]=sum(qr[i][f].r)-sum(qr[i][f].l-1)-res[qr[i][f].id];
            }
        }
    }

    for(int i=0;i<q;i++){
        if(res[i]>0)ans.push_back(1);
        else ans.push_back(0);
    }

    return ans;
}

/*
int main(){
    bro=check_validity(6, {5, 1, 1, 3, 3, 5}, {1, 2, 3, 4, 0, 2},
{4, 4, 5}, {2, 2, 4}, {1, 2, 3}, {2, 2, 4});
    cout<<bro[0]<<" "<<bro[1]<<" "<<bro[2]<<"\n";
}
*/

Compilation message

werewolf.cpp: In function 'void dfs(int, int)':
werewolf.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i=0;i<maxt[x].size();i++){
      |                 ~^~~~~~~~~~~~~~~
werewolf.cpp: In function 'void dfs2(int, int)':
werewolf.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i=0;i<mint[x].size();i++){
      |                 ~^~~~~~~~~~~~~~~
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:103:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         for(int f=0;f<v[i].size();f++){
      |                     ~^~~~~~~~~~~~
werewolf.cpp:114:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |         for(int f=0;f<v[i].size();f++){
      |                     ~^~~~~~~~~~~~
werewolf.cpp:146:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |         for(int f=0;f<qr[i].size();f++){
      |                     ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19096 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 11 ms 19088 KB Output is correct
5 Correct 11 ms 19156 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 10 ms 19128 KB Output is correct
9 Correct 10 ms 19104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19096 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 11 ms 19088 KB Output is correct
5 Correct 11 ms 19156 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 10 ms 19128 KB Output is correct
9 Correct 10 ms 19104 KB Output is correct
10 Correct 14 ms 20564 KB Output is correct
11 Correct 14 ms 20500 KB Output is correct
12 Correct 13 ms 20436 KB Output is correct
13 Correct 15 ms 20692 KB Output is correct
14 Correct 14 ms 20564 KB Output is correct
15 Correct 16 ms 20564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 615 ms 109192 KB Output is correct
2 Correct 1128 ms 112316 KB Output is correct
3 Correct 735 ms 109896 KB Output is correct
4 Correct 587 ms 108736 KB Output is correct
5 Correct 592 ms 108688 KB Output is correct
6 Correct 623 ms 117252 KB Output is correct
7 Correct 537 ms 116244 KB Output is correct
8 Correct 615 ms 120664 KB Output is correct
9 Correct 409 ms 101180 KB Output is correct
10 Correct 288 ms 69064 KB Output is correct
11 Correct 279 ms 72100 KB Output is correct
12 Correct 312 ms 75836 KB Output is correct
13 Correct 1490 ms 126928 KB Output is correct
14 Correct 1499 ms 127020 KB Output is correct
15 Correct 1503 ms 126956 KB Output is correct
16 Correct 1487 ms 127148 KB Output is correct
17 Correct 526 ms 116232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 19156 KB Output is correct
2 Correct 10 ms 19096 KB Output is correct
3 Correct 11 ms 19156 KB Output is correct
4 Correct 11 ms 19088 KB Output is correct
5 Correct 11 ms 19156 KB Output is correct
6 Correct 13 ms 19200 KB Output is correct
7 Correct 11 ms 19156 KB Output is correct
8 Correct 10 ms 19128 KB Output is correct
9 Correct 10 ms 19104 KB Output is correct
10 Correct 14 ms 20564 KB Output is correct
11 Correct 14 ms 20500 KB Output is correct
12 Correct 13 ms 20436 KB Output is correct
13 Correct 15 ms 20692 KB Output is correct
14 Correct 14 ms 20564 KB Output is correct
15 Correct 16 ms 20564 KB Output is correct
16 Correct 615 ms 109192 KB Output is correct
17 Correct 1128 ms 112316 KB Output is correct
18 Correct 735 ms 109896 KB Output is correct
19 Correct 587 ms 108736 KB Output is correct
20 Correct 592 ms 108688 KB Output is correct
21 Correct 623 ms 117252 KB Output is correct
22 Correct 537 ms 116244 KB Output is correct
23 Correct 615 ms 120664 KB Output is correct
24 Correct 409 ms 101180 KB Output is correct
25 Correct 288 ms 69064 KB Output is correct
26 Correct 279 ms 72100 KB Output is correct
27 Correct 312 ms 75836 KB Output is correct
28 Correct 1490 ms 126928 KB Output is correct
29 Correct 1499 ms 127020 KB Output is correct
30 Correct 1503 ms 126956 KB Output is correct
31 Correct 1487 ms 127148 KB Output is correct
32 Correct 526 ms 116232 KB Output is correct
33 Correct 829 ms 118272 KB Output is correct
34 Correct 267 ms 58132 KB Output is correct
35 Correct 1057 ms 121780 KB Output is correct
36 Correct 751 ms 118464 KB Output is correct
37 Correct 966 ms 120772 KB Output is correct
38 Correct 814 ms 119148 KB Output is correct
39 Correct 981 ms 127364 KB Output is correct
40 Correct 1219 ms 127404 KB Output is correct
41 Correct 419 ms 86660 KB Output is correct
42 Correct 305 ms 72824 KB Output is correct
43 Correct 766 ms 109696 KB Output is correct
44 Correct 564 ms 99904 KB Output is correct
45 Correct 456 ms 103720 KB Output is correct
46 Correct 473 ms 103472 KB Output is correct
47 Correct 1480 ms 127320 KB Output is correct
48 Correct 1480 ms 126984 KB Output is correct
49 Correct 1473 ms 127064 KB Output is correct
50 Correct 1499 ms 127088 KB Output is correct
51 Correct 1154 ms 127500 KB Output is correct
52 Correct 1100 ms 127464 KB Output is correct