Submission #554175

# Submission time Handle Problem Language Result Execution time Memory
554175 2022-04-27T18:42:04 Z alontanay Inside information (BOI21_servers) C++14
60 / 100
253 ms 44748 KB
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define debug(n) cout << n << endl;
using namespace std;

struct Seg {
    vector<int> seg;
    int n;
    Seg(int n): n(n) {
        seg.resize(2*n);
    }
    void update(int i, int x) {
        i += n;
        seg[i] += x;
        for(i >>= 1; i; i >>= 1) {
            seg[i] = seg[i<<1] + seg[(i<<1)|1];
        }
    }
    int query(int a, int b) {
        int res = 0;
        for(a += n, b += n; a < b; a >>= 1, b >>= 1) {
            if(a&1) {
                res += seg[a++];
            }
            if(b&1) {
                res += seg[--b];
            }
        }
        return res;
    }
};

const int mxN = 120005;

int n, k, q;
vector<pair<char,vector<int>>> input;

//
int cnt[mxN];
bool ser[4002][4002];

struct SOL1 {

    SOL1() {
        for(int i = 0; i < 4002; i ++) {
            ser[i][i] = true;
            cnt[i] = 1;
        }
    }

    void Share(int a, int b) {
        for(int d = 1; d < 4002; d ++) {
            if(ser[a][d] ^ ser[b][d]) {
                cnt[d] ++;
                ser[a][d] = ser[b][d] = true;
            }
        }
    }

    bool Query(int a, int d) {
        return ser[a][d];
    }

    int Count(int d) {
        return cnt[d];
    }

};

ll date[120005];
struct SOL2 {
    int cur_date = 1;

    SOL2() {
        date[1] = 1;
    }

    void Share(int a, int b) {
        if(a == 1) { swap(a,b); }
        date[a] = cur_date ++;
    }

    bool Query(int a, int d) {
        if(a == d) { return true; }
        if(date[a]*date[d] == 0) { return false; }
        if(a == 1 || d == 1) {
            return true;
        }
        return date[d] < date[a];
    }

    int Count(int d) {
        if(date[d] == 0) { return 1;}
        if(d == 1) {
            return cur_date;
        }
        return cur_date - date[d] + 1;
    }
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vector<int> nei[mxN];
int sub[mxN];
int idx[mxN];
int sup[mxN][17];
int depth[mxN];
int dir[mxN];
int cidx = 0;
Seg segUp(mxN);
Seg segDn(mxN);

struct SOLG {
    int cur_date = 1;
    int dfs(int node, int par = -1) {
        idx[node] = cidx ++;
        int sz = 1;
        for(int ne : nei[node]) {
            if(par == ne) { continue; }
            
            depth[ne] = depth[node] + 1;
            sup[ne][0] = node;

            int cur = dfs(ne,node);
            sz += cur;
        }
        return sub[node] = sz;
    }
    int lift(int node, int d) {
        for(int p = 0; p < 17; p ++) {
            if(d&1) { node = sup[node][p]; }
            d /= 2;
        }
        return node;
    }
    void level(int &a, int &b) {
        if(depth[a] < depth[b]) {
            swap(a,b);
        }
        int dif = depth[a] - depth[b];
        a = lift(a,dif);
    }
    int lca(int a, int b) {
        level(a,b);
        if(a == b) { return a; }
        for(int i = 16; i >= 0; i --) {
            if(sup[a][i] != sup[b][i]) {
                a = sup[a][i];
                b = sup[b][i];
            }
        }
        return sup[a][0];
    }
    SOLG() {
        sup[1][0] = 1;
        dfs(1);
        for(int p = 1; p < 17; p ++) {
            for(int i = 1; i <= n; i ++) {
                sup[i][p] = sup[sup[i][p-1]][p-1];
            }
        }
    }

