답안 #412902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412902 2021-05-27T18:52:31 Z mat_v 늑대인간 (IOI18_werewolf) C++14
100 / 100
1804 ms 142748 KB
#include "werewolf.h"
#include <bits/stdc++.h>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define pb push_back

using namespace std;

int n;
int m;
int q;
vector<int> graf[200005];
vector<int> manji[200005];
int cale[200005][2];
int lca[200005][20][2];
vector<int> veci[200005];
int disc[200005][2];
int out[200005][2];
int dsu[200005];
int sajz[200005];
int findpar(int x){
    if(x == dsu[x])return x;
    return dsu[x] = findpar(dsu[x]);
}
void unite(int a, int b, int idx){
    a = findpar(a);
    b = findpar(b);
    if(a == b)return;
    if(idx == 0){
        if(a < b)dsu[b] = a;
        else dsu[a] = b;
    }
    else{
        if(a < b)dsu[a] = b;
        else dsu[b] = a;
    }
}

int br = 1;
void dfs1(int x){
    disc[x][0] = br;
    lca[x][0][0] = cale[x][0];
    ff(j,1,18){
        lca[x][j][0] = lca[lca[x][j - 1][0]][j - 1][0];
    }
    for(auto c:manji[x]){
        br++;
        dfs1(c);
    }
    out[x][0] = br;
}
void dfs2(int x){
    disc[x][1] = br;
    lca[x][0][1] = cale[x][1];
    ff(j,1,18){
        lca[x][j][1] = lca[lca[x][j - 1][1]][j - 1][1];
    }
    for(auto c:veci[x]){
        br++;
        dfs2(c);
    }
    out[x][1] = br;
}

bool insubtr(int x, int y, int idx){
    ///y u x
    if(disc[y][idx] >= disc[x][idx] && disc[y][idx] <= out[x][idx])return 1;
    return 0;
}

int val[200005];
vector<int> mgs[4 * 200001];

void spoji(int x, int y, int z){
    int l1 = mgs[x].size();
    int l2 = mgs[y].size();
    int br1 = 0, br2 = 0;
    while(br1 < l1 || br2 < l2){
        if(br1 == l1){
            mgs[z].pb(mgs[y][br2]);
            br2++;
            continue;
        }
        if(br2 == l2){
            mgs[z].pb(mgs[x][br1]);
            br1++;
            continue;
        }
        if(mgs[x][br1] < mgs[y][br2]){
            mgs[z].pb(mgs[x][br1]);
            br1++;
        }
        else{
            mgs[z].pb(mgs[y][br2]);
            br2++;
        }
    }
}

void init(int node, int l, int r){
    if(l == r){
        mgs[node].pb(val[l]);
        return;
    }
    int mid = (l + r) / 2;
    init(node * 2, l, mid);
    init(node * 2 + 1, mid + 1, r);
    spoji(node * 2, node * 2 + 1, node);
}

int kolko(int node, int mv, int vv){
    int vel = mgs[node].size();
    int prvi = 0;
    if(mgs[node][vel - 1] < mv)return 0;
    int l = 0, r = vel-1;
    while(l < r){
        int mid = (l + r) / 2;
        if(mgs[node][mid] >= mv)r = mid;
        else l = mid + 1;
    }
    prvi = l;
    if(mgs[node][0] > vv)return 0;
    l = 0, r = vel - 1;
    while(l < r){
        int mid = (l + r + 1) / 2;
        if(mgs[node][mid] <= vv)l = mid;
        else r = mid - 1;
    }
    if(prvi <= l)return 1;
    return 0;
}

int kveri(int node, int l, int r, int levo, int desno, int mv, int vv){
    if(l > r || levo > desno)return 0;
    if(levo > r || l > desno)return 0;
    if(l >= levo && r <= desno){
        return kolko(node, mv, vv);
    }
    int mid = (l + r) / 2;
    return kveri(node * 2, l, mid, levo, desno, mv, vv) + kveri(node * 2 + 1, mid + 1, r, levo, desno, mv, vv);
}

