Submission #1056062

# Submission time Handle Problem Language Result Execution time Memory
1056062 2024-08-13T07:32:33 Z 정민찬(#11068) Summer Driving (CCO24_day1problem3) C++17
9 / 25
6000 ms 87864 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

const int inf = 1e9;

struct SegmentTree{
    vector<int> tree;
    void init(int n) {
        int sz = 1 << __lg(n-1) + 2;
        tree.assign(sz, inf);
    }
    void update(int node, int s, int e, int l, int r, int val) {
        if (l > e || s > r) return;
        if (l <= s && e <= r) {
            tree[node] = min(tree[node], val);
            return;
        }
        int mid = s + e >> 1;
        update(node*2, s, mid, l, r, val);
        update(node*2+1, mid+1, e, l, r, val);
    }
    int query(int node, int s, int e, int tar) {
        if (s == e) return tree[node];
        int mid = s + e >> 1;
        if (tar <= mid) return min(tree[node], query(node*2, s, mid, tar));
        return min(tree[node], query(node*2+1, mid+1, e, tar));
    }
};

/*struct Node{
    int l, r, val;
};

struct DynamicSegmentTree{
    vector<Node> tree;

};*/

SegmentTree seg;

int N, R, A, B;
vector<int> adj[300010];
vector<int> g[300010];
int down[300010], par[300010];
int dep[300010];
int in[300010], out[300010];
int pv;
int sz[300010];
int top[300010];

void dfs(int x, int p) {
    sz[x] = 1;
    down[x] = 0;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        par[y] = x;
        dep[y] = dep[x] + 1;
        g[x].push_back(y);
        dfs(y, x);
        down[x] = max(down[x], down[y] + 1);
        //if (sz[g[x][0]] < sz[y]) swap(g[x][0], g[x].back());
        sz[x] += sz[y];
    }
}

void dfs2(int x) {
    in[x] = ++ pv;
    for (auto &y : g[x]) {
        top[y] = y;
        dfs2(y);
    }
    out[x] = pv;
}

void HLDupdate(int x) {
    int ori = x;
    int cnt = B;
    while (x) {
        int tp = top[x];
        assert(tp == x);
        if (dep[x] - dep[tp] + 1 < cnt) {
            seg.update(1, 1, N, in[tp], in[x], ori);
            cnt -= dep[x] - dep[tp] + 1;
        }
        else {
            assert(cnt == 1);
            assert(in[x]);
            seg.update(1, 1, N, in[x]-cnt+1, in[x], ori);
            break;
        }
        x = par[tp];
    }
}

int findmin(int x, int p, int dist) {
    if (dist > B-1) return N + 1;
    int ret = x;
    for (auto &y : adj[x]) {
        if (y == p) continue;
        ret = min(ret, findmin(y, x, dist+1));
    }
    return ret;
}

int dp[300010];
int dp2[300010];

int simulate(int x, int p, int dist) {
    if (dist > B) return N + 1;
    int ret;
    int d = 0;
    for (auto &y : adj[x]) {
        if (y == p || y == par[x]) continue;
        d = max(d, down[y] + 1);
    }
    if (d < A) {
        ret = x;
        for (auto &y : adj[x]) {
            if (y == p || y == par[x]) continue;
            ret = min(ret, dp[y]);
        }
    }
    else {
        ret = 0;
        for (auto &y : adj[x]) {
            if (y == p || y == par[x]) continue;
            if (down[y] >= A-1)
                ret = max(ret, dp[y]);
        }
    }
    for (auto &y : adj[x]) {
        if (y == p) continue;
        ret = min(ret, simulate(y, x, dist + 1));
    }
    return ret;
}
vector<int> vtx[300010];

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> R >> A >> B;
    if (A <= B) {
        cout << "1\n";
        return 0;
    }
    for (int i=0; i<N-1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(R, 0);
    top[R] = R;
    dfs2(R);
    seg.init(N);
    int mxdep = 0;
    for (int i=1; i<=N; i++) {
        mxdep = max(mxdep, dep[i]);
        vtx[dep[i]].push_back(i);
        HLDupdate(i);
    }
    for (int i=mxdep; i>=1; i--) {
        if (i+A-1 <= mxdep) {
            for (auto &x : vtx[i+A-1]) {
                dp2[x] = simulate(x, -1, 0);
            }
        }
        for (auto &x : vtx[i]) {
            if (down[x] < A-1) {
                dp[x] = min(par[x], seg.query(1, 1, N, in[x]));
            }
            else {
                for (auto &y : vtx[i+A-1]) {
                    if (in[x] <= in[y] && in[y] <= out[x])
                        dp[x] = max(dp[x], dp2[y]);
                }
            }
        }
    }
    if (down[R] < A) {
        int ans = R;
        for (auto &x : vtx[1]) {
            ans = min(ans, dp[x]);
        }
        cout << ans;
    }
    else {
        int ans = 0;
        for (auto &x : vtx[1]) {
            if (down[x] >= A-1)
                ans = max(ans, dp[x]);
        }
        cout << ans;
    }
}

