답안 #968291

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968291 2024-04-23T09:37:51 Z VinhLuu 늑대인간 (IOI18_werewolf) C++17
100 / 100
706 ms 227832 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
//const int mod = 1e9 + 7;
//const int oo = 1e9;

int n, q, cal[N], ans[N];
vector<int> p[N];

struct tree{
    int pa[N], sz[N], Min[N], Max[N], in[N], en[N], demin, be[N], f[N][22];
    vector<int> g[N];
    void pre(){
        for(int i = 1; i <= n; i ++) pa[i] = i, sz[i] = 1, Max[i] = Min[i] = i;
    }
    int fin(int u){
        return pa[u] == u ? u : pa[u] = fin(pa[u]);
    }
    bool dsu(int x,int y){
        x = fin(x);
        y = fin(y);
        if(x == y) return 0;
        if(sz[x] < sz[y]) swap(x, y);
        sz[x] += sz[y];
        pa[y] = x;
        Max[x] = max(Max[x], Max[y]);
        Min[x] = min(Min[x], Min[y]);
        return 1;
    }
    void dfs(int u,int v){
        in[u] = ++demin;
        if(v == 0) for(int i = 0; i <= 18; i ++) f[u][i] = u;
        else{
            f[u][0] = v;
            for(int i = 1; i <= 18; i ++) f[u][i] = f[f[u][i - 1]][i - 1];
        }
        be[demin] = u;
        for(auto j : g[u]){
            if(j == v) continue;
            dfs(j, u);
        }
        en[u] = demin;
    }
    bool kt(int u,int v){
        return in[u] <= in[v] && in[v] <= en[u];
    }
    int Find(int type, int x,int lim){
        if(type == 0){
            int kq = x;
            for(int i = 18; i >= 0; i --) if(f[x][i] <= lim){
                kq = f[x][i];
                x = f[x][i];
            }
            return kq;
        }else{
            int kq = x;
            for(int i = 18; i >= 0; i --) if(f[x][i] >= lim){
                kq = f[x][i];
                x = f[x][i];
            }
            return kq;
        }
    }
} T[2];

int st[N << 1];

void update(int x){
    x += n - 1;
    st[x]++;
    while(x > 1){
        x /= 2;
        st[x]++;
    }
}

int get(int l,int r){
    r++;
    int ret = 0;
    for(l += n - 1, r += n - 1; l < r; l /= 2, r /= 2){
        if(l & 1) ret += st[l++];
        if(r & 1) ret += st[--r];
    }
    return ret;
}

vector<pii> open[N], close[N];

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, q = L.size();
    for(int i = 0; i < X.size(); i ++){
        int x = ++X[i], y = ++Y[i];
        p[x].pb(y);
        p[y].pb(x);
    }
    T[0].pre(), T[1].pre();
    for(int i = 1; i <= n; i ++) for(auto j : p[i]) if(j < i){
        int tmp = T[0].Max[T[0].fin(j)];
        if(T[0].dsu(i, j)){
            T[0].g[i].pb(tmp);
        }
    }
    T[0].dfs(n, 0);
    for(int i = n; i >= 1; i --) for(auto j : p[i]) if(j > i){
        int tmp = T[1].Min[T[1].fin(j)];
        if(T[1].dsu(i, j)){
            T[1].g[i].pb(tmp);
        }
    }
    T[1].dfs(1, 0);
    for(int i = 0; i < q; i ++){
        int s = ++S[i], e = ++E[i], l = ++L[i], r = ++R[i];
        if(e > R[i] || s < l){
            ans[i] = 0;
            continue;
        }
        int ce = T[0].Find(0, e, r), cs = T[1].Find(1, s, l);
        open[T[0].in[ce] - 1].pb({i, cs});
        close[T[0].en[ce]].pb({i, cs});
    }

    for(int i = 1; i <= n; i ++){
        int u = T[0].be[i];
        update(T[1].in[u]);
        for(auto [id, j] : open[i]) cal[id] -= get(T[1].in[j], T[1].en[j]);

        for(auto [id, j] : close[i]) cal[id] += get(T[1].in[j], T[1].en[j]);

    }
    vector<int> out;
    for(int i = 0; i < q; i ++){
//        cerr << cal[i] << " ";
        if(!cal[i]) out.pb(0);
        else out.pb(1);
    }
//    cerr << "h\n";
    return out;
}

//#define lpv
#ifdef lpv

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }

    vector<int> x, y, st, ed, lf, rt;
    int n, m, q; cin >> n >> m >> q;
    for(int i = 1; i <= m; i ++){
        int gg, hh; cin >> gg >> hh;
        x.pb(gg),y.pb(hh);
    }

    for(int i = 1; i <= q; i ++){
        int g, h, j, k; cin >> g >> h >> j >> k;
        st.pb(g), ed.pb(h), lf.pb(j), rt.pb(k);
    }
    vector<int> oo = check_validity(n,x,y,st,ed,lf,rt);
    for(auto j : oo) cout << j << "\n";
}

