# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
794015 | vjudge1 | Tourism (JOI23_tourism) | C++17 | 5035 ms | 252648 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Author : حسن
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 2e5 + 9 , mod = 1e9 + 7;
ll c[N] = {} , d[N] = {} , a[N] = {}, b[N] = {} , us[N] , us1[N] , st[20][N][2] , dp[N] = {};
vector<int>v[N];
set<pair<int,int>>st1[N / 2];
void dfs(int n,int p = 0){
d[n] = d[p] + 1;
for(auto to : v[n])
if(to != p)
dfs(to , n);
}
void dfs2(int n,int p = 0){
for(auto to1 : v[n])
if(to1 != p){
dfs2(to1 , n);
if(st1[to1].size() > st1[n].size())
swap(st1[to1] , st1[n]);
for(auto to : st1[to1]){
auto it = st1[n].lower_bound({to.fi , 0});
if(it != st1[n].end() && it->fi == to.fi){
c[to.fi] += max(to.se - d[n] - 1 ,0ll);
if(to.se != d[n] && it->se != d[n])
c[to.fi]++;
c[to.fi] += max(it->se - d[n] - 1 ,0ll);
st1[n].erase(it);
st1[n].insert({to.fi , d[n]});
}else
st1[n].insert(to);
}
}
}
ll ans;
void dfs1(int n,int p = 0){
for(auto to : v[n])
if(to != p)
dfs1(to , n);
ll s = 0;
for(auto to : v[n])
if(b[to] != 0)
if(to != p)
s++, b[n] = b[to];
if(s != 0){
if(s == 1 && us[n] == 0){
}else {
ans++;
for(auto to : v[n])
if(to != p && b[to] != 0){
ans += d[b[to]] - d[n] - 1;
}
b[n] = n;
}
}else if(us[n] == 1){
ans++;
b[n] = n;
}
}
pair<ll,ll> get(int x , int y){
ll i = dp[y - x + 1];
ll minimum = min(st[i][x][1], st[i][y - (1 << i) + 1][1]);
ll maximum = max(st[i][x][0], st[i][y - (1 << i) + 1][0]);
//cout<<i<<" "<<minimum<<" "<<maximum<<" ";
return {maximum , minimum};
}
void get1(){
ll x ,y , i;
x = 0;
y = 1;
dp[0] = 0;
for(i = 1; i <= 2e5; i++){
if(2 * y <= i)
x++, y *= 2;
dp[i] = x;
}
}
void solve(){
ll q , i , j , m , n , z , s = 0, f, l , r , k , x , y , mn = 1e18 , mx = 0;
cin>>n>>m>>q;
for(i = 1; i < n; i++){
cin>>l>>r;
v[l].pb(r);
v[r].pb(l);
if(!(l == i && r == i + 1))
s++;
}
for(i = 1; i <= m; i++)
cin>>a[i];
dfs(1);
if(s && n <= 2000 && m <= 2000 && q <= 2000){
while(q--){
cin>>l>>r;
for(i = 1; i <= n; i++){
us[i] = b[i] = c[i] = 0;
}
for(i = l; i <= r; i++)
us[a[i]] = 1;
ans = 0;
dfs1(1);
cout<<ans<<"\n";
}
}else if(s == 0){
get1();
for(i = 1; i <= m; i++)
st[0][i][0] = st[0][i][1] = d[a[i]];
for (int i = 1; i < 18; i++)
for (int j = 1; j + (1 << i) <= (m + 2); j++){
st[i][j][0] = max(st[i - 1][j][0], st[i - 1][j + (1 << (i - 1))][0]);
st[i][j][1] = min(st[i - 1][j][1], st[i - 1][j + (1 << (i - 1))][1]);
}
while(q--){
cin>>l>>r;
cout<<get(l , r).fi - get(l , r).se + 1<<"\n";
}
}else {
for(i = 1; i <= q; i++){
cin>>l>>r;
for(j = l; j <= r; j++){
if(us[a[j]] != i){ us[a[j]] = i , c[i]++;}
st1[a[j]].insert({i , d[a[j]]});
}
}
dfs2( 1);
for(i = 1; i <= q; i++)
cout<<c[i]<<"\n";
}
}
int main(){
TL;
/*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
*/
int t = 1;
//cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |