Submission #1253200

#TimeUsernameProblemLanguageResultExecution timeMemory
1253200thdh__Tourism (JOI23_tourism)C++20
100 / 100
747 ms32004 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

/*
Competitive Programming notes that I need to study & fix my dumbass self:

1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you

2. Stress: 
- Always try generating big testcases and try if they run

3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time

Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'

+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'

I hate offline because I am dumb
*/

typedef pair<int, int> ii;
const int N = 2e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int bit[N];

void update(int i, int val) 
{
	for (; i < N; i += i & -i) bit[i] += val;
}

int get(int i) 
{
	int ret = 0;
	for (; i; i -= i & -i) ret += bit[i];
	return ret;
}

int n,m,q;
vector<int> adj[N];
vector<ii> qu[N];
int c[N], ans[N];

int sz[N], head[N], heavy[N], par[N], h[N];
int timer = 0, tin[N], tout[N];

void dfs(int u, int p)
{
	sz[u] = 1;
	for (auto v : adj[u]) 
	{
		if (v == p) continue;
		h[v] = h[u]+1;
		dfs(v, u);
		par[v] = u;
		sz[u] += sz[v];
		if (sz[heavy[u]] < sz[v]) heavy[u] = v;
	}
}

void decompose(int u, int p) 
{
	head[u] = p;
	tin[u] = ++timer;
	if (heavy[u]) decompose(heavy[u], p);
	for (auto v : adj[u]) 
	{
		if (v == par[u] || v == heavy[u]) continue;
		decompose(v, v);
	}
	tout[u] = timer;
}

set<pair<ii, int>> seg;

void upd_seg(int l, int r, int id) 
{
	while (true) 
	{
		auto it = seg.lower_bound({{l, 0}, 0});
		if (it == seg.end() || (*it).fi.se > r) break;
		auto tmp = *it;
		int R = tmp.fi.fi, L = tmp.fi.se, ID = tmp.se;
		// cout<<"Erase segment: "<<L<<" "<<R<<" "<<ID<<endl;
		seg.erase(it);
		update(ID, -max(min(R, r) - max(L, l) + 1, 0ll));
		if (L < l) 
		{
			seg.insert({{l-1, L}, ID});
		}
		if (R > r) 
		{
			seg.insert({{R, r+1}, ID});
		}
	}
	update(id, r-l+1);
	// cout<<"Add segment: "<<l<<" "<<r<<" "<<id<<endl;
	seg.insert({{r, l}, id});
}

void upd(int u, int v, int id) 
{
	for (; head[u] != head[v]; u = par[head[u]]) 
	{
		if (h[head[u]] < h[head[v]]) swap(u, v);
		// cout<<"Update path: "<<head[u]<<" "<<u<<endl;
		upd_seg(tin[head[u]], tin[u], id);
	}
	if (h[u] > h[v]) swap(u, v);
	// cout<<"Update path: "<<u<<" "<<v<<endl;
	if (h[u] > h[v]) swap(u, v);
	if (u != v) upd_seg(tin[u] + 1, tin[v], id);
}

void solve()
{
	cin>>n>>m>>q;
	for (int i = 1; i < n; i++) 
	{
		int u,v; cin>>u>>v;
		adj[u].pb(v); adj[v].pb(u);
	}
	dfs(1, 0);
	decompose(1, 1);
	// for (int i = 1; i <= n; i++) cout<<tin[i]<<" ";
	// cout<<endl;
	// for (int i = 1; i <= n; i++) cout<<head[i]<<" ";
	// cout<<endl;
	for (int i = 1; i <= m; i++) cin>>c[i];
	for (int i = 0; i < q; i++) 
	{
		int l,r; cin>>l>>r;
		qu[r].pb({l, i});
	}
	// seg.insert({{tin[c[1]], tin[c[1]]}, 1});
	// update(1, 1);
	for (int i = 2; i <= m; i++) 
	{
		// cout<<"Path: "<<c[i-1]<<" "<<c[i]<<endl;
		upd(c[i-1], c[i], i);
		for (auto j : qu[i]) 
		{
			ans[j.se] = get(i) - get(j.fi);
		}	
		// for (int j = i; j >= 1; j--) cout<<get(j) - get(j-1)<<" ";
		// cout<<endl;
	}
	for (int i = 0; i < q; i++) cout<<ans[i]+1<<endl;
}

/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	int t = 1;
	// cin>>t;
	while (t--)
	{
		solve();
		cout<<"\n";
	}
}
#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...