답안 #305514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
305514 2020-09-23T12:09:06 Z oleh1421 늑대인간 (IOI18_werewolf) C++17
100 / 100
956 ms 104940 KB
#define SYS
#ifdef SYS
#include "werewolf.h"
#endif // SYS

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=300010;
int p[N];
int q[N];
int a[N];
int b[N];
int dsu[N];
int get(int x){
    if (x==dsu[x]) return x;
    return get(dsu[x]);
}
vector<int>cmp[N];
vector<int>g[N];
vector<pair<int,int> >qS[N];
vector<pair<int,int> >qE[N];
int La[N];
int Ra[N];
int Lb[N];
int Rb[N];
int dsu1[N];
int mn[N];
int mx[N];
int get1(int x){
    if (dsu1[x]==x) return x;
    return dsu1[x]=get1(dsu1[x]);
}
int sum[N];
vector<int>dec1[N];
vector<int>add[N];
int BIT[N];
void upd(int n,int pos,int x){
    for (;pos<=n;pos|=pos+1){
        BIT[pos]+=x;
    }
}
int get_sum(int pos){
    int sum=0;
    for (;pos>=0;pos&=pos+1,pos--){
        sum+=BIT[pos];
    }
    return sum;
}
vector<int> check_validity(int N, vector<int> X, std::vector<int> Y,vector<int> S,vector<int> E,vector<int> L, vector<int> R) {
    int Q = S.size();
    vector<int> Ans(Q,0);
    for (int i=0;i<X.size();i++){
        g[X[i]].push_back(Y[i]);
        g[Y[i]].push_back(X[i]);
    }
    for (int i=0;i<N;i++) dsu[i]=i,cmp[i]={i},p[i]=0;
    for (int i=N-1;i>=0;i--){
        for (int to:g[i]){
            if (to<i) continue;
            int x=get(i);
            int y=get(to);
            if (x==y) continue;
            if (cmp[x].size()>cmp[y].size()) swap(x,y);
            for (int v:cmp[x]){
                p[v]+=cmp[y].size();
            }
            for (int v:cmp[x]){
                cmp[y].push_back(v);
            }
            cmp[x].clear();
            dsu[x]=y;
        }
    }
    for (int i=0;i<N;i++) dsu[i]=i,cmp[i]={i},q[i]=0;
    for (int i=0;i<N;i++){
        for (int to:g[i]){
            if (to>i) continue;
            int x=get(i);
            int y=get(to);
            if (x==y) continue;
//            cout<<"edge: "<<to<<" "<<i<<" "<<x<<" "<<y<<" "<<cmp[x].size()<<" "<<cmp[y].size()<<endl;

            if (cmp[x].size()>cmp[y].size()) swap(x,y);
            for (int v:cmp[x]){
                q[v]+=cmp[y].size();
            }
            for (int v:cmp[x]){
                cmp[y].push_back(v);
            }
            cmp[x].clear();
            dsu[x]=y;

        }
//        for (int j=0;j<N;j++) cout<<q[j]<<" ";
//        cout<<endl;
    }

    for (int i=0;i<N;i++){
//        cout<<p[i]<<" "<<q[i]<<endl;
        a[p[i]]=i;
        b[q[i]]=i;
    }
//    for (int i=0;i<N;i++) cout<<a[i]<<" ";
//    cout<<endl;
//    for (int i=0;i<N;i++) cout<<b[i]<<" ";
//    cout<<endl;
    for (int i=0;i<Q;i++){
        qS[L[i]].push_back({S[i],i});
        qE[R[i]].push_back({E[i],i});
    }
    for (int i=0;i<N;i++){
        dsu1[i]=i;
        mn[i]=p[i];
        mx[i]=p[i];
    }
    for (int i=N-1;i>=0;i--){
        for (int to:g[i]){
            if (to<i) continue;
            int x=get1(i);
            int y=get1(to);
            if (x==y) continue;
            mn[y]=min(mn[y],mn[x]);
            mx[y]=max(mx[y],mx[x]);
            dsu1[x]=y;
        }
        for (auto cur:qS[i]){
            La[cur.second]=mn[get1(cur.first)];
            Ra[cur.second]=mx[get1(cur.first)];
        }
    }
    for (int i=0;i<N;i++){
        dsu1[i]=i;
        mn[i]=q[i];
        mx[i]=q[i];
    }
    for (int i=0;i<N;i++){
        for (int to:g[i]){
            if (to>i) continue;
            int x=get1(i);
            int y=get1(to);
            if (x==y) continue;
            mn[y]=min(mn[y],mn[x]);
            mx[y]=max(mx[y],mx[x]);
            dsu1[x]=y;
        }
        for (auto cur:qE[i]){
            Lb[cur.second]=mn[get1(cur.first)];
            Rb[cur.second]=mx[get1(cur.first)];
        }
    }
    for (int i=0;i<Q;i++){
        if (La[i]){
            dec1[La[i]-1].push_back(i);
        }
        add[Ra[i]].push_back(i);
    }
    for (int i=0;i<N;i++){
        upd(N,q[a[i]],1);
        for (int j:dec1[i]){
            sum[j]-=get_sum(Rb[j])-get_sum(Lb[j]-1);
        }
        for (int j:add[i]){
            sum[j]+=get_sum(Rb[j])-get_sum(Lb[j]-1);
        }
    }
    for (int i=0;i<Q;i++){
        Ans[i]=(sum[i]>0);
    }
//    for (int i=0;i<Q;i++){
//        Ans[i]=0;
//        set<int>st;
//        for (int j=La[i];j<=Ra[i];j++){
//            st.insert(a[j]);
//        }
//        for (int j=Lb[i];j<=Rb[i];j++){
//            if (st.find(b[j])!=st.end()) Ans[i]=1;
//        }
//    }
    return Ans;
}
#include <cstdio>
#include <cstdlib>
#include <vector>
#ifndef SYS
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();
  std::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();
  }

  std::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
