답안 #764211

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
764211 2023-06-23T08:38:54 Z huutuan Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 33120 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

const int N=1e5+1, S=320, LG=17;

struct query{
    int l, r, i;

    query(int a=0, int b=0, int c=0): l(a), r(b), i(c){}

    bool operator<(const query& x){
        return l/S!=x.l/S?l<x.l:r<x.r;
    }
} qq[N];

int n, m, q, a[N], tdfs, tin[N], tout[N], up[N][LG], dep[N], ans[N];
vector<int> g[N];

void dfs(int u){
    tin[u]=++tdfs;
    dep[u]=dep[up[u][0]]+1;
    for (int v:g[u]) if (v!=up[u][0]){
        up[v][0]=u;
        for (int k=1; k<LG; ++k) up[v][k]=up[up[v][k-1]][k-1];
        dfs(v);
    }
    tout[u]=tdfs;
}

int lca(int u, int v){
    if (dep[u]!=dep[v]){
        if (dep[u]<dep[v]) swap(u, v);
        int d=dep[u]-dep[v];
        for (int k=0; k<LG; ++k) if (d>>k&1) u=up[u][k];
    }
    if (u==v) return u;
    for (int k=LG-1; k>=0; --k) if (up[u][k]!=up[v][k]) u=up[u][k], v=up[v][k];
    return up[u][0];
}

