답안 #588757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588757 2022-07-04T03:08:02 Z maomao90 Inside information (BOI21_servers) C++17
100 / 100
761 ms 162796 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 300005;
const int MAXL = 20;

struct Qry {
    char t;
    int a, b;
};

struct Edge {
    int v, lo, hi;
    bool inc, dec;
    Edge operator+ (const Edge &o) const {
        if (v == -1) {
            return o;
        }
        if (o.v == -1) {
            return *this;
        }
        Edge res;
        res.v = o.v;
        res.lo = lo;
        res.hi = o.hi;
        res.inc = inc && o.inc && hi < o.lo;
        res.dec = dec && o.dec && hi > o.lo;
        return res;
    }
    Edge operator~ () const {
        return {v, hi, lo, dec, inc};
    }
    friend ostream& operator<< (ostream &os, const Edge &o) {
        return os << "(" << o.v << ' ' << o.lo << ' ' << o.hi << ' ' << o.inc << ' ' << o.dec << ")";
    }
};

int n, k;
vector<Edge> adj[MAXN];
Qry qry[MAXN];
int ans[MAXN];

Edge p[MAXL][MAXN];
int lvl[MAXN];
void dfs(int u) {
    REP (k, 1, MAXL) {
        if (p[k - 1][u].v == -1 || p[k - 1][p[k - 1][u].v].v == -1) {
            p[k][u] = {-1, -1, -1, 0, 0};
        } else {
            p[k][u] = p[k - 1][u] + p[k - 1][p[k - 1][u].v];
        }
    }
    for (Edge e : adj[u]) {
        if (e.v == p[0][u].v) {
            continue;
        }
        lvl[e.v] = lvl[u] + 1;
        p[0][e.v] = {u, e.lo, e.hi, 1, 1};
        dfs(e.v);
    }
}

int sub[MAXN], cp[MAXN];
Edge pth[MAXL][MAXN];
vii ev[MAXN];
void getst(int u, int p, int l) {
    sub[u] = 1;
    for (Edge e : adj[u]) {
        if (e.v == p || lvl[e.v] != -1) {
            continue;
        }
        if (l > 0) {
            pth[l - 1][e.v] = e + pth[l - 1][u];
        }
        getst(e.v, u, l);
        sub[u] += sub[e.v];
    }
}
int getcent(int u, int p, int s) {
    for (Edge e : adj[u]) {
        if (e.v == p || lvl[e.v] != -1) {
            continue;
        }
        if (sub[e.v] > s / 2) {
            return getcent(e.v, u, s);
        }
    }
    return u;
}
void build(int u, int p, int l) {
    getst(u, -1, l);
    int cent = getcent(u, -1, sub[u]);
    cp[cent] = p;
    lvl[cent] = l;
    for (Edge e : adj[cent]) {
        if (e.v == p || lvl[e.v] != -1) {
            continue;
        }
        pth[l][e.v] = e;
        build(e.v, cent, l + 1);
    }
}

