제출 #1324040

#제출 시각아이디문제언어결과실행 시간메모리
1324040sonarchtTourism (JOI23_tourism)C++20
28 / 100
5095 ms39332 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(v) begin(v), end(v)
#define pll pair<ll, ll>
#define fi first
#define se second
#define vll vector<ll>
#define mll map<ll, ll>
mt19937_64 rng(chrono::high_resolution_clock().now().time_since_epoch().count());
const ll N = 1e5 + 6, inf = 1e18 + 7, mod = 1e9 + 7, B = 310;
ll n, m, k, q, ans[N], mp[N], a[N], res = 0;

vll adj[N];
ll timer = 0, tin[N], tout[N], tour[N], dep[N], li[N][21];
void dfs(ll p, ll u) {
	tin[u] = ++timer;
	tour[timer] = u;
	for (ll v : adj[u]) {
		if (v == p) continue;
		dep[v] = dep[u] + 1;
		li[v][0] = u;
		for (int j = 1; j <= 20; ++j) li[v][j] = li[li[v][j-1]][j-1];
		dfs(u, v);
	}
	tout[u] = timer;
}

bool is_anc(ll u, ll v) {
	return (tin[u] <= tin[v] && tout[v] <= tout[u]);
}

ll lca(ll u, ll v) {
	if (is_anc(u, v)) return u;
	if (is_anc(v, u)) return v;
	for (int j = __lg(dep[u]); j >= 0; --j) {
		if (li[u][j] && !is_anc(li[u][j], v)) u = li[u][j];
	}
	return li[u][0];
}

ll dist(ll u, ll v) {
	ll p = lca(u, v);
	return dep[u] + dep[v] - 2*dep[p];
}

struct cmp {
	bool operator () (ll a, ll b) const {
		return tin[a] < tin[b];
	}
};
set<ll, cmp> s;
void add(ll u) {
	++mp[u];
	if (mp[u] != 1) return;
	if (s.empty()) {
		s.insert(u);
		return;
	}
	auto it = s.lower_bound(u);
	auto prv = (it == s.begin() ? prev(s.end()) : prev(it));
	auto nxt = (it == s.end() ? s.begin() : it);
	res += -dist(*prv, *nxt) + dist(u, *nxt) + dist(u, *prv);
	s.insert(u);
}

void del(ll u) {
	--mp[u];
	if (mp[u] != 0) return;
	if (s.size() == 1 || s.size() == 2) {
		res = 0;
		s.erase(u);
		return;
	}
	auto it = s.find(u);
	auto prv = (it == s.begin() ? prev(s.end()) : prev(it));
	++it;
	auto nxt = (it == s.end() ? s.begin() : it);
	res += dist(*prv, *nxt) - dist(u, *nxt) - dist(u, *prv);
	s.erase(u);
}

struct Query {
	ll l, r, idx;
	bool operator < (const Query &o) const {
		if (l/B != o.l/B) return l < o.l;
		if (l/B & 1) return r > o.r;
		return r < o.r;
	}
} qry[N];
ll l = 1, r = 0;
void solve() {
	cin >> n >> m >> q;
	for (int i = 1; i < n; ++i) {
		ll x, y;
		cin >> x >> y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	dfs(-1, 1);
	for (int i = 1; i <= m; ++i) cin >> a[i];
	for (int i = 1; i <= q; ++i) {
		ll l, r;
		cin >> l >> r;
		qry[i] = {l, r, i};
	}
	sort(qry+1, qry+q+1);
	for (int i = 1; i <= q; ++i) {
		auto [ql, qr, idx] = qry[i];
		while (r < qr) add(a[++r]);
		while (r > qr) del(a[r--]);
		while (l > ql) add(a[--l]);
		while (l < ql) del(a[l++]);
		ans[idx] = res / 2 + 1;
		//for (ll u : s) cout << u << " "; cout << endl;
	}
	for (int i = 1; i <= q; ++i) cout << ans[i] << endl;
	return;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	if (fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	ll tc = 1;
//	cin >> tc;
	while (tc--) {
		solve();
	}
	return 0;
}

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

tourism.cpp: In function 'int main()':
tourism.cpp:129:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
tourism.cpp:130:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |                 freopen(".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...