/*
10 9 10
6 7
1 5
8 0
2 9
9 4
2 7
8 5
6 0
3 4
4 9 0 9
8 1 8 9
1 8 1 8
8 3 5 5
8 9 3 9
0 1 0 2
9 0 6 6
1 7 1 8
9 4 5 6
9 5 0 9

*/

Compilation message

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:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |     for (int i=0;i<X.size();i++){
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 42744 KB Output is correct
2 Correct 30 ms 42752 KB Output is correct
3 Correct 31 ms 42752 KB Output is correct
4 Correct 30 ms 42624 KB Output is correct
5 Correct 31 ms 42744 KB Output is correct
6 Correct 34 ms 42744 KB Output is correct
7 Correct 31 ms 42748 KB Output is correct
8 Correct 31 ms 42752 KB Output is correct
9 Correct 33 ms 42744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 42744 KB Output is correct
2 Correct 30 ms 42752 KB Output is correct
3 Correct 31 ms 42752 KB Output is correct
4 Correct 30 ms 42624 KB Output is correct
5 Correct 31 ms 42744 KB Output is correct
6 Correct 34 ms 42744 KB Output is correct
7 Correct 31 ms 42748 KB Output is correct
8 Correct 31 ms 42752 KB Output is correct
9 Correct 33 ms 42744 KB Output is correct
10 Correct 36 ms 43512 KB Output is correct
11 Correct 36 ms 43520 KB Output is correct
12 Correct 37 ms 43640 KB Output is correct
13 Correct 35 ms 43512 KB Output is correct
14 Correct 34 ms 43520 KB Output is correct
15 Correct 36 ms 43648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 956 ms 102288 KB Output is correct
2 Correct 629 ms 96364 KB Output is correct
3 Correct 635 ms 98076 KB Output is correct
4 Correct 737 ms 101484 KB Output is correct
5 Correct 757 ms 102408 KB Output is correct
6 Correct 863 ms 104940 KB Output is correct
7 Correct 760 ms 104676 KB Output is correct
8 Correct 613 ms 96236 KB Output is correct
9 Correct 619 ms 97004 KB Output is correct
10 Correct 634 ms 99552 KB Output is correct
11 Correct 657 ms 100236 KB Output is correct
12 Correct 760 ms 103020 KB Output is correct
13 Correct 685 ms 97984 KB Output is correct
14 Correct 697 ms 98024 KB Output is correct
15 Correct 668 ms 98004 KB Output is correct
16 Correct 670 ms 97896 KB Output is correct
17 Correct 771 ms 104932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 42744 KB Output is correct
2 Correct 30 ms 42752 KB Output is correct
3 Correct 31 ms 42752 KB Output is correct
4 Correct 30 ms 42624 KB Output is correct
5 Correct 31 ms 42744 KB Output is correct
6 Correct 34 ms 42744 KB Output is correct
7 Correct 31 ms 42748 KB Output is correct
8 Correct 31 ms 42752 KB Output is correct
9 Correct 33 ms 42744 KB Output is correct
10 Correct 36 ms 43512 KB Output is correct
11 Correct 36 ms 43520 KB Output is correct
12 Correct 37 ms 43640 KB Output is correct
13 Correct 35 ms 43512 KB Output is correct
14 Correct 34 ms 43520 KB Output is correct
15 Correct 36 ms 43648 KB Output is correct
16 Correct 956 ms 102288 KB Output is correct
17 Correct 629 ms 96364 KB Output is correct
18 Correct 635 ms 98076 KB Output is correct
19 Correct 737 ms 101484 KB Output is correct
20 Correct 757 ms 102408 KB Output is correct
21 Correct 863 ms 104940 KB Output is correct
22 Correct 760 ms 104676 KB Output is correct
23 Correct 613 ms 96236 KB Output is correct
24 Correct 619 ms 97004 KB Output is correct
25 Correct 634 ms 99552 KB Output is correct
26 Correct 657 ms 100236 KB Output is correct
27 Correct 760 ms 103020 KB Output is correct
28 Correct 685 ms 97984 KB Output is correct
29 Correct 697 ms 98024 KB Output is correct
30 Correct 668 ms 98004 KB Output is correct
31 Correct 670 ms 97896 KB Output is correct
32 Correct 771 ms 104932 KB Output is correct
33 Correct 911 ms 101516 KB Output is correct
34 Correct 381 ms 83064 KB Output is correct
35 Correct 823 ms 99692 KB Output is correct
36 Correct 926 ms 102124 KB Output is correct
37 Correct 851 ms 99656 KB Output is correct
38 Correct 943 ms 101228 KB Output is correct
39 Correct 872 ms 101620 KB Output is correct
40 Correct 843 ms 103784 KB Output is correct
41 Correct 771 ms 97772 KB Output is correct
42 Correct 728 ms 97444 KB Output is correct
43 Correct 841 ms 100464 KB Output is correct
44 Correct 794 ms 97900 KB Output is correct
45 Correct 671 ms 96540 KB Output is correct
46 Correct 688 ms 96872 KB Output is correct
47 Correct 660 ms 98152 KB Output is correct
48 Correct 691 ms 98036 KB Output is correct
49 Correct 673 ms 98024 KB Output is correct
50 Correct 656 ms 97812 KB Output is correct
51 Correct 787 ms 101736 KB Output is correct
52 Correct 759 ms 101732 KB Output is correct