int st[MAXN * 40], lc[MAXN * 40], rc[MAXN * 40], ptr;
void incre(int p, int x, int u, int lo = 1, int hi = n + k) {
    if (lo == hi) {
        st[u] += x;
        return;
    }
    int mid = lo + hi >> 1;
    if (p <= mid) {
        if (lc[u] == 0) {
            lc[u] = ptr++;
        }
        incre(p, x, lc[u], lo, mid);
    } else {
        if (rc[u] == 0) {
            rc[u] = ptr++;
        }
        incre(p, x, rc[u], mid + 1, hi);
    }
    st[u] = st[lc[u]] + st[rc[u]];
}
int qsm(int s, int e, int u, int lo = 1, int hi = n + k) {
    if (lo >= s && hi <= e) {
        return st[u];
    }
    int mid = lo + hi >> 1;
    int res = 0;
    if (s <= mid) {
        res += qsm(s, e, lc[u], lo, mid);
    }
    if (e > mid) {
        res += qsm(s, e, rc[u], mid + 1, hi);
    }
    return res;
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> n >> k;
    REP (i, 1, n + k) {
        char t; cin >> t;
        if (t == 'S') {
            int a, b; cin >> a >> b;
            adj[a].pb({b, i, i, 1, 1});
            adj[b].pb({a, i, i, 1, 1});
            qry[i] = {t, a, b};
        } else if (t == 'Q') {
            int a, b; cin >> a >> b;
            qry[i] = {t, a, b};
        } else {
            int a; cin >> a;
            qry[i] = {t, a, -1};
        }
    }
    REP (k, 0, MAXL) {
        p[k][1] = {-1, -1, -1, 0, 0};
    }
    dfs(1);
    REP (i, 1, n + k) {
        auto [t, a, b] = qry[i];
        if (t == 'S') {
        } else if (t == 'Q') {
            if (a == b) {
                ans[i] = 1;
                continue;
            }
            Edge lft = {-1, -1, -1, 0, 0}, rht = {-1, -1, -1, 0, 0};
            bool swp = 0;
            if (lvl[a] < lvl[b]) {
                swap(a, b);
                swp = 1;
            }
            RREP (k, MAXL - 1, 0) {
                if (p[k][a].v != -1 && lvl[p[k][a].v] >= lvl[b]) {
                    lft = lft + p[k][a];
                    a = p[k][a].v;
                }
            }
            if (a != b) {
                RREP (k, MAXL - 1, 0) {
                    if (p[k][a].v != p[k][b].v) {
                        lft = lft + p[k][a];
                        a = p[k][a].v;
                        rht = rht + p[k][b];
                        b = p[k][b].v;
                    }
                }
                lft = lft + p[0][a];
                a = p[0][a].v;
                rht = rht + p[0][b];
                b = p[0][b].v;
            }
            assert(a == b);
            if (swp) {
                swap(lft, rht);
                swap(a, b);
            }
            Edge res = rht + ~lft;
            if (res.inc && res.hi <= i) {
                ans[i] = 1;
            } else {
                ans[i] = 0;
            }
        } else {
        }
    }
    memset(lvl, -1, sizeof lvl);
    build(1, -1, 0);
    ptr = n + 1;
    REP (i, 1, n + 1) {
        int a = i;
        cerr << i << '\n';
        ev[0].pb({n + k, i});
        RREP (j, lvl[i] - 1, 0) {
            a = cp[a];
            if (pth[j][i].dec) {
                ev[pth[j][i].lo].pb({pth[j][i].hi, a});
                cerr << ' ' << a << ' ' << pth[j][i].lo << ' ' << pth[j][i].hi << '\n';
            }
        }
        assert(cp[a] == -1);
    }
    REP (i, 0, n + k) {
        auto [t, a, b] = qry[i];
        if (i == 0 || t == 'S') {
            for (auto [x, y] : ev[i]) {
                incre(x, 1, y);
            }
        } else if (t == 'Q') {
        } else {
            cerr << a << '\n';
            ans[i] = st[a];
            cerr << ' ' << st[a] << '\n';
            int u = a;
            RREP (l, lvl[a] - 1, 0) {
                u = cp[u];
                Edge e = pth[l][a];
                if (e.inc && e.hi <= i) {
                    int tmp = qsm(e.hi + 1, n + k, u);
                    ans[i] += tmp;
                    cerr << ' ' << u << ' ' << e.hi << ' ' << tmp << '\n';
                }
            }
            assert(cp[u] == -1);
        }
    }
    REP (i, 1, n + k) {
        auto [t, a, b] = qry[i];
        if (t == 'S') {
        } else if (t == 'Q') {
            if (ans[i]) {
                cout << "yes\n";
            } else {
                cout << "no\n"; 
            }
        } else {
            cout << ans[i] << '\n';
        }
    }
    return 0;
}

Compilation message

servers.cpp: In function 'void incre(int, int, int, int, int)':
servers.cpp:142:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  142 |     int mid = lo + hi >> 1;
      |               ~~~^~~~
servers.cpp: In function 'int qsm(int, int, int, int, int)':
servers.cpp:160:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  160 |     int mid = lo + hi >> 1;
      |               ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 18980 KB Output is correct
