Submission #944914

# Submission time Handle Problem Language Result Execution time Memory
944914 2024-03-13T07:59:34 Z phoenix0423 Tourism (JOI23_tourism) C++17
10 / 100
4430 ms 78920 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 = 3e5 + 5;
const int INF = 1e9 + 7;
int n, m, q;
vector<int> adj[maxn];
int dep[maxn], in[maxn], dfn = 0;
int arr[maxn][18];
int id[maxn];

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

int lca(int a, int b){
    if(a > b) swap(a, b);
    int len = __lg(b - a + 1);
    return min(arr[a][len], arr[b - (1 << len) + 1][len]);
}

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;
    };
};
vector<info> b[500];
const int block = 320;
int ans[maxn];
int c[maxn], loc[maxn];
int cur = 0;
int path(int a, int b){
    return dep[a] + dep[b] - 2 * lca(in[a], in[b]);
}
struct node{
    int l, r, val;
} st[maxn];

struct rollback{
    int l, r, m;
};
stack<rollback> stk;
void roll_back(){
    auto [l, r, m] = stk.top(); stk.pop();
    cur -= path(st[l].val, st[r].val);
    cur += path(st[l].val, st[m].val) + path(st[m].val, st[r].val);
    st[l].r = m, st[r].l = m;
}
void del(int x){
    x = loc[x];
    int prv = st[x].l, nxt = st[x].r;
    stk.push({prv, nxt, x});
    cur -= path(st[prv].val, st[x].val) + path(st[x].val, st[nxt].val);
    cur += path(st[prv].val, st[nxt].val);
    st[prv].r = nxt, st[nxt].l = prv;
}

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 = 1; (1 << i) <= dfn; i++){
        for(int j = 0; j + (1 << i) - 1 <= dfn; j++){
            arr[j][i] = min(arr[j][i - 1], arr[j + (1 << (i - 1))][i - 1]);
        }
    }
    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++){
        sort(b[i].begin(), b[i].end());
    }
    vector<pll> cpy;
    for(int i = 0; i < m; i++) cpy.eb(c[i], i);
    sort(cpy.begin(), cpy.end(), [&](pll a, pll b){ return in[a.f] < in[b.f];});
    for(int i = 0; i < m; i++){
        auto [val, id] = cpy[i];
        loc[id] = i;
        st[i].val = val;
        if(!i) st[i].l = m - 1;
        else st[i].l = i - 1;
        if(i == m - 1) st[i].r = 0;
        else st[i].r = i + 1;
    }
    for(int i = 0; i < m; i++) cur += path(st[i].val, st[st[i].r].val);
    int cl = 0, cr = m - 1;
    for(int i = 0; i <= m / block; i++){
        if(!b[i].size()) continue;
        while(cr < m - 1){
            roll_back();
            cr++;
        }
        while(cl < i * block){
            del(cl++);
        }
        while(stk.size()) stk.pop();
        for(auto [l, r, id] : b[i]){
            while(r > cr){
                roll_back();
                cr++;
            }
            while(r < cr){
                del(cr--);
            }
            while(cl < l) del(cl++);
            ans[id] = cur;
            while(cl > i * block){
                roll_back();
                cl--;
            }
        }
    }
    for(int i = 0; i < q; i++) cout<<ans[i] / 2 + 1<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19292 KB Output is correct
