제출 #889697

#제출 시각아이디문제언어결과실행 시간메모리
889697vjudge1Tourism (JOI23_tourism)C++17
7 / 100
134 ms23388 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_multiset tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;

using namespace std;
using namespace __gnu_pbds;

const int mod = 1e9+7;
const double PI = acos(-1.0);
const double epsilon = 1e-6;
const int N = 1e5+5;
vector<int> g[N];
int c[N],t2[N*4],t[N*4],dis[N],up[20][N],tin[N],tout[N];
int timer;

void dfs(int v, int pr,int d) {
	dis[v] = d;
    tin[v] = ++timer;
    up[0][v] = pr;
    for (int i = 1; i <= 17; i++) {
        up[i][v] = up[i - 1][ up[i - 1][v] ]; // parent of the parent
    }
    for (int to : g[v]) {
        if (to != pr) dfs(to, v, d + 1);
    }
    tout[v] = timer;
}
 
bool upper(int a, int b) {
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}
 
int lca(int a, int b) {
    if (upper(a, b)) return a;
    if (upper(b, a)) return b;
    for (int i = 17; i >= 0; i--) {
        if (!upper(up[i][a], b)) {
            a = up[i][a];
        }
    }
    return up[0][a];
}

void build(int v, int l, int r){
	if(l == r){
		t[v] = c[l];
		t2[v] = c[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(v * 2, l, mid);
	build(v * 2 + 1, mid + 1, r);
	t[v] = min(t[v * 2], t[v * 2 + 1]);
	t2[v] = max(t2[v * 2], t2[v * 2 + 1]);
}
 
int getmn(int v, int tl, int tr, int l, int r) {
    if (l > tr || tl > r) return 1e18;
    if (l <= tl && tr <= r)return t[v];
    int tm = (tl + tr) / 2;
    return min(getmn(v * 2, tl, tm, l, r),getmn(v * 2 + 1, tm + 1, tr, l, r));
}

int getmx(int v, int tl, int tr, int l, int r) {
    if (l > tr || tl > r) return 0;
    if (l <= tl && tr <= r)return t2[v];
    int tm = (tl + tr) / 2;
    return max(getmx(v * 2, tl, tm, l, r),getmx(v * 2 + 1, tm + 1, tr, l, r));
}

void solve(){
	int n,m,q;
	cin >> n >> m >> q;
	bool ok = 1;
	for(int i = 1; i < n; i++){
		int u,v; cin >> u >> v;
		g[u].pb(v); g[v].pb(u);
		if(!(u == i && v == i+1)) ok = 0;
	}
	for(int i = 1; i <= m; i++){
		cin >> c[i];
	}
	if(ok){
		build(1,1,m);
		while(q--){
			int l,r; cin >> l >> r;
			cout << getmx(1,1,m,l,r) - getmn(1,1,m,l,r) + 1 << '\n';
		}
		return;
	}
	dfs(1,0,0);
	
	vector<int> lc(m);
	for(int i = 0; i < m-1; i++){
		lc[i] = lca(c[i],c[i+1]);
	}
	while(q--){
		int l,r; cin >> l >> r;
		l--;r--;
		int ans = 1;
		for(int i = l; i < r; i++){
			if(i == l){
				ans += dis[c[i]] + dis[c[i+1]] - dis[lc[i]] * 2;
			}else{
				
				ans += dis[c[i]] + dis[c[i+1]] - dis[lc[i]] * 2 - (dis[c[i]] - dis[lc[i-1]] + 1);
			}
			cout << ans << ' ';
		}
		cout << ans << '\n';
	}
}

main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--) {
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

tourism.cpp:123:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  123 | main(){
      | ^~~~
#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...