답안 #944832

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944832 2024-03-13T06:21:25 Z phoenix0423 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 36688 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0);
#pragma GCC optimize("Ofast","unroll-loops")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 2e5 + 5;
const int INF = 1e9 + 7;
int n, m, q;
vector<int> adj[maxn];
int dep[maxn], in[maxn], dfn = 0;
int succ[maxn][18];
int id[maxn];

void dfs(int pos, int prev){
    succ[pos][0] = prev;
    for(int i = 1; i < 18; i++) succ[pos][i] = succ[succ[pos][i - 1]][i - 1];
    in[pos] = ++dfn;
    id[dfn] = pos;
    for(auto x : adj[pos]){
        if(x == prev) continue;
        dep[x] = dep[pos] + 1;
        dfs(x, pos);
    }
}

void lift(int &b, int steps){
    for(int i = 17; i >= 0; i--){
        if(steps & (1 << i)) b = succ[b][i];
    }
}

int lca(int a, int b){
    if(dep[a] > dep[b]) swap(a, b);
    lift(b, dep[b] - dep[a]);
    if(a == b) return a;
    for(int i = 17; i >= 0; i--){
        if(succ[a][i] != succ[b][i]) a = succ[a][i], b = succ[b][i];
    }
    return succ[a][0];
}


struct info{
    int l, r, id;
    info(){}
    info(int _l, int _r, int _id) : l(_l), r(_r), id(_id){}
    bool operator < (const info& other) const{
        return r < other.r;
    };
    bool operator > (const info& other) const{
        return r > other.r;
    }
};
vector<info> b[500];
const int block = 320;
int ans[maxn];
int c[maxn];
int cur = 0;
multiset<int> st;
int path(int a, int b){
    a = id[a], b = id[b];
    return dep[a] + dep[b] - 2 * dep[lca(a, b)];
}
void add(int x){
    x = in[c[x]];
    st.insert(x);
    if(st.size() == 1) return;
    auto it = st.lower_bound(x);
    auto itp = (it == st.begin() ? prev(st.end()) : prev(it));
    auto itn = (it == prev(st.end()) ? st.begin() : next(it));
    cur -= path(*itp, *itn);
    cur += path(*it, *itp);
    cur += path(*it, *itn);
}
void del(int x){
    x = in[c[x]];
    auto it = st.lower_bound(x);
    auto itp = (it == st.begin() ? prev(st.end()) : prev(it));
    auto itn = (it == prev(st.end()) ? st.begin() : next(it));
    cur += path(*itp, *itn);
    cur -= path(*it, *itp);
    cur -= path(*it, *itn);
    st.erase(it);
}

