제출 #1134978

#제출 시각아이디문제언어결과실행 시간메모리
1134978mychecksedadTourism (JOI23_tourism)C++20
0 / 100
347 ms58412 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; Fenwick(int _n){ n = _n; t.resize(n+1, 0); } void add(int v, int x){ while(v <= n){ t[v] += x; v += (v&-v); } } int get(int v){ int res = 0; while(v > 0){ res += t[v]; v -= (v&-v); } return res; } }; int n, m, q, c[N], up[N][K], ans[N], heavy[N], tin[N], dep[N], tout[N], s[N], timer = 1; vector<int> g[N]; vector<pii> Q[N]; void pre(int v, int p){ s[v] = 1; dep[v] = dep[p] + 1; up[v][0] = p; for(int u: g[v]){ if(u != p) pre(u, v); } } void dfs(int v, int p){ tin[v] = timer++; for(int u: g[v]){ if(u != p){ if(u == g[v][0]){ heavy[g[v][0]] = heavy[v]; }else{ heavy[u] = u; } dfs(u, v); } } tout[v] = timer - 1; } bool is(int u, int v){ return tin[u] <= tin[v] && tout[v] <= tout[u]; } int _lca(int u, int v){ if(is(u, v)) return u; if(is(v, u)) return v; for(int j = K-1; j >= 0; --j){ if(is(up[u][j], v) == 0) u = up[u][j]; } return up[u][0]; } int go_up(int u, int d){ for(int j = K-1; j >= 0; --j){ if(d&(1<<j)){ u = up[u][j]; } } return u; } 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); } 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); for(int v = 1; v <= n; ++v){ sort(all(g[v]), [&](const int &x, const int &y){ return s[x] > s[y]; }); swap(g[v][0], g[v].back()); } heavy[1] = 1; dfs(1, 1); for(int j = 1; j < K; ++j){ for(int i = 1; i <= n; ++i){ up[i][j] = up[up[i][j - 1]][j - 1]; } } Fenwick fw(n); set<array<int, 3>> S; for(int i = 1; i <= m; ++i){ if(i > 1){ int u = c[i - 1]; int v = c[i]; int lca = _lca(u, v); while(u != lca){ int L, R; if(dep[heavy[u]] > dep[lca]){ L = tin[heavy[u]]; R = tin[u]; u = up[heavy[u]][0]; }else{ L = tin[go_up(u, dep[u] - dep[lca] - 1)]; R = tin[u]; u = lca; } 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); } u = v; while(u != lca){ int L, R; if(dep[heavy[u]] > dep[lca]){ L = tin[heavy[u]]; R = tin[u]; u = up[heavy[u]][0]; }else{ L = tin[go_up(u, dep[u] - dep[lca] - 1)]; R = tin[u]; u = lca; } 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); } } 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(); en; } 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...