Submission #416801

# Submission time Handle Problem Language Result Execution time Memory
416801 2021-06-03T00:25:06 Z jainbot27 Inside information (BOI21_servers) C++17
50 / 100
372 ms 45876 KB
// Inside Information

/*
    how to solve queries of is A at B:

    basically we can just note that 
    we are looking at at a tree, find LCA of (A, B) 
    A has to be decreasing to whatever value we care about 
    B has to be increasing , max has to be less than time of query 

    O(nLOGn) or even constant with the other version of LCA that i dont know how to code

    also be careful of special cases on the LCA 
    
    how to solve the other kind of queries
    

*/


#include <bits/stdc++.h>
using namespace std;

using ll=long long;

#define FOR(i, a, b) for(int i=a; i<b; i++) 
#define ROF(i, a, b) for(int i=b-1; i>=a; i--)
#define F0R(i, n) FOR(i, 0, n) 
#define R0F(i, n) ROF(i, 0, n) 

#define f first
#define s second
#define pb push_back
#define siz(x) (int)x.size() 
#define ar array

const int mxN=2.4e5+10;
const int LG=20;

int n, q;
ar<int, 3> qq[mxN];
vector<pair<int, int>> adj[mxN];
int depth[mxN];
int anc[mxN][LG];
int mx[mxN][LG];

int mX[mxN], mN[mxN];

template<class T>void ckmax(T&A, T B){
    A=max(A, B);
}

void dfs(int u, int p, int c=-1){
    anc[u][0]=p; mx[u][0]=c;
    FOR(i, 1, LG){
        if(anc[u][i-1]==-1) anc[u][i]=-1, mx[u][i]=-1;
        else anc[u][i]=anc[anc[u][i-1]][i-1], mx[u][i]=max(mx[u][i-1], mx[anc[u][i-1]][i-1]);
    }
    for(auto[v, w]:adj[u]){
        if(v==p) continue;
        depth[v]=depth[u]+1;
        dfs(v, u, w);
    }
}

void dfs2(int u, int p, int L, int MX, int MN){
    mX[u]=MX, mN[u]=MN;
    for(auto[v, w]:adj[u]){
        if(v==p) continue;
        int nx=MX, ny=MN;
        if(w<L) nx=depth[u];
        if(w>L) ny=depth[u];
        dfs2(v, u, w, nx, ny);
    }
}

pair<int, int> lft(int u, int d){
    int maxx=0;
    for(int i=0; i<LG; i++){
        if(d&(1<<i)) ckmax(maxx, mx[u][i]), u=anc[u][i];
    }
    return {u, maxx};
}