Compilation message

Main.cpp: In member function 'void SegmentTree::init(int)':
Main.cpp:12:33: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   12 |         int sz = 1 << __lg(n-1) + 2;
      |                       ~~~~~~~~~~^~~
Main.cpp: In member function 'void SegmentTree::update(int, int, int, int, int, int)':
Main.cpp:21:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   21 |         int mid = s + e >> 1;
      |                   ~~^~~
Main.cpp: In member function 'int SegmentTree::query(int, int, int, int)':
Main.cpp:27:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   27 |         int mid = s + e >> 1;
      |                   ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23900 KB Output is correct
2 Correct 3 ms 23900 KB Output is correct
3 Correct 3 ms 23900 KB Output is correct
4 Correct 3 ms 23912 KB Output is correct
5 Correct 3 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 552 ms 87792 KB Output is correct
2 Correct 588 ms 87864 KB Output is correct
3 Correct 5719 ms 87492 KB Output is correct
4 Execution timed out 6047 ms 76136 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 7 ms 21596 KB Output is correct
3 Correct 7 ms 21596 KB Output is correct
4 Correct 9 ms 21596 KB Output is correct
5 Correct 10 ms 21596 KB Output is correct
6 Correct 5 ms 22620 KB Output is correct
7 Correct 4 ms 22620 KB Output is correct
8 Correct 5 ms 28084 KB Output is correct
9 Correct 5 ms 23900 KB Output is correct
10 Correct 4 ms 25948 KB Output is correct
11 Correct 8 ms 21468 KB Output is correct
12 Correct 9 ms 21596 KB Output is correct
13 Correct 10 ms 21652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 7 ms 21596 KB Output is correct
3 Correct 7 ms 21596 KB Output is correct
4 Correct 9 ms 21596 KB Output is correct
5 Correct 10 ms 21596 KB Output is correct
6 Correct 5 ms 22620 KB Output is correct
7 Correct 4 ms 22620 KB Output is correct
8 Correct 5 ms 28084 KB Output is correct
9 Correct 5 ms 23900 KB Output is correct
10 Correct 4 ms 25948 KB Output is correct
11 Correct 8 ms 21468 KB Output is correct
12 Correct 9 ms 21596 KB Output is correct
13 Correct 10 ms 21652 KB Output is correct
14 Correct 10 ms 21848 KB Output is correct
15 Correct 11 ms 21992 KB Output is correct
16 Correct 15 ms 22104 KB Output is correct
17 Correct 9 ms 21848 KB Output is correct
18 Correct 9 ms 21852 KB Output is correct
19 Correct 10 ms 21852 KB Output is correct
20 Correct 10 ms 21872 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 10 ms 21592 KB Output is correct
23 Correct 22 ms 21948 KB Output is correct
24 Correct 10 ms 21848 KB Output is correct
25 Correct 36 ms 21852 KB Output is correct
26 Correct 20 ms 21852 KB Output is correct
27 Correct 9 ms 21848 KB Output is correct
28 Correct 9 ms 21852 KB Output is correct
29 Correct 8 ms 21852 KB Output is correct
30 Correct 31 ms 21852 KB Output is correct
31 Correct 33 ms 21852 KB Output is correct
32 Correct 31 ms 21756 KB Output is correct
33 Correct 27 ms 23128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 790 ms 34556 KB Output is correct
2 Correct 619 ms 31720 KB Output is correct
3 Correct 490 ms 31368 KB Output is correct
4 Correct 436 ms 32080 KB Output is correct
5 Correct 733 ms 31576 KB Output is correct
6 Correct 634 ms 32000 KB Output is correct
7 Correct 256 ms 33364 KB Output is correct
8 Correct 92 ms 33872 KB Output is correct
9 Correct 477 ms 32340 KB Output is correct
10 Correct 271 ms 31828 KB Output is correct
11 Correct 129 ms 31312 KB Output is correct
12 Correct 75 ms 35544 KB Output is correct
13 Correct 8 ms 21596 KB Output is correct
14 Correct 12 ms 21604 KB Output is correct
15 Correct 296 ms 30908 KB Output is correct
16 Execution timed out 6056 ms 31040 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 21596 KB Output is correct
2 Correct 7 ms 21596 KB Output is correct
3 Correct 7 ms 21596 KB Output is correct
4 Correct 9 ms 21596 KB Output is correct
5 Correct 10 ms 21596 KB Output is correct
6 Correct 5 ms 22620 KB Output is correct
7 Correct 4 ms 22620 KB Output is correct
8 Correct 5 ms 28084 KB Output is correct
9 Correct 5 ms 23900 KB Output is correct
10 Correct 4 ms 25948 KB Output is correct
11 Correct 8 ms 21468 KB Output is correct
12 Correct 9 ms 21596 KB Output is correct
13 Correct 10 ms 21652 KB Output is correct
14 Correct 10 ms 21848 KB Output is correct
15 Correct 11 ms 21992 KB Output is correct
16 Correct 15 ms 22104 KB Output is correct
17 Correct 9 ms 21848 KB Output is correct
18 Correct 9 ms 21852 KB Output is correct
19 Correct 10 ms 21852 KB Output is correct
20 Correct 10 ms 21872 KB Output is correct
21 Correct 8 ms 21852 KB Output is correct
22 Correct 10 ms 21592 KB Output is correct
23 Correct 22 ms 21948 KB Output is correct
24 Correct 10 ms 21848 KB Output is correct
25 Correct 36 ms 21852 KB Output is correct
26 Correct 20 ms 21852 KB Output is correct
27 Correct 9 ms 21848 KB Output is correct
28 Correct 9 ms 21852 KB Output is correct
29 Correct 8 ms 21852 KB Output is correct
30 Correct 31 ms 21852 KB Output is correct
31 Correct 33 ms 21852 KB Output is correct
32 Correct 31 ms 21756 KB Output is correct
33 Correct 27 ms 23128 KB Output is correct
34 Correct 883 ms 31448 KB Output is correct
35 Correct 693 ms 31672 KB Output is correct
36 Correct 472 ms 36688 KB Output is correct
37 Correct 467 ms 33440 KB Output is correct
38 Correct 816 ms 31716 KB Output is correct
39 Correct 663 ms 31824 KB Output is correct
40 Correct 253 ms 31796 KB Output is correct
41 Correct 96 ms 30804 KB Output is correct
42 Correct 3 ms 23900 KB Output is correct
43 Correct 3772 ms 33884 KB Output is correct
44 Execution timed out 6058 ms 31076 KB Time limit exceeded
45 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 23900 KB Output is correct
2 Correct 3 ms 23900 KB Output is correct
3 Correct 3 ms 23900 KB Output is correct
4 Correct 3 ms 23912 KB Output is correct
5 Correct 3 ms 23900 KB Output is correct
6 Correct 552 ms 87792 KB Output is correct
7 Correct 588 ms 87864 KB Output is correct
8 Correct 5719 ms 87492 KB Output is correct
9 Execution timed out 6047 ms 76136 KB Time limit exceeded
10 Halted 0 ms 0 KB -