void solve(int tc){
    // cout << "Case #" << tc << ": ";
    cin >> n >> m >> q;
    for (int i=1; i<n; ++i){
        int x, y; cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    for (int i=1; i<=m; ++i) cin >> a[i];
    for (int i=1; i<=q; ++i){
        int x, y; cin >> x >> y;
        qq[i]={x, y, i};
    }
    sort(qq+1, qq+q+1);
    multiset<pair<int, int>> st;
    int val=0;
    function<int(int, int)> calc=[&](int x, int y) -> int {
        return dep[y]-dep[lca(x, y)];
    };
    function<void(int)> add=[&](int x) -> void {
        if (st.empty()){
            st.emplace(tin[x], x);
            return;
        }
        auto it=st.lower_bound({tin[x], x});
        auto previt=it==st.begin()?prev(st.end()):prev(it);
        auto nextit=it==st.end()?st.begin():it;
        val-=calc(previt->second, nextit->second);
        val+=calc(previt->second, x);
        val+=calc(x, nextit->second);
        st.emplace(tin[x], x);
    };
    function<void(int)> del=[&](int x) -> void {
        if (sz(st)==1){
            st.erase({tin[x], x});
            return;
        }
        auto it=st.find({tin[x], x});
        auto previt=it==st.begin()?prev(st.end()):prev(it);
        auto nextit=it==prev(st.end())?st.begin():next(it);
        val+=calc(previt->second, nextit->second);
        val-=calc(previt->second, x);
        val-=calc(x, nextit->second);
        st.erase(it);
    };
    int cl=1, cr=0;
    for (int i=1; i<=q; ++i){
        int l=qq[i].l, r=qq[i].r;
        while (l<cl){
            --cl;
            add(a[cl]);
        }
        while (r>cr){
            ++cr;
            add(a[cr]);
        }
        while (l>cl){
            del(a[cl]);
            ++cl;
        }
        while (r<cr){
            del(a[cr]);
            --cr;
        }
        ans[qq[i].i]=val;
    }
    for (int i=1; i<=q; ++i) cout << ans[i]+1 << '\n';
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int ntests=1;
    // cin >> ntests;
    for (int i=1; i<=ntests; ++i) solve(i);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5028 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 7 ms 5028 KB Output is correct
7 Correct 6 ms 5028 KB Output is correct
8 Correct 6 ms 5032 KB Output is correct
9 Correct 11 ms 5036 KB Output is correct
10 Correct 12 ms 5136 KB Output is correct
11 Correct 9 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 7 ms 5032 KB Output is correct
16 Correct 9 ms 5160 KB Output is correct
17 Correct 8 ms 5140 KB Output is correct
18 Correct 7 ms 5076 KB Output is correct
19 Correct 8 ms 5076 KB Output is correct
20 Correct 7 ms 5040 KB Output is correct
21 Correct 8 ms 5076 KB Output is correct
22 Correct 9 ms 5032 KB Output is correct
23 Correct 7 ms 5036 KB Output is correct
24 Correct 8 ms 5124 KB Output is correct
25 Correct 10 ms 5136 KB Output is correct
26 Correct 9 ms 5032 KB Output is correct
27 Correct 4 ms 5032 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5028 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 7 ms 5028 KB Output is correct
7 Correct 6 ms 5028 KB Output is correct
8 Correct 6 ms 5032 KB Output is correct
9 Correct 11 ms 5036 KB Output is correct
10 Correct 12 ms 5136 KB Output is correct
11 Correct 9 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 7 ms 5032 KB Output is correct
16 Correct 9 ms 5160 KB Output is correct
17 Correct 8 ms 5140 KB Output is correct
18 Correct 7 ms 5076 KB Output is correct
19 Correct 8 ms 5076 KB Output is correct
20 Correct 7 ms 5040 KB Output is correct
21 Correct 8 ms 5076 KB Output is correct
22 Correct 9 ms 5032 KB Output is correct
23 Correct 7 ms 5036 KB Output is correct
24 Correct 8 ms 5124 KB Output is correct
25 Correct 10 ms 5136 KB Output is correct
26 Correct 9 ms 5032 KB Output is correct
27 Correct 4 ms 5032 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5120 KB Output is correct
30 Correct 47 ms 5456 KB Output is correct
31 Correct 57 ms 5472 KB Output is correct
32 Correct 73 ms 5588 KB Output is correct
33 Correct 69 ms 5584 KB Output is correct
34 Correct 67 ms 5616 KB Output is correct
35 Correct 9 ms 5640 KB Output is correct
36 Correct 9 ms 5588 KB Output is correct
37 Correct 9 ms 5588 KB Output is correct
38 Correct 50 ms 5676 KB Output is correct
39 Correct 48 ms 5664 KB Output is correct
40 Correct 55 ms 5656 KB Output is correct
41 Correct 8 ms 5616 KB Output is correct
42 Correct 8 ms 5684 KB Output is correct
43 Correct 9 ms 5668 KB Output is correct
44 Correct 53 ms 5644 KB Output is correct
45 Correct 66 ms 5628 KB Output is correct
46 Correct 52 ms 5608 KB Output is correct
47 Correct 8 ms 5588 KB Output is correct
48 Correct 7 ms 5588 KB Output is correct
49 Correct 8 ms 5552 KB Output is correct
50 Correct 57 ms 5628 KB Output is correct
51 Correct 57 ms 5560 KB Output is correct
52 Correct 57 ms 5588 KB Output is correct
53 Correct 57 ms 5624 KB Output is correct
54 Correct 59 ms 5588 KB Output is correct
55 Correct 58 ms 5580 KB Output is correct
56 Correct 18 ms 5172 KB Output is correct
57 Correct 3 ms 5424 KB Output is correct
58 Correct 4 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 4 ms 5024 KB Output is correct
3 Correct 17 ms 5216 KB Output is correct
4 Execution timed out 5043 ms 26788 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5032 KB Output is correct
2 Correct 79 ms 16176 KB Output is correct
3 Correct 132 ms 18436 KB Output is correct
4 Correct 105 ms 19860 KB Output is correct
5 Correct 157 ms 33120 KB Output is correct
6 Correct 244 ms 31096 KB Output is correct
7 Correct 203 ms 28276 KB Output is correct
8 Correct 192 ms 27232 KB Output is correct
9 Correct 194 ms 26956 KB Output is correct
10 Correct 187 ms 26972 KB Output is correct
11 Correct 207 ms 26936 KB Output is correct
12 Correct 166 ms 26892 KB Output is correct
13 Correct 157 ms 27144 KB Output is correct
14 Correct 164 ms 27508 KB Output is correct
15 Correct 139 ms 29036 KB Output is correct
16 Correct 182 ms 29948 KB Output is correct
17 Correct 178 ms 29916 KB Output is correct
18 Correct 226 ms 30112 KB Output is correct
19 Correct 137 ms 32572 KB Output is correct
20 Correct 188 ms 32668 KB Output is correct
21 Correct 211 ms 28904 KB Output is correct
22 Correct 165 ms 27652 KB Output is correct
23 Correct 168 ms 27048 KB Output is correct
24 Correct 207 ms 26668 KB Output is correct
25 Correct 187 ms 26448 KB Output is correct
26 Correct 252 ms 26392 KB Output is correct
27 Correct 248 ms 26452 KB Output is correct
28 Correct 263 ms 26428 KB Output is correct
29 Correct 249 ms 26444 KB Output is correct
30 Correct 306 ms 26500 KB Output is correct
31 Correct 260 ms 26572 KB Output is correct
32 Correct 245 ms 26816 KB Output is correct
33 Correct 228 ms 27492 KB Output is correct
34 Correct 178 ms 28452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 5036 KB Output is correct
3 Correct 17 ms 5224 KB Output is correct
4 Execution timed out 5029 ms 25144 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5028 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 6 ms 5100 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 7 ms 5028 KB Output is correct
7 Correct 6 ms 5028 KB Output is correct
8 Correct 6 ms 5032 KB Output is correct
9 Correct 11 ms 5036 KB Output is correct
10 Correct 12 ms 5136 KB Output is correct
11 Correct 9 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 3 ms 5032 KB Output is correct
15 Correct 7 ms 5032 KB Output is correct
16 Correct 9 ms 5160 KB Output is correct
17 Correct 8 ms 5140 KB Output is correct
18 Correct 7 ms 5076 KB Output is correct
19 Correct 8 ms 5076 KB Output is correct
20 Correct 7 ms 5040 KB Output is correct
21 Correct 8 ms 5076 KB Output is correct
22 Correct 9 ms 5032 KB Output is correct
23 Correct 7 ms 5036 KB Output is correct
24 Correct 8 ms 5124 KB Output is correct
25 Correct 10 ms 5136 KB Output is correct
26 Correct 9 ms 5032 KB Output is correct
27 Correct 4 ms 5032 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 3 ms 5120 KB Output is correct
30 Correct 47 ms 5456 KB Output is correct
31 Correct 57 ms 5472 KB Output is correct
32 Correct 73 ms 5588 KB Output is correct
33 Correct 69 ms 5584 KB Output is correct
34 Correct 67 ms 5616 KB Output is correct
35 Correct 9 ms 5640 KB Output is correct
36 Correct 9 ms 5588 KB Output is correct
37 Correct 9 ms 5588 KB Output is correct
38 Correct 50 ms 5676 KB Output is correct
39 Correct 48 ms 5664 KB Output is correct
40 Correct 55 ms 5656 KB Output is correct
41 Correct 8 ms 5616 KB Output is correct
42 Correct 8 ms 5684 KB Output is correct
43 Correct 9 ms 5668 KB Output is correct
44 Correct 53 ms 5644 KB Output is correct
45 Correct 66 ms 5628 KB Output is correct
46 Correct 52 ms 5608 KB Output is correct
47 Correct 8 ms 5588 KB Output is correct
48 Correct 7 ms 5588 KB Output is correct
49 Correct 8 ms 5552 KB Output is correct
50 Correct 57 ms 5628 KB Output is correct
51 Correct 57 ms 5560 KB Output is correct
52 Correct 57 ms 5588 KB Output is correct
53 Correct 57 ms 5624 KB Output is correct
54 Correct 59 ms 5588 KB Output is correct
55 Correct 58 ms 5580 KB Output is correct
56 Correct 18 ms 5172 KB Output is correct
57 Correct 3 ms 5424 KB Output is correct
58 Correct 4 ms 5460 KB Output is correct
59 Correct 2 ms 4948 KB Output is correct
60 Correct 4 ms 5024 KB Output is correct
61 Correct 17 ms 5216 KB Output is correct
62 Execution timed out 5043 ms 26788 KB Time limit exceeded
63 Halted 0 ms 0 KB -