signed main(void){
    fastio;
    cin>>n>>m>>q;
    for(int i = 0; i < n - 1; i++){
        int a, b;
        cin>>a>>b;
        a--, b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    dfs(0, 0);
    for(int i = 0; i < m; i++) cin>>c[i], c[i] --;
    for(int i = 0; i < q; i++){
        int l, r;
        cin>>l>>r;
        l--, r--;
        b[l / block].pb(info(l, r, i));
    }
    for(int i = 0; i <= m / block; i++){
        if(i % 2) sort(b[i].begin(), b[i].end(), greater<info>());
        else sort(b[i].begin(), b[i].end());
    }
    int cl = 0, cr = -1;
    for(int i = 0; i <= m / block; i++){
        for(auto [l, r, id] : b[i]){
            while(cr < r) add(++cr);
            while(cl > l) add(--cl);
            while(cr > r) del(cr--);
            while(cl < l) del(cl++);
            ans[id] = cur;
        }
    }
    for(int i = 0; i < q; i++) cout<<ans[i] / 2 + 1<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12124 KB Output is correct
2 Correct 3 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 5 ms 12232 KB Output is correct
5 Correct 4 ms 12120 KB Output is correct
6 Correct 4 ms 12124 KB Output is correct
7 Correct 5 ms 12376 KB Output is correct
8 Correct 4 ms 12124 KB Output is correct
9 Correct 7 ms 12340 KB Output is correct
10 Correct 7 ms 12120 KB Output is correct
11 Correct 7 ms 12124 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 6 ms 12124 KB Output is correct
16 Correct 6 ms 12124 KB Output is correct
17 Correct 6 ms 12120 KB Output is correct
18 Correct 6 ms 12124 KB Output is correct
19 Correct 6 ms 12136 KB Output is correct
20 Correct 6 ms 12120 KB Output is correct
21 Correct 6 ms 12124 KB Output is correct
22 Correct 7 ms 12124 KB Output is correct
23 Correct 6 ms 12124 KB Output is correct
24 Correct 6 ms 12124 KB Output is correct
25 Correct 8 ms 12124 KB Output is correct
26 Correct 6 ms 12124 KB Output is correct
27 Correct 5 ms 12124 KB Output is correct
28 Correct 3 ms 12124 KB Output is correct
29 Correct 3 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12124 KB Output is correct
2 Correct 3 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 5 ms 12232 KB Output is correct
5 Correct 4 ms 12120 KB Output is correct
6 Correct 4 ms 12124 KB Output is correct
7 Correct 5 ms 12376 KB Output is correct
8 Correct 4 ms 12124 KB Output is correct
9 Correct 7 ms 12340 KB Output is correct
10 Correct 7 ms 12120 KB Output is correct
11 Correct 7 ms 12124 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 6 ms 12124 KB Output is correct
16 Correct 6 ms 12124 KB Output is correct
17 Correct 6 ms 12120 KB Output is correct
18 Correct 6 ms 12124 KB Output is correct
19 Correct 6 ms 12136 KB Output is correct
20 Correct 6 ms 12120 KB Output is correct
21 Correct 6 ms 12124 KB Output is correct
22 Correct 7 ms 12124 KB Output is correct
23 Correct 6 ms 12124 KB Output is correct
24 Correct 6 ms 12124 KB Output is correct
25 Correct 8 ms 12124 KB Output is correct
26 Correct 6 ms 12124 KB Output is correct
27 Correct 5 ms 12124 KB Output is correct
28 Correct 3 ms 12124 KB Output is correct
29 Correct 3 ms 12124 KB Output is correct
30 Correct 35 ms 12376 KB Output is correct
31 Correct 44 ms 12380 KB Output is correct
32 Correct 53 ms 12380 KB Output is correct
33 Correct 54 ms 12376 KB Output is correct
34 Correct 52 ms 12380 KB Output is correct
35 Correct 8 ms 12380 KB Output is correct
36 Correct 9 ms 12380 KB Output is correct
37 Correct 8 ms 12376 KB Output is correct
38 Correct 43 ms 12636 KB Output is correct
39 Correct 44 ms 12636 KB Output is correct
40 Correct 43 ms 12636 KB Output is correct
41 Correct 9 ms 12636 KB Output is correct
42 Correct 8 ms 12636 KB Output is correct
43 Correct 8 ms 12380 KB Output is correct
44 Correct 47 ms 12572 KB Output is correct
45 Correct 45 ms 12380 KB Output is correct
46 Correct 45 ms 12380 KB Output is correct
47 Correct 8 ms 12376 KB Output is correct
48 Correct 8 ms 12380 KB Output is correct
49 Correct 7 ms 12380 KB Output is correct
50 Correct 46 ms 12572 KB Output is correct
51 Correct 46 ms 12380 KB Output is correct
52 Correct 46 ms 12380 KB Output is correct
53 Correct 47 ms 12380 KB Output is correct
54 Correct 48 ms 12376 KB Output is correct
55 Correct 48 ms 12636 KB Output is correct
56 Correct 21 ms 12376 KB Output is correct
57 Correct 3 ms 12376 KB Output is correct
58 Correct 6 ms 12380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12120 KB Output is correct
2 Correct 4 ms 12124 KB Output is correct
3 Correct 24 ms 12376 KB Output is correct
4 Execution timed out 5040 ms 33040 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 90 ms 21596 KB Output is correct
3 Correct 136 ms 23632 KB Output is correct
4 Correct 122 ms 24320 KB Output is correct
5 Correct 134 ms 36688 KB Output is correct
6 Correct 163 ms 35052 KB Output is correct
7 Correct 163 ms 32680 KB Output is correct
8 Correct 136 ms 32088 KB Output is correct
9 Correct 133 ms 32020 KB Output is correct
10 Correct 127 ms 31828 KB Output is correct
11 Correct 167 ms 31772 KB Output is correct
12 Correct 184 ms 32084 KB Output is correct
13 Correct 177 ms 32080 KB Output is correct
14 Correct 179 ms 32852 KB Output is correct
15 Correct 171 ms 35528 KB Output is correct
16 Correct 190 ms 34128 KB Output is correct
17 Correct 170 ms 34320 KB Output is correct
18 Correct 168 ms 34128 KB Output is correct
19 Correct 106 ms 36076 KB Output is correct
20 Correct 118 ms 36128 KB Output is correct
21 Correct 127 ms 33312 KB Output is correct
22 Correct 128 ms 32340 KB Output is correct
23 Correct 141 ms 31824 KB Output is correct
24 Correct 140 ms 31620 KB Output is correct
25 Correct 150 ms 31312 KB Output is correct
26 Correct 183 ms 31568 KB Output is correct
27 Correct 221 ms 31492 KB Output is correct
28 Correct 247 ms 31432 KB Output is correct
29 Correct 285 ms 31568 KB Output is correct
30 Correct 270 ms 31568 KB Output is correct
31 Correct 303 ms 31824 KB Output is correct
32 Correct 289 ms 32048 KB Output is correct
33 Correct 305 ms 33312 KB Output is correct
34 Correct 273 ms 35152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12124 KB Output is correct
2 Correct 4 ms 12124 KB Output is correct
3 Correct 21 ms 12460 KB Output is correct
4 Execution timed out 5013 ms 30928 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12124 KB Output is correct
2 Correct 3 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 5 ms 12232 KB Output is correct
5 Correct 4 ms 12120 KB Output is correct
6 Correct 4 ms 12124 KB Output is correct
7 Correct 5 ms 12376 KB Output is correct
8 Correct 4 ms 12124 KB Output is correct
9 Correct 7 ms 12340 KB Output is correct
10 Correct 7 ms 12120 KB Output is correct
11 Correct 7 ms 12124 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 12124 KB Output is correct
15 Correct 6 ms 12124 KB Output is correct
16 Correct 6 ms 12124 KB Output is correct
17 Correct 6 ms 12120 KB Output is correct
18 Correct 6 ms 12124 KB Output is correct
19 Correct 6 ms 12136 KB Output is correct
20 Correct 6 ms 12120 KB Output is correct
21 Correct 6 ms 12124 KB Output is correct
22 Correct 7 ms 12124 KB Output is correct
23 Correct 6 ms 12124 KB Output is correct
24 Correct 6 ms 12124 KB Output is correct
25 Correct 8 ms 12124 KB Output is correct
26 Correct 6 ms 12124 KB Output is correct
27 Correct 5 ms 12124 KB Output is correct
28 Correct 3 ms 12124 KB Output is correct
29 Correct 3 ms 12124 KB Output is correct
30 Correct 35 ms 12376 KB Output is correct
31 Correct 44 ms 12380 KB Output is correct
32 Correct 53 ms 12380 KB Output is correct
33 Correct 54 ms 12376 KB Output is correct
34 Correct 52 ms 12380 KB Output is correct
35 Correct 8 ms 12380 KB Output is correct
36 Correct 9 ms 12380 KB Output is correct
37 Correct 8 ms 12376 KB Output is correct
38 Correct 43 ms 12636 KB Output is correct
39 Correct 44 ms 12636 KB Output is correct
40 Correct 43 ms 12636 KB Output is correct
41 Correct 9 ms 12636 KB Output is correct
42 Correct 8 ms 12636 KB Output is correct
43 Correct 8 ms 12380 KB Output is correct
44 Correct 47 ms 12572 KB Output is correct
45 Correct 45 ms 12380 KB Output is correct
46 Correct 45 ms 12380 KB Output is correct
47 Correct 8 ms 12376 KB Output is correct
48 Correct 8 ms 12380 KB Output is correct
49 Correct 7 ms 12380 KB Output is correct
50 Correct 46 ms 12572 KB Output is correct
51 Correct 46 ms 12380 KB Output is correct
52 Correct 46 ms 12380 KB Output is correct
53 Correct 47 ms 12380 KB Output is correct
54 Correct 48 ms 12376 KB Output is correct
55 Correct 48 ms 12636 KB Output is correct
56 Correct 21 ms 12376 KB Output is correct
57 Correct 3 ms 12376 KB Output is correct
58 Correct 6 ms 12380 KB Output is correct
59 Correct 3 ms 12120 KB Output is correct
60 Correct 4 ms 12124 KB Output is correct
61 Correct 24 ms 12376 KB Output is correct
62 Execution timed out 5040 ms 33040 KB Time limit exceeded
63 Halted 0 ms 0 KB -