2 Correct 3 ms 19292 KB Output is correct
3 Correct 4 ms 19288 KB Output is correct
4 Correct 5 ms 19292 KB Output is correct
5 Correct 4 ms 19292 KB Output is correct
6 Correct 4 ms 19292 KB Output is correct
7 Correct 5 ms 19292 KB Output is correct
8 Correct 7 ms 19288 KB Output is correct
9 Correct 5 ms 19288 KB Output is correct
10 Correct 5 ms 19292 KB Output is correct
11 Correct 5 ms 19292 KB Output is correct
12 Correct 4 ms 19288 KB Output is correct
13 Correct 4 ms 19292 KB Output is correct
14 Correct 3 ms 19288 KB Output is correct
15 Correct 5 ms 19292 KB Output is correct
16 Correct 5 ms 19292 KB Output is correct
17 Correct 5 ms 19292 KB Output is correct
18 Correct 5 ms 19292 KB Output is correct
19 Correct 5 ms 19292 KB Output is correct
20 Correct 4 ms 19292 KB Output is correct
21 Correct 5 ms 19292 KB Output is correct
22 Correct 5 ms 19292 KB Output is correct
23 Correct 5 ms 19292 KB Output is correct
24 Correct 4 ms 19536 KB Output is correct
25 Correct 5 ms 19288 KB Output is correct
26 Correct 5 ms 19288 KB Output is correct
27 Correct 4 ms 19288 KB Output is correct
28 Correct 3 ms 19292 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19292 KB Output is correct
2 Correct 3 ms 19292 KB Output is correct
3 Correct 4 ms 19288 KB Output is correct
4 Correct 5 ms 19292 KB Output is correct
5 Correct 4 ms 19292 KB Output is correct
6 Correct 4 ms 19292 KB Output is correct
7 Correct 5 ms 19292 KB Output is correct
8 Correct 7 ms 19288 KB Output is correct
9 Correct 5 ms 19288 KB Output is correct
10 Correct 5 ms 19292 KB Output is correct
11 Correct 5 ms 19292 KB Output is correct
12 Correct 4 ms 19288 KB Output is correct
13 Correct 4 ms 19292 KB Output is correct
14 Correct 3 ms 19288 KB Output is correct
15 Correct 5 ms 19292 KB Output is correct
16 Correct 5 ms 19292 KB Output is correct
17 Correct 5 ms 19292 KB Output is correct
18 Correct 5 ms 19292 KB Output is correct
19 Correct 5 ms 19292 KB Output is correct
20 Correct 4 ms 19292 KB Output is correct
21 Correct 5 ms 19292 KB Output is correct
22 Correct 5 ms 19292 KB Output is correct
23 Correct 5 ms 19292 KB Output is correct
24 Correct 4 ms 19536 KB Output is correct
25 Correct 5 ms 19288 KB Output is correct
26 Correct 5 ms 19288 KB Output is correct
27 Correct 4 ms 19288 KB Output is correct
28 Correct 3 ms 19292 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
30 Correct 13 ms 21596 KB Output is correct
31 Correct 16 ms 21596 KB Output is correct
32 Correct 18 ms 21592 KB Output is correct
33 Correct 19 ms 21824 KB Output is correct
34 Correct 19 ms 21596 KB Output is correct
35 Correct 6 ms 21596 KB Output is correct
36 Correct 8 ms 21596 KB Output is correct
37 Correct 6 ms 21776 KB Output is correct
38 Correct 18 ms 21848 KB Output is correct
39 Correct 18 ms 21852 KB Output is correct
40 Correct 18 ms 21852 KB Output is correct
41 Correct 6 ms 21852 KB Output is correct
42 Correct 6 ms 21852 KB Output is correct
43 Correct 7 ms 21852 KB Output is correct
44 Correct 18 ms 21880 KB Output is correct
45 Correct 18 ms 21852 KB Output is correct
46 Correct 18 ms 21872 KB Output is correct
47 Correct 6 ms 21596 KB Output is correct
48 Correct 7 ms 21596 KB Output is correct
49 Correct 7 ms 21696 KB Output is correct
50 Correct 18 ms 21596 KB Output is correct
51 Correct 17 ms 21596 KB Output is correct
52 Correct 18 ms 21596 KB Output is correct
53 Correct 18 ms 21596 KB Output is correct
54 Correct 22 ms 21828 KB Output is correct
55 Correct 17 ms 21596 KB Output is correct
56 Correct 14 ms 19676 KB Output is correct
57 Correct 4 ms 21596 KB Output is correct
58 Correct 18 ms 21848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 4 ms 19292 KB Output is correct
3 Correct 14 ms 19676 KB Output is correct
4 Correct 1925 ms 59416 KB Output is correct
5 Correct 1155 ms 71368 KB Output is correct
6 Incorrect 1833 ms 77504 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19288 KB Output is correct
2 Correct 577 ms 51656 KB Output is correct
3 Correct 1864 ms 55144 KB Output is correct
4 Correct 1018 ms 56744 KB Output is correct
5 Incorrect 110 ms 72496 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19472 KB Output is correct
2 Correct 6 ms 19400 KB Output is correct
3 Correct 19 ms 19676 KB Output is correct
4 Correct 3137 ms 56572 KB Output is correct
5 Correct 3085 ms 56700 KB Output is correct
6 Correct 3990 ms 72828 KB Output is correct
7 Incorrect 4430 ms 78920 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19292 KB Output is correct
2 Correct 3 ms 19292 KB Output is correct
3 Correct 4 ms 19288 KB Output is correct
4 Correct 5 ms 19292 KB Output is correct
5 Correct 4 ms 19292 KB Output is correct
6 Correct 4 ms 19292 KB Output is correct
7 Correct 5 ms 19292 KB Output is correct
8 Correct 7 ms 19288 KB Output is correct
9 Correct 5 ms 19288 KB Output is correct
10 Correct 5 ms 19292 KB Output is correct
11 Correct 5 ms 19292 KB Output is correct
12 Correct 4 ms 19288 KB Output is correct
13 Correct 4 ms 19292 KB Output is correct
14 Correct 3 ms 19288 KB Output is correct
15 Correct 5 ms 19292 KB Output is correct
16 Correct 5 ms 19292 KB Output is correct
17 Correct 5 ms 19292 KB Output is correct
18 Correct 5 ms 19292 KB Output is correct
19 Correct 5 ms 19292 KB Output is correct
20 Correct 4 ms 19292 KB Output is correct
21 Correct 5 ms 19292 KB Output is correct
22 Correct 5 ms 19292 KB Output is correct
23 Correct 5 ms 19292 KB Output is correct
24 Correct 4 ms 19536 KB Output is correct
25 Correct 5 ms 19288 KB Output is correct
26 Correct 5 ms 19288 KB Output is correct
27 Correct 4 ms 19288 KB Output is correct
28 Correct 3 ms 19292 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
30 Correct 13 ms 21596 KB Output is correct
31 Correct 16 ms 21596 KB Output is correct
32 Correct 18 ms 21592 KB Output is correct
33 Correct 19 ms 21824 KB Output is correct
34 Correct 19 ms 21596 KB Output is correct
35 Correct 6 ms 21596 KB Output is correct
36 Correct 8 ms 21596 KB Output is correct
37 Correct 6 ms 21776 KB Output is correct
38 Correct 18 ms 21848 KB Output is correct
39 Correct 18 ms 21852 KB Output is correct
40 Correct 18 ms 21852 KB Output is correct
41 Correct 6 ms 21852 KB Output is correct
42 Correct 6 ms 21852 KB Output is correct
43 Correct 7 ms 21852 KB Output is correct
44 Correct 18 ms 21880 KB Output is correct
45 Correct 18 ms 21852 KB Output is correct
46 Correct 18 ms 21872 KB Output is correct
47 Correct 6 ms 21596 KB Output is correct
48 Correct 7 ms 21596 KB Output is correct
49 Correct 7 ms 21696 KB Output is correct
50 Correct 18 ms 21596 KB Output is correct
51 Correct 17 ms 21596 KB Output is correct
52 Correct 18 ms 21596 KB Output is correct
53 Correct 18 ms 21596 KB Output is correct
54 Correct 22 ms 21828 KB Output is correct
55 Correct 17 ms 21596 KB Output is correct
56 Correct 14 ms 19676 KB Output is correct
57 Correct 4 ms 21596 KB Output is correct
58 Correct 18 ms 21848 KB Output is correct
59 Correct 3 ms 19288 KB Output is correct
60 Correct 4 ms 19292 KB Output is correct
61 Correct 14 ms 19676 KB Output is correct
62 Correct 1925 ms 59416 KB Output is correct
63 Correct 1155 ms 71368 KB Output is correct
64 Incorrect 1833 ms 77504 KB Output isn't correct
65 Halted 0 ms 0 KB -