std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R) {

    n = N;
    m = X.size();
    ff(i,0,m - 1){
        graf[X[i] + 1].pb(Y[i] + 1);
        graf[Y[i] + 1].pb(X[i] + 1);
    }
    ff(i,1,n){
        dsu[i] = i;
        sajz[i] = 1;
        sort(graf[i].begin(), graf[i].end());
    }
    fb(i,n,1){
        for(auto c:graf[i]){
            if(c > i){
                if(findpar(i) == findpar(c))continue;
                int koji = findpar(c);
                manji[i].pb(koji);
                cale[koji][0] = i;
                unite(i,c,0);
            }
        }
    }
    br = 1;
    dfs1(1);

//    ff(i,1,n){
//        cout << manji[i].size() << " ";
//        for(auto c:manji[i])cout << c << " ";
//        cout << "\n";
//    }
//    exit(0);

    ff(i,1,n){
        dsu[i] = i;
        sajz[i] = 1;
        reverse(graf[i].begin(), graf[i].end());
    }

    ff(i,1,n){
        for(auto c:graf[i]){
            if(c < i){
                if(findpar(c) == findpar(i))continue;
                int koji = findpar(c);
                veci[i].pb(koji);
                cale[koji][1] = i;
                unite(i,c,1);
            }
        }
    }
    br = 1;
    dfs2(n);
    ff(i,1,n){
        val[disc[i][1]] = disc[i][0];
    }
    init(1,1,n);
    q = S.size();
    vector<int> ans;
    ff(i,0,q - 1){
        S[i]++;
        E[i]++;
        L[i]++;
        R[i]++;
       // cout << S[i] << " " << E[i] << " " << L[i] << " " << R[i] << "\n";
        int p1 = S[i];
        fb(j,18,0){
            int koji = lca[p1][j][0];
            if(koji > 0 && koji >= L[i])p1 = koji;
        }
        int l1 = disc[p1][0], r1 = out[p1][0];


        int p2 = E[i];
        fb(j,18,0){
            int koji = lca[p2][j][1];
            if(koji > 0 && koji <= R[i])p2 = koji;
        }
        int l2 = disc[p2][1], r2 = out[p2][1];

        int odg = 0;
        if(kveri(1,1,n,l2,r2,l1,r1))odg = 1;

        ans.pb(odg);
    }
    return ans;
}
/*
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 'void dfs1(int)':
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:43:5: note: in expansion of macro 'ff'
   43 |     ff(j,1,18){
      |     ^~
werewolf.cpp: In function 'void dfs2(int)':
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:55:5: note: in expansion of macro 'ff'
   55 |     ff(j,1,18){
      |     ^~
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:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:149:5: note: in expansion of macro 'ff'
  149 |     ff(i,0,m - 1){
      |     ^~
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:153:5: note: in expansion of macro 'ff'
  153 |     ff(i,1,n){
      |     ^~
werewolf.cpp:4:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
werewolf.cpp:158:5: note: in expansion of macro 'fb'
  158 |     fb(i,n,1){
      |     ^~
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:179:5: note: in expansion of macro 'ff'
  179 |     ff(i,1,n){
      |     ^~
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:185:5: note: in expansion of macro 'ff'
  185 |     ff(i,1,n){
      |     ^~
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:198:5: note: in expansion of macro 'ff'
  198 |     ff(i,1,n){
      |     ^~
werewolf.cpp:3:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
werewolf.cpp:204:5: note: in expansion of macro 'ff'
  204 |     ff(i,0,q - 1){
      |     ^~
werewolf.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
werewolf.cpp:211:9: note: in expansion of macro 'fb'
  211 |         fb(j,18,0){
      |         ^~
werewolf.cpp:4:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    4 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
werewolf.cpp:219:9: note: in expansion of macro 'fb'
  219 |         fb(j,18,0){
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 19 ms 33228 KB Output is correct
3 Correct 18 ms 33196 KB Output is correct
4 Correct 18 ms 33132 KB Output is correct
5 Correct 19 ms 33252 KB Output is correct
6 Correct 19 ms 33136 KB Output is correct
7 Correct 18 ms 33220 KB Output is correct
8 Correct 20 ms 33204 KB Output is correct
9 Correct 21 ms 33252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 19 ms 33228 KB Output is correct
3 Correct 18 ms 33196 KB Output is correct
4 Correct 18 ms 33132 KB Output is correct
5 Correct 19 ms 33252 KB Output is correct
6 Correct 19 ms 33136 KB Output is correct
7 Correct 18 ms 33220 KB Output is correct
8 Correct 20 ms 33204 KB Output is correct
9 Correct 21 ms 33252 KB Output is correct
10 Correct 27 ms 34552 KB Output is correct
11 Correct 27 ms 34588 KB Output is correct
12 Correct 25 ms 34508 KB Output is correct
13 Correct 28 ms 34704 KB Output is correct
14 Correct 26 ms 34676 KB Output is correct
15 Correct 27 ms 34628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 911 ms 124244 KB Output is correct
2 Correct 1485 ms 126592 KB Output is correct
3 Correct 1480 ms 133216 KB Output is correct
4 Correct 1162 ms 132516 KB Output is correct
5 Correct 1180 ms 132612 KB Output is correct
6 Correct 1054 ms 132408 KB Output is correct
7 Correct 832 ms 132360 KB Output is correct
8 Correct 1395 ms 134784 KB Output is correct
9 Correct 1093 ms 133304 KB Output is correct
10 Correct 664 ms 132608 KB Output is correct
11 Correct 681 ms 132568 KB Output is correct
12 Correct 830 ms 132432 KB Output is correct
13 Correct 1163 ms 139808 KB Output is correct
14 Correct 1213 ms 139764 KB Output is correct
15 Correct 1157 ms 139844 KB Output is correct
16 Correct 1230 ms 139748 KB Output is correct
17 Correct 813 ms 132488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33228 KB Output is correct
2 Correct 19 ms 33228 KB Output is correct
3 Correct 18 ms 33196 KB Output is correct
4 Correct 18 ms 33132 KB Output is correct
5 Correct 19 ms 33252 KB Output is correct
6 Correct 19 ms 33136 KB Output is correct
7 Correct 18 ms 33220 KB Output is correct
8 Correct 20 ms 33204 KB Output is correct
9 Correct 21 ms 33252 KB Output is correct
10 Correct 27 ms 34552 KB Output is correct
11 Correct 27 ms 34588 KB Output is correct
12 Correct 25 ms 34508 KB Output is correct
13 Correct 28 ms 34704 KB Output is correct
14 Correct 26 ms 34676 KB Output is correct
15 Correct 27 ms 34628 KB Output is correct
16 Correct 911 ms 124244 KB Output is correct
17 Correct 1485 ms 126592 KB Output is correct
18 Correct 1480 ms 133216 KB Output is correct
19 Correct 1162 ms 132516 KB Output is correct
20 Correct 1180 ms 132612 KB Output is correct
21 Correct 1054 ms 132408 KB Output is correct
22 Correct 832 ms 132360 KB Output is correct
23 Correct 1395 ms 134784 KB Output is correct
24 Correct 1093 ms 133304 KB Output is correct
25 Correct 664 ms 132608 KB Output is correct
26 Correct 681 ms 132568 KB Output is correct
27 Correct 830 ms 132432 KB Output is correct
28 Correct 1163 ms 139808 KB Output is correct
29 Correct 1213 ms 139764 KB Output is correct
30 Correct 1157 ms 139844 KB Output is correct
31 Correct 1230 ms 139748 KB Output is correct
32 Correct 813 ms 132488 KB Output is correct
33 Correct 1245 ms 132596 KB Output is correct
34 Correct 450 ms 65100 KB Output is correct
35 Correct 1611 ms 134764 KB Output is correct
36 Correct 1156 ms 133016 KB Output is correct
37 Correct 1567 ms 134056 KB Output is correct
38 Correct 1280 ms 133436 KB Output is correct
39 Correct 1424 ms 140404 KB Output is correct
40 Correct 1248 ms 142748 KB Output is correct
41 Correct 1213 ms 133488 KB Output is correct
42 Correct 724 ms 133072 KB Output is correct
43 Correct 1804 ms 139476 KB Output is correct
44 Correct 1466 ms 134100 KB Output is correct
45 Correct 1059 ms 140512 KB Output is correct
46 Correct 1174 ms 140148 KB Output is correct
47 Correct 1185 ms 139980 KB Output is correct
48 Correct 1184 ms 139812 KB Output is correct
49 Correct 1240 ms 139832 KB Output is correct
50 Correct 1195 ms 139844 KB Output is correct
51 Correct 1134 ms 142576 KB Output is correct
52 Correct 1102 ms 142536 KB Output is correct