    void Share(int a, int b) {
        if(depth[a] >= depth[b]) { swap(a,b); }
        if(date[a]) {
            segDn.update(idx[b],1);
            segDn.update(idx[b]+sub[b],-1);
            dir[b] = 2;
        }
        for(int ne : nei[b]) {
            if(ne == a) { continue; }
            if(date[ne]) {
                segUp.update(idx[ne],1);
                segUp.update(idx[ne]+sub[ne],-1);
                dir[ne] = 1;
            }
        }
        date[b] = cur_date++;
    }
    bool qUP(int b, int u) {
        int cnt = segUp.query(0,idx[b]+1) - segUp.query(0,idx[u]+1);
        return (cnt == depth[b]-depth[u]);
    }
    bool qDN(int b, int u) {
        int cnt = segDn.query(0,idx[b]+1) - segDn.query(0,idx[u]+1);
        return (cnt == depth[b]-depth[u]);
    }
    bool Query(int a, int d) {
        if(a == d) { return true; }
        if(sup[d][0] == a) { return date[d]; }
        if(sup[a][0] == d) { return date[a]; }

        int c = lca(a,d);
        if(c == a) {
            return qUP(d,lift(d,depth[d]-depth[c]-1));
        }
        if(c == d) {
            return qDN(a,lift(a,depth[a]-depth[c]-1));
        }
        int ca = lift(a,depth[a]-depth[c]-1);
        int cd = lift(d,depth[d]-depth[c]-1);
        bool upSide = qUP(d,cd);
        bool dnSide = qDN(a,ca);
        bool sw = (0 < date[cd] && date[cd] < date[ca]);
        

        return upSide && dnSide && sw;
    }

