Submission #1135104

#TimeUsernameProblemLanguageResultExecution timeMemory
1135104mychecksedadTourism (JOI23_tourism)C++20
0 / 100
5095 ms61452 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; #define ll long long int #define MOD (1000000000+7) #define MOD1 (998244353) #define pb push_back #define all(x) x.begin(), x.end() #define en cout << '\n' #define ff first #define ss second #define pii pair<int,int> #define vi vector<int> const int N = 1e6+100, M = 1e5+10, K = 22, MX = 30; struct Fenwick{ int n; vector<int> t; void init(int _n){ n = _n; t.resize(n+1, 0); } void add(int v, int x){ for(; v <= n; v+=(v&-v)) t[v] += x; } int get(int v){ int res = 0; for(; v > 0; v -= (v&-v)) res += t[v]; return res; } }; int n, m, q, c[N], rmq[N][K], ans[N], heavy[N], tin[N], dep[N], s[N], timer = 1, par[N], pos[N]; vector<int> g[N]; vector<pii> Q[N]; vi E; void pre(int v, int p){ s[v] = 1; dep[v] = dep[p] + 1; par[v] = p; for(int &u: g[v]){ if(u != p){ pre(u, v), s[v] += s[u]; if(g[v][0]==p || s[u]>s[g[v][0]]) swap(u, g[v][0]); } } } void dfs(int v, int p){ tin[v] = E.size(); E.pb(v); for(int u: g[v]){ if(u != p){ if(u == g[v][0]){ heavy[u] = heavy[v]; }else{ heavy[u] = u; } dfs(u, v); E.pb(v); } } } // bool is(int u, int v){ // return tin[u] <= tin[v] && tout[v] <= tout[u]; // } int _lca(int u, int v){ int mn = min(tin[u], tin[v]); int mx = max(tin[u], tin[v]); int k = int(log2(mx-mn+1)); return tin[rmq[mn][k]] <= tin[rmq[mx-(1<<k)+1][k]] ? rmq[mn][k] : rmq[mx-(1<<k)+1][k]; } Fenwick fw; set<array<int, 3>> S; void add(int L, int R, int i){ auto it = S.lower_bound(array<int, 3>{L, -1, -1}); if(it != S.begin()){ auto itt = prev(it); if((*itt)[1] >= L){ auto p = *itt; S.erase(itt); fw.add(p[2], -(p[1] - p[0] + 1)); if(p[1] > R){ S.insert({p[0], L - 1, p[2]}); S.insert({R + 1, p[1], p[2]}); fw.add(p[2], L - 1 - p[0] + 1); fw.add(p[2], p[1] - (R + 1) + 1); }else{ fw.add(p[2], L - 1 - p[0] + 1); S.insert({p[0], L - 1, p[2]}); } it = S.lower_bound(array<int, 3>{L, -1, -1}); } } while(it != S.end() && (*it)[0] <= R){ auto p = *it; S.erase(it); fw.add(p[2], - (p[1] - p[0] + 1)); if(p[1] > R){ S.insert({R + 1, p[1], p[2]}); fw.add(p[2], p[1] - (R + 1) + 1); } it = S.lower_bound(array<int, 3>{L, -1, -1}); } S.insert({L, R, i}); fw.add(i, R-L+1); } void solve(){ cin >> n >> m >> q; for(int i = 0; i + 1 < n; ++i){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } fw.init(m); for(int i = 1; i <= m; ++i){ cin >> c[i]; } for(int i = 1; i <= q; ++i){ int l, r; cin >> l >> r; Q[r].pb({l, i}); } dep[1] = 0; pre(1, 1); heavy[1] = 1; dfs(1, 1); for(int i = 1; i <= E.size(); ++i){ rmq[i][0] = E[i - 1]; } for(int j = 1; j < K; ++j){ for(int i = 1; i + (1<<j) <= E.size() + 1; ++i){ rmq[i][j] = (tin[rmq[i][j - 1]] <= tin[rmq[i+(1<<(j-1))][j-1]] ? rmq[i][j-1] : rmq[i+(1<<(j-1))][j-1]); } } for(int i = 1; i <= m; ++i){ if(i > 1){ int u = c[i - 1]; int v = c[i]; int lca = _lca(u, v); for(int rep = 0; rep < 2; ++rep){ while(u != lca){ int L, R; if(dep[heavy[u]] > dep[lca]){ L = tin[heavy[u]]; R = tin[u]; u = par[heavy[u]]; }else{ L = tin[lca]+1; R = tin[u]; u = lca; } // assert(L <= R); add(L, R, i); } u = v; } } for(auto qq: Q[i]){ ans[qq.ss] = fw.get(i) - fw.get(qq.ff); } } for(int i = 1; i <= q; ++i) cout << ans[i] + 1 << '\n'; } int main(){ cin.tie(0); ios::sync_with_stdio(0); int tt = 1, aa; // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(tt--){ solve(); } cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...