Submission #619167

# Submission time Handle Problem Language Result Execution time Memory
619167 2022-08-02T10:20:54 Z Zanite Dynamic Diameter (CEOI19_diameter) C++17
0 / 100
5000 ms 20684 KB
#include <bits/stdc++.h>
using namespace std;

using ll	= long long;
using pll	= pair<ll, ll>;
using Edge	= pair<ll, pll>;

#define fi	first
#define se	second

const int maxN	= 1e5 + 5;

ll n, q, w;
set<pll> adj[maxN];
vector<Edge> edges;

vector<ll> dist;

namespace Subtask12 {

void dfs(ll cur, ll par, ll cdist) {
	dist[cur] = cdist;
	for (auto &[nxt, w] : adj[cur]) {
		if (nxt == par) continue;
		dfs(nxt, cur, cdist+w);
	}
}

ll solve() {
	dist.assign(n+1, 0);
	dfs(1, 0, 0);

	ll e1 = -1, mx = -1;
	for (ll i = 1; i <= n; i++) {
		if (dist[i] > mx) {
			mx = dist[i];
			e1 = i;
		}
	}

	dist.assign(n+1, 0);
	dfs(e1, 0, 0);

	ll ret = 0;
	for (ll i = 1; i <= n; i++) {
		ret = max(ret, dist[i]);
	}
	return ret;
}

void caller() {
	ll last = 0;
	cout << solve() << '\n';
	for (ll d, e, i = 1; i <= q; i++) {
		scanf("%lld %lld", &d, &e);
		d = (d + last) % (n - 1);
		e = (e + last) % w;

		auto &[w, p] = edges[d];

		adj[p.fi].erase({p.se, w});
		adj[p.se].erase({p.fi, w});

		edges[d].fi = e;
		adj[p.fi].insert({p.se, e});
		adj[p.se].insert({p.fi, e});

		/*
		for (ll i = 1; i <= n; i++) {
			cout << i << ": ";
			for (auto &[j, k] : adj[i]) {
				cout << "{" << j << ", " << k << "} ";
			}
			cout << '\n';
		}
		*/

		last = solve();
		printf("%lld\n", last);
	}
}

}

namespace Subtask3 {
ll id[maxN], dist[maxN];
multiset<ll> dpq;

void caller() {
	dpq.insert(0);

	for (ll i = 0; i < n-1; i++) {
		ll c = edges[i].fi;
		auto &[a, b] = edges[i].se;

		if (b == 1) swap(a, b);

		id[i-1] = b;
		dist[b] = c;
		dpq.insert(c);
	}

	ll last = 0;
	for (ll d, e, i = 1; i <= q; i++) {
		scanf("%lld %lld", &d, &e);
		d = (d + last) % (n - 1);
		e = (e + last) % w;

		ll change = id[d];

		dpq.erase(dpq.lower_bound(dist[change]));
		dist[change] = e;
		dpq.insert(e);

		auto it = dpq.rbegin();
		last = *it;
		it++;
		last += *it;

		printf("%lld\n", last);
	}
}

}

int main() {
	scanf("%lld %lld %lld", &n, &q, &w);

	bool Star = 1;
	for (ll a, b, c, i = 1; i < n; i++) {
		scanf("%lld %lld %lld", &a, &b, &c);
		adj[a].insert({b, c});
		adj[b].insert({a, c});
		edges.push_back({c, {a, b}});

		if (a != 1 || b != 1) {Star = 0;}
	}

	if (Star) {
		Subtask3::caller();
	} else {
		Subtask12::caller();
	}
	
}

Compilation message

diameter.cpp: In function 'void Subtask12::caller()':
diameter.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |   scanf("%lld %lld", &d, &e);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'void Subtask3::caller()':
diameter.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |   scanf("%lld %lld", &d, &e);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'int main()':
diameter.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |  scanf("%lld %lld %lld", &n, &q, &w);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diameter.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |   scanf("%lld %lld %lld", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 33 ms 5172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5081 ms 20684 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 4904 KB Output isn't correct
2 Halted 0 ms 0 KB -