2 Correct 63 ms 23188 KB Output is correct
3 Correct 47 ms 22352 KB Output is correct
4 Correct 76 ms 23488 KB Output is correct
5 Correct 59 ms 23484 KB Output is correct
6 Correct 48 ms 21932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 18980 KB Output is correct
2 Correct 63 ms 23188 KB Output is correct
3 Correct 47 ms 22352 KB Output is correct
4 Correct 76 ms 23488 KB Output is correct
5 Correct 59 ms 23484 KB Output is correct
6 Correct 48 ms 21932 KB Output is correct
7 Correct 40 ms 19056 KB Output is correct
8 Correct 56 ms 22880 KB Output is correct
9 Correct 59 ms 21984 KB Output is correct
10 Correct 79 ms 23224 KB Output is correct
11 Correct 59 ms 23116 KB Output is correct
12 Correct 47 ms 21756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18896 KB Output is correct
2 Correct 267 ms 94688 KB Output is correct
3 Correct 240 ms 94716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18896 KB Output is correct
2 Correct 267 ms 94688 KB Output is correct
3 Correct 240 ms 94716 KB Output is correct
4 Correct 34 ms 18852 KB Output is correct
5 Correct 259 ms 94532 KB Output is correct
6 Correct 181 ms 93884 KB Output is correct
7 Correct 185 ms 93964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 18892 KB Output is correct
2 Correct 368 ms 162704 KB Output is correct
3 Correct 409 ms 162792 KB Output is correct
4 Correct 414 ms 157880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 18892 KB Output is correct
2 Correct 368 ms 162704 KB Output is correct
3 Correct 409 ms 162792 KB Output is correct
4 Correct 414 ms 157880 KB Output is correct
5 Correct 33 ms 18892 KB Output is correct
6 Correct 410 ms 162268 KB Output is correct
7 Correct 440 ms 157044 KB Output is correct
8 Correct 409 ms 161852 KB Output is correct
9 Correct 420 ms 162016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 18884 KB Output is correct
2 Correct 343 ms 137680 KB Output is correct
3 Correct 342 ms 144468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 18884 KB Output is correct
2 Correct 343 ms 137680 KB Output is correct
3 Correct 342 ms 144468 KB Output is correct
4 Correct 47 ms 18896 KB Output is correct
5 Correct 419 ms 137348 KB Output is correct
6 Correct 402 ms 143968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18892 KB Output is correct
2 Correct 393 ms 162796 KB Output is correct
3 Correct 373 ms 162764 KB Output is correct
4 Correct 408 ms 158084 KB Output is correct
5 Correct 36 ms 18980 KB Output is correct
6 Correct 338 ms 137760 KB Output is correct
7 Correct 337 ms 144472 KB Output is correct
8 Correct 550 ms 141692 KB Output is correct
9 Correct 449 ms 140916 KB Output is correct
10 Correct 529 ms 155712 KB Output is correct
11 Correct 672 ms 155092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18892 KB Output is correct
2 Correct 393 ms 162796 KB Output is correct
3 Correct 373 ms 162764 KB Output is correct
4 Correct 408 ms 158084 KB Output is correct
5 Correct 36 ms 18980 KB Output is correct
6 Correct 338 ms 137760 KB Output is correct
7 Correct 337 ms 144472 KB Output is correct
8 Correct 550 ms 141692 KB Output is correct
9 Correct 449 ms 140916 KB Output is correct
10 Correct 529 ms 155712 KB Output is correct
11 Correct 672 ms 155092 KB Output is correct
12 Correct 33 ms 18892 KB Output is correct
13 Correct 414 ms 162404 KB Output is correct
14 Correct 439 ms 157000 KB Output is correct
15 Correct 405 ms 162008 KB Output is correct
16 Correct 419 ms 161852 KB Output is correct
17 Correct 38 ms 18892 KB Output is correct
18 Correct 411 ms 137448 KB Output is correct
19 Correct 375 ms 144092 KB Output is correct
20 Correct 524 ms 141776 KB Output is correct
21 Correct 455 ms 141504 KB Output is correct
22 Correct 622 ms 154048 KB Output is correct
23 Correct 757 ms 159096 KB Output is correct
24 Correct 639 ms 157988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 18892 KB Output is correct
2 Correct 63 ms 23312 KB Output is correct
3 Correct 52 ms 22348 KB Output is correct
4 Correct 86 ms 23536 KB Output is correct
5 Correct 61 ms 23500 KB Output is correct
6 Correct 47 ms 21984 KB Output is correct
7 Correct 34 ms 18912 KB Output is correct
8 Correct 243 ms 94672 KB Output is correct
9 Correct 252 ms 94656 KB Output is correct
10 Correct 32 ms 18924 KB Output is correct
11 Correct 395 ms 162760 KB Output is correct
12 Correct 376 ms 162736 KB Output is correct
13 Correct 424 ms 157968 KB Output is correct
14 Correct 35 ms 18892 KB Output is correct
15 Correct 348 ms 137664 KB Output is correct
16 Correct 347 ms 144472 KB Output is correct
17 Correct 541 ms 141636 KB Output is correct
18 Correct 497 ms 140768 KB Output is correct
19 Correct 532 ms 155784 KB Output is correct
20 Correct 632 ms 155144 KB Output is correct
21 Correct 274 ms 98140 KB Output is correct
22 Correct 271 ms 98756 KB Output is correct
23 Correct 368 ms 121096 KB Output is correct
24 Correct 349 ms 120348 KB Output is correct
25 Correct 452 ms 147660 KB Output is correct
26 Correct 399 ms 141292 KB Output is correct
27 Correct 342 ms 136680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 18892 KB Output is correct
2 Correct 63 ms 23312 KB Output is correct
3 Correct 52 ms 22348 KB Output is correct
4 Correct 86 ms 23536 KB Output is correct
5 Correct 61 ms 23500 KB Output is correct
6 Correct 47 ms 21984 KB Output is correct
7 Correct 34 ms 18912 KB Output is correct
8 Correct 243 ms 94672 KB Output is correct
9 Correct 252 ms 94656 KB Output is correct
10 Correct 32 ms 18924 KB Output is correct
11 Correct 395 ms 162760 KB Output is correct
12 Correct 376 ms 162736 KB Output is correct
13 Correct 424 ms 157968 KB Output is correct
14 Correct 35 ms 18892 KB Output is correct
15 Correct 348 ms 137664 KB Output is correct
16 Correct 347 ms 144472 KB Output is correct
17 Correct 541 ms 141636 KB Output is correct
18 Correct 497 ms 140768 KB Output is correct
19 Correct 532 ms 155784 KB Output is correct
20 Correct 632 ms 155144 KB Output is correct
21 Correct 274 ms 98140 KB Output is correct
22 Correct 271 ms 98756 KB Output is correct
23 Correct 368 ms 121096 KB Output is correct
24 Correct 349 ms 120348 KB Output is correct
25 Correct 452 ms 147660 KB Output is correct
26 Correct 399 ms 141292 KB Output is correct
27 Correct 342 ms 136680 KB Output is correct
28 Correct 38 ms 18916 KB Output is correct
29 Correct 56 ms 22932 KB Output is correct
30 Correct 50 ms 22084 KB Output is correct
31 Correct 67 ms 23144 KB Output is correct
32 Correct 61 ms 23064 KB Output is correct
33 Correct 48 ms 21704 KB Output is correct
34 Correct 36 ms 18948 KB Output is correct
35 Correct 242 ms 94580 KB Output is correct
36 Correct 184 ms 93772 KB Output is correct
37 Correct 188 ms 94040 KB Output is correct
38 Correct 34 ms 18892 KB Output is correct
39 Correct 432 ms 162348 KB Output is correct
40 Correct 433 ms 157016 KB Output is correct
41 Correct 436 ms 161940 KB Output is correct
42 Correct 411 ms 161940 KB Output is correct
43 Correct 37 ms 18988 KB Output is correct
44 Correct 425 ms 137348 KB Output is correct
45 Correct 369 ms 143960 KB Output is correct
46 Correct 505 ms 141888 KB Output is correct
47 Correct 477 ms 141540 KB Output is correct
48 Correct 577 ms 154208 KB Output is correct
49 Correct 761 ms 159168 KB Output is correct
50 Correct 672 ms 158076 KB Output is correct
51 Correct 210 ms 97864 KB Output is correct
52 Correct 216 ms 95136 KB Output is correct
53 Correct 208 ms 94392 KB Output is correct
54 Correct 189 ms 95152 KB Output is correct
55 Correct 209 ms 97624 KB Output is correct
56 Correct 336 ms 121732 KB Output is correct
57 Correct 409 ms 147200 KB Output is correct
58 Correct 528 ms 141592 KB Output is correct
59 Correct 362 ms 136464 KB Output is correct