#endif // lpv

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: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 i = 0; i < X.size(); i ++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 123480 KB Output is correct
2 Correct 53 ms 123676 KB Output is correct
3 Correct 48 ms 123464 KB Output is correct
4 Correct 52 ms 123256 KB Output is correct
5 Correct 48 ms 123476 KB Output is correct
6 Correct 48 ms 123316 KB Output is correct
7 Correct 49 ms 123316 KB Output is correct
8 Correct 49 ms 123480 KB Output is correct
9 Correct 54 ms 123472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 123480 KB Output is correct
2 Correct 53 ms 123676 KB Output is correct
3 Correct 48 ms 123464 KB Output is correct
4 Correct 52 ms 123256 KB Output is correct
5 Correct 48 ms 123476 KB Output is correct
6 Correct 48 ms 123316 KB Output is correct
7 Correct 49 ms 123316 KB Output is correct
8 Correct 49 ms 123480 KB Output is correct
9 Correct 54 ms 123472 KB Output is correct
10 Correct 51 ms 124756 KB Output is correct
11 Correct 55 ms 124764 KB Output is correct
12 Correct 51 ms 124500 KB Output is correct
13 Correct 52 ms 124764 KB Output is correct
14 Correct 58 ms 124748 KB Output is correct
15 Correct 59 ms 124752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 519 ms 210296 KB Output is correct
2 Correct 519 ms 211856 KB Output is correct
3 Correct 459 ms 209676 KB Output is correct
4 Correct 414 ms 209068 KB Output is correct
5 Correct 426 ms 210380 KB Output is correct
6 Correct 485 ms 209616 KB Output is correct
7 Correct 439 ms 208764 KB Output is correct
8 Correct 514 ms 211952 KB Output is correct
9 Correct 372 ms 210124 KB Output is correct
10 Correct 334 ms 208140 KB Output is correct
11 Correct 353 ms 208992 KB Output is correct
12 Correct 343 ms 208496 KB Output is correct
13 Correct 697 ms 217152 KB Output is correct
14 Correct 619 ms 217340 KB Output is correct
15 Correct 706 ms 217268 KB Output is correct
16 Correct 658 ms 218084 KB Output is correct
17 Correct 435 ms 208468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 123480 KB Output is correct
2 Correct 53 ms 123676 KB Output is correct
3 Correct 48 ms 123464 KB Output is correct
4 Correct 52 ms 123256 KB Output is correct
5 Correct 48 ms 123476 KB Output is correct
6 Correct 48 ms 123316 KB Output is correct
7 Correct 49 ms 123316 KB Output is correct
8 Correct 49 ms 123480 KB Output is correct
9 Correct 54 ms 123472 KB Output is correct
10 Correct 51 ms 124756 KB Output is correct
11 Correct 55 ms 124764 KB Output is correct
12 Correct 51 ms 124500 KB Output is correct
13 Correct 52 ms 124764 KB Output is correct
14 Correct 58 ms 124748 KB Output is correct
15 Correct 59 ms 124752 KB Output is correct
16 Correct 519 ms 210296 KB Output is correct
17 Correct 519 ms 211856 KB Output is correct
18 Correct 459 ms 209676 KB Output is correct
19 Correct 414 ms 209068 KB Output is correct
20 Correct 426 ms 210380 KB Output is correct
21 Correct 485 ms 209616 KB Output is correct
22 Correct 439 ms 208764 KB Output is correct
23 Correct 514 ms 211952 KB Output is correct
24 Correct 372 ms 210124 KB Output is correct
25 Correct 334 ms 208140 KB Output is correct
26 Correct 353 ms 208992 KB Output is correct
27 Correct 343 ms 208496 KB Output is correct
28 Correct 697 ms 217152 KB Output is correct
29 Correct 619 ms 217340 KB Output is correct
30 Correct 706 ms 217268 KB Output is correct
31 Correct 658 ms 218084 KB Output is correct
32 Correct 435 ms 208468 KB Output is correct
33 Correct 552 ms 210500 KB Output is correct
34 Correct 272 ms 160328 KB Output is correct
35 Correct 633 ms 212960 KB Output is correct
36 Correct 561 ms 210888 KB Output is correct
37 Correct 576 ms 215924 KB Output is correct
38 Correct 543 ms 211412 KB Output is correct
39 Correct 507 ms 218048 KB Output is correct
40 Correct 539 ms 218560 KB Output is correct
41 Correct 485 ms 210004 KB Output is correct
42 Correct 366 ms 208908 KB Output is correct
43 Correct 571 ms 217104 KB Output is correct
44 Correct 496 ms 210980 KB Output is correct
45 Correct 489 ms 219864 KB Output is correct
46 Correct 465 ms 218804 KB Output is correct
47 Correct 684 ms 221020 KB Output is correct
48 Correct 661 ms 227832 KB Output is correct
49 Correct 691 ms 217336 KB Output is correct
50 Correct 645 ms 217384 KB Output is correct
51 Correct 613 ms 216344 KB Output is correct
52 Correct 570 ms 218352 KB Output is correct