    int Count(int d) {
        return 0;
    }
};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void solve1() {
    SOL1 sol1;
    for(pair<char,vector<int>> &Q : input) {
        char t = Q.f;
        int a = Q.s[0], b;
        if(Q.s.size() > 1) { b = Q.s[1]; }
        if(t == 'S') {
            sol1.Share(a,b);
        } else if(t == 'Q') {
            cout << (sol1.Query(a,b) ? "yes\n" : "no\n");
        } else {
            cout << sol1.Count(a) << "\n";
        }
    }
}

void solve2() {
    SOL2 sol2;
    for(pair<char,vector<int>> &Q : input) {
        char t = Q.f;
        int a = Q.s[0], b;
        if(Q.s.size() > 1) { b = Q.s[1]; }
        if(t == 'S') {
            sol2.Share(a,b);
        } else if(t == 'Q') {
            cout << (sol2.Query(a,b) ? "yes\n" : "no\n");
        } else {
            cout << sol2.Count(a) << "\n";
        }
    }
}

int LL[mxN];
int RR[mxN];
void solve3() {
    SOLG solG;
    for(int i = 1 ; i<= n; i ++) {
        LL[i] = RR[i] = i;
        cnt[i] = 1;
    }
    Seg seg(mxN);
    seg.update(1,1);
    for(pair<char,vector<int>> &Q : input) {
        char t = Q.f;
        int a = Q.s[0], b;
        if(Q.s.size() > 1) { b = Q.s[1]; }
        if(t == 'S') {
            solG.Share(a,b);
            int nl = min(LL[a],LL[b]);
            int nr = max(RR[a],RR[b]);
            LL[a] = LL[b] = nl;
            RR[a] = RR[b] = nr;
            seg.update(nl,1);
            seg.update(nr+1,-1);
        } else if(t == 'Q') {
            cout << (solG.Query(a,b) ? "yes\n" : "no\n");
        } else {
            cout << seg.query(0,a+1) << "\n";
        }
    }
}


void solveG() {
    SOLG solG;
    for(pair<char,vector<int>> &Q : input) {
        char t = Q.f;
        int a = Q.s[0], b;
        if(Q.s.size() > 1) { b = Q.s[1]; }
        if(t == 'S') {
            solG.Share(a,b);
        } else if(t == 'Q') {
            cout << (solG.Query(a,b) ? "yes\n" : "no\n");
        } else {
            cout << solG.Count(a) << "\n";
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;
    q = n + k - 1;
    input.resize(q);
    bool sub2 = true;
    bool sub3 = true;
    for(int i = 0; i < q; i ++) {
        char t;
        cin >> t;
        if(t == 'C') {
            int a;
            cin >> a;
            input[i] = {'C',{a}};
        } else if(t == 'S') {
            int a,b;
            cin >> a >> b;
            if(a != 1 && b != 1) {
                sub2 = false;
            }
            if(abs(a-b) != 1) {
                sub3 = false;
            }
            nei[a].push_back(b);
            nei[b].push_back(a);
            input[i] = {t,{a,b}};
        } else {
            int a, b;
            cin >> a >> b;
            input[i] = {t,{a,b}};
        }
    }
    if(n <= 4000) {
        solve1();
    } else if(sub2) {
        solve2();
    } else if(sub3) {
        solve3();
    } else {
        solveG();
    }
    return 0;
}

/*
3 18
Q 1 2
Q 2 1
Q 1 3
Q 3 1
Q 2 3
Q 3 2
S 1 3
Q 1 2
Q 2 1
Q 1 3
Q 3 1
Q 2 3
Q 3 2
S 1 2
Q 1 3
Q 3 1
Q 1 2
Q 2 1
Q 2 3
Q 3 2
*/

/*
4
Q 1 2
Q 2 1
Q 1 3
Q 3 1
Q 1 4
Q 4 1
Q 2 3
Q 3 2
Q 2 4
Q 4 2
Q 3 4
Q 4 3
*/

/*
4 16
C 1
C 2
C 3
C 4
S 4 3
C 1
C 2
C 3
C 4
S 2 1
C 1
C 2
C 3
C 4
S 3 2
C 1
C 2
C 3
C 4
*/

Compilation message

servers.cpp: In function 'void solve1()':
servers.cpp:225:25: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  225 |         int a = Q.s[0], b;
      |                         ^
servers.cpp: In function 'void solve2()':
servers.cpp:87:26: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
   87 |         if(date[a]*date[d] == 0) { return false; }
      |                    ~~~~~~^
servers.cpp:241:25: note: 'b' was declared here
  241 |         int a = Q.s[0], b;
      |                         ^
servers.cpp: In function 'void solveG()':
servers.cpp:293:32: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
  293 |             cout << (solG.Query(a,b) ? "yes\n" : "no\n");
      |                      ~~~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 36 ms 28628 KB Output is correct
2 Correct 64 ms 28976 KB Output is correct
3 Correct 63 ms 28944 KB Output is correct
4 Correct 63 ms 28936 KB Output is correct
5 Correct 59 ms 28988 KB Output is correct
6 Correct 83 ms 29064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 28628 KB Output is correct
2 Correct 64 ms 28976 KB Output is correct
3 Correct 63 ms 28944 KB Output is correct
4 Correct 63 ms 28936 KB Output is correct
5 Correct 59 ms 28988 KB Output is correct
6 Correct 83 ms 29064 KB Output is correct
7 Correct 38 ms 28556 KB Output is correct
8 Correct 59 ms 28808 KB Output is correct
9 Correct 65 ms 29040 KB Output is correct
10 Correct 57 ms 28904 KB Output is correct
11 Correct 59 ms 28968 KB Output is correct
12 Correct 80 ms 29100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 28584 KB Output is correct
2 Correct 85 ms 25564 KB Output is correct
3 Correct 91 ms 25560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 28584 KB Output is correct
2 Correct 85 ms 25564 KB Output is correct
3 Correct 91 ms 25560 KB Output is correct
4 Correct 40 ms 28624 KB Output is correct
5 Correct 86 ms 25608 KB Output is correct
6 Correct 81 ms 26012 KB Output is correct
7 Correct 78 ms 25804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28628 KB Output is correct
2 Correct 209 ms 41732 KB Output is correct
3 Correct 212 ms 42844 KB Output is correct
4 Correct 168 ms 42776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28628 KB Output is correct
2 Correct 209 ms 41732 KB Output is correct
3 Correct 212 ms 42844 KB Output is correct
4 Correct 168 ms 42776 KB Output is correct
5 Correct 36 ms 29512 KB Output is correct
6 Correct 210 ms 44448 KB Output is correct
7 Correct 168 ms 44744 KB Output is correct
8 Correct 191 ms 44212 KB Output is correct
9 Correct 189 ms 44208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 28504 KB Output is correct
2 Correct 162 ms 34940 KB Output is correct
3 Correct 185 ms 35016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 28504 KB Output is correct
2 Correct 162 ms 34940 KB Output is correct
3 Correct 185 ms 35016 KB Output is correct
4 Correct 37 ms 28504 KB Output is correct
5 Incorrect 132 ms 34900 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28500 KB Output is correct
2 Correct 221 ms 41744 KB Output is correct
3 Correct 212 ms 42736 KB Output is correct
4 Correct 171 ms 42812 KB Output is correct
5 Correct 34 ms 29388 KB Output is correct
6 Correct 165 ms 36000 KB Output is correct
7 Correct 181 ms 36008 KB Output is correct
8 Correct 240 ms 36444 KB Output is correct
9 Correct 208 ms 36544 KB Output is correct
10 Correct 187 ms 37644 KB Output is correct
11 Correct 251 ms 37376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28500 KB Output is correct
2 Correct 221 ms 41744 KB Output is correct
3 Correct 212 ms 42736 KB Output is correct
4 Correct 171 ms 42812 KB Output is correct
5 Correct 34 ms 29388 KB Output is correct
6 Correct 165 ms 36000 KB Output is correct
7 Correct 181 ms 36008 KB Output is correct
8 Correct 240 ms 36444 KB Output is correct
9 Correct 208 ms 36544 KB Output is correct
10 Correct 187 ms 37644 KB Output is correct
11 Correct 251 ms 37376 KB Output is correct
12 Correct 36 ms 29528 KB Output is correct
13 Correct 203 ms 44584 KB Output is correct
14 Correct 150 ms 44704 KB Output is correct
15 Correct 187 ms 44132 KB Output is correct
16 Correct 190 ms 44248 KB Output is correct
17 Correct 35 ms 29512 KB Output is correct
18 Incorrect 134 ms 37820 KB Extra information in the output file
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28620 KB Output is correct
2 Correct 66 ms 29004 KB Output is correct
3 Correct 64 ms 29036 KB Output is correct
4 Correct 62 ms 28884 KB Output is correct
5 Correct 63 ms 28956 KB Output is correct
6 Correct 84 ms 28984 KB Output is correct
7 Correct 35 ms 28620 KB Output is correct
8 Correct 83 ms 25616 KB Output is correct
9 Correct 83 ms 25664 KB Output is correct
10 Correct 36 ms 28580 KB Output is correct
11 Correct 213 ms 41768 KB Output is correct
12 Correct 209 ms 42756 KB Output is correct
13 Correct 177 ms 42696 KB Output is correct
14 Correct 38 ms 29420 KB Output is correct
15 Correct 165 ms 36020 KB Output is correct
16 Correct 184 ms 36080 KB Output is correct
17 Correct 230 ms 36420 KB Output is correct
18 Correct 211 ms 36584 KB Output is correct
19 Correct 182 ms 37684 KB Output is correct
20 Correct 253 ms 37288 KB Output is correct
21 Correct 193 ms 36448 KB Output is correct
22 Correct 190 ms 36544 KB Output is correct
23 Correct 202 ms 36680 KB Output is correct
24 Correct 208 ms 36792 KB Output is correct
25 Correct 197 ms 37444 KB Output is correct
26 Correct 163 ms 36188 KB Output is correct
27 Correct 146 ms 36240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 28620 KB Output is correct
2 Correct 66 ms 29004 KB Output is correct
3 Correct 64 ms 29036 KB Output is correct
4 Correct 62 ms 28884 KB Output is correct
5 Correct 63 ms 28956 KB Output is correct
6 Correct 84 ms 28984 KB Output is correct
7 Correct 35 ms 28620 KB Output is correct
8 Correct 83 ms 25616 KB Output is correct
9 Correct 83 ms 25664 KB Output is correct
10 Correct 36 ms 28580 KB Output is correct
11 Correct 213 ms 41768 KB Output is correct
12 Correct 209 ms 42756 KB Output is correct
13 Correct 177 ms 42696 KB Output is correct
14 Correct 38 ms 29420 KB Output is correct
15 Correct 165 ms 36020 KB Output is correct
16 Correct 184 ms 36080 KB Output is correct
17 Correct 230 ms 36420 KB Output is correct
18 Correct 211 ms 36584 KB Output is correct
19 Correct 182 ms 37684 KB Output is correct
20 Correct 253 ms 37288 KB Output is correct
21 Correct 193 ms 36448 KB Output is correct
22 Correct 190 ms 36544 KB Output is correct
23 Correct 202 ms 36680 KB Output is correct
24 Correct 208 ms 36792 KB Output is correct
25 Correct 197 ms 37444 KB Output is correct
26 Correct 163 ms 36188 KB Output is correct
27 Correct 146 ms 36240 KB Output is correct
28 Correct 36 ms 29388 KB Output is correct
29 Correct 58 ms 30024 KB Output is correct
30 Correct 66 ms 30124 KB Output is correct
31 Correct 59 ms 30000 KB Output is correct
32 Correct 58 ms 29944 KB Output is correct
33 Correct 83 ms 30220 KB Output is correct
34 Correct 36 ms 29456 KB Output is correct
35 Correct 85 ms 28372 KB Output is correct
36 Correct 77 ms 27528 KB Output is correct
37 Correct 83 ms 27792 KB Output is correct
38 Correct 39 ms 29440 KB Output is correct
39 Correct 205 ms 44432 KB Output is correct
40 Correct 153 ms 44748 KB Output is correct
41 Correct 186 ms 44220 KB Output is correct
42 Correct 202 ms 44216 KB Output is correct
43 Correct 37 ms 29476 KB Output is correct
44 Incorrect 155 ms 37776 KB Extra information in the output file
45 Halted 0 ms 0 KB -