pair<int, int> lca(int X, int Y){
    if(depth[X]<depth[Y]) swap(X, Y); 
    auto[XXX, maxx]=lft(X, depth[X]-depth[Y]);
    X=XXX;
    if(X==Y) return {X, maxx};
    R0F(i, LG){
        if(anc[X][i]!=anc[Y][i]){
            ckmax(maxx, mx[X][i]); 
            ckmax(maxx, mx[Y][i]); 
            X=anc[X][i]; Y=anc[Y][i];
        }
    }
    ckmax(maxx, mx[X][0]);
    ckmax(maxx, mx[Y][0]);
    return {anc[X][0], maxx};
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q;
    F0R(i, n+q-1){
        char T; cin >> T;
        if(T=='S'){
            int X, Y; cin >> X >> Y; X--; Y--; 
            adj[X].pb({Y, i}); 
            adj[Y].pb({X, i});
            qq[i]={1, X, Y};
        }
        else if(T=='Q'){
            int A, B; cin >> A >> B; A--; B--; 
            qq[i]={2, A, B};
        }
        else{
            assert(false);
            int C; cin >> C; C--; 
            qq[i]={3, C, -1};
        }
    }
    dfs(0, -1);
    dfs2(0, -1, 0, 0, 0);
    // F0R(i, n){
    //     // cout << mx[i][0] << ' ';
    //     cout << mX[i] << ' ' << mN[i] << "\n";
    // }
    // cout << "\n";
    F0R(i, n+q-1){
        if(qq[i][0]==2){
            int A=qq[i][1], B=qq[i][2];
            swap(A, B);
            auto[L, V]=lca(A, B);
            // cout << A << ' ' << B << ' ' << L << ' ' << V << ' ' << i << "\n";
            if(V>i){
                cout << "no\n"; 
                continue;
            }
            if(mN[A]<=depth[L]&&mX[B]<=depth[L]){
                if(A==L||B==L){
                    cout << "yes\n";
                }
                else{
                    int AA=lft(A, depth[A]-depth[L]-1).f, BB=lft(B, depth[B]-depth[L]-1).f;
                    if(mx[AA][0]<mx[BB][0]) cout << "yes\n";
                    else cout << "no\n";
                }
            }
            else
                cout << "no\n";
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7664 KB Output is correct
2 Correct 52 ms 9996 KB Output is correct
3 Correct 54 ms 9992 KB Output is correct
4 Correct 71 ms 10052 KB Output is correct
5 Correct 51 ms 10192 KB Output is correct
6 Correct 57 ms 10056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7664 KB Output is correct
2 Correct 52 ms 9996 KB Output is correct
3 Correct 54 ms 9992 KB Output is correct
4 Correct 71 ms 10052 KB Output is correct
5 Correct 51 ms 10192 KB Output is correct
6 Correct 57 ms 10056 KB Output is correct
7 Runtime error 15 ms 12044 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 7760 KB Output is correct
2 Correct 172 ms 36972 KB Output is correct
3 Correct 190 ms 36936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 7760 KB Output is correct
2 Correct 172 ms 36972 KB Output is correct
3 Correct 190 ms 36936 KB Output is correct
4 Runtime error 13 ms 12020 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 35 ms 7724 KB Output is correct
2 Correct 207 ms 42648 KB Output is correct
3 Correct 200 ms 42564 KB Output is correct
4 Correct 184 ms 42524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 7724 KB Output is correct
2 Correct 207 ms 42648 KB Output is correct
3 Correct 200 ms 42564 KB Output is correct
4 Correct 184 ms 42524 KB Output is correct
5 Runtime error 13 ms 11980 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7628 KB Output is correct
2 Correct 178 ms 37344 KB Output is correct
3 Correct 175 ms 37792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7628 KB Output is correct
2 Correct 178 ms 37344 KB Output is correct
3 Correct 175 ms 37792 KB Output is correct
4 Runtime error 15 ms 12108 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 7748 KB Output is correct
2 Correct 185 ms 42544 KB Output is correct
3 Correct 235 ms 42488 KB Output is correct
4 Correct 198 ms 42532 KB Output is correct
5 Correct 38 ms 7668 KB Output is correct
6 Correct 191 ms 37276 KB Output is correct
7 Correct 178 ms 37852 KB Output is correct
8 Correct 262 ms 38132 KB Output is correct
9 Correct 228 ms 38164 KB Output is correct
10 Correct 243 ms 42780 KB Output is correct
11 Correct 372 ms 41964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 7748 KB Output is correct
2 Correct 185 ms 42544 KB Output is correct
3 Correct 235 ms 42488 KB Output is correct
4 Correct 198 ms 42532 KB Output is correct
5 Correct 38 ms 7668 KB Output is correct
6 Correct 191 ms 37276 KB Output is correct
7 Correct 178 ms 37852 KB Output is correct
8 Correct 262 ms 38132 KB Output is correct
9 Correct 228 ms 38164 KB Output is correct
10 Correct 243 ms 42780 KB Output is correct
11 Correct 372 ms 41964 KB Output is correct
12 Runtime error 12 ms 12108 KB Execution killed with signal 6
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7788 KB Output is correct
2 Correct 54 ms 9980 KB Output is correct
3 Correct 53 ms 10028 KB Output is correct
4 Correct 58 ms 10120 KB Output is correct
5 Correct 51 ms 10180 KB Output is correct
6 Correct 56 ms 10052 KB Output is correct
7 Correct 40 ms 8644 KB Output is correct
8 Correct 171 ms 36920 KB Output is correct
9 Correct 174 ms 37048 KB Output is correct
10 Correct 36 ms 8596 KB Output is correct
11 Correct 191 ms 45788 KB Output is correct
12 Correct 173 ms 45876 KB Output is correct
13 Correct 191 ms 45708 KB Output is correct
14 Correct 39 ms 8632 KB Output is correct
15 Correct 160 ms 37280 KB Output is correct
16 Correct 166 ms 37792 KB Output is correct
17 Correct 248 ms 38168 KB Output is correct
18 Correct 221 ms 38152 KB Output is correct
19 Correct 233 ms 42764 KB Output is correct
20 Correct 321 ms 41984 KB Output is correct
21 Correct 182 ms 37356 KB Output is correct
22 Correct 187 ms 37620 KB Output is correct
23 Correct 218 ms 37684 KB Output is correct
24 Correct 227 ms 37764 KB Output is correct
25 Correct 221 ms 39600 KB Output is correct
26 Correct 188 ms 37336 KB Output is correct
27 Correct 196 ms 37468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 7788 KB Output is correct
2 Correct 54 ms 9980 KB Output is correct
3 Correct 53 ms 10028 KB Output is correct
4 Correct 58 ms 10120 KB Output is correct
5 Correct 51 ms 10180 KB Output is correct
6 Correct 56 ms 10052 KB Output is correct
7 Correct 40 ms 8644 KB Output is correct
8 Correct 171 ms 36920 KB Output is correct
9 Correct 174 ms 37048 KB Output is correct
10 Correct 36 ms 8596 KB Output is correct
11 Correct 191 ms 45788 KB Output is correct
12 Correct 173 ms 45876 KB Output is correct
13 Correct 191 ms 45708 KB Output is correct
14 Correct 39 ms 8632 KB Output is correct
15 Correct 160 ms 37280 KB Output is correct
16 Correct 166 ms 37792 KB Output is correct
17 Correct 248 ms 38168 KB Output is correct
18 Correct 221 ms 38152 KB Output is correct
19 Correct 233 ms 42764 KB Output is correct
20 Correct 321 ms 41984 KB Output is correct
21 Correct 182 ms 37356 KB Output is correct
22 Correct 187 ms 37620 KB Output is correct
23 Correct 218 ms 37684 KB Output is correct
24 Correct 227 ms 37764 KB Output is correct
25 Correct 221 ms 39600 KB Output is correct
26 Correct 188 ms 37336 KB Output is correct
27 Correct 196 ms 37468 KB Output is correct
28 Runtime error 12 ms 12092 KB Execution killed with signal 6
29 Halted 0 ms 0 KB -