This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
const int N = 5e3 + 5;
const int INF = 1e9;
vector<iii> vec;
vector<ii> lst[N];
int dist[N];
void solve_sub2(int n, int q, int w){
int last = 0;
while(q--){
int d, e; cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
vec[d].fi = e;
for(int i = 1; i <= n; ++i)lst[i].clear();
for(int i = 0; i < n - 1; ++i){
lst[vec[i].se.fi].pb(mp(vec[i].se.se, vec[i].fi));
lst[vec[i].se.se].pb(mp(vec[i].se.fi, vec[i].fi));
}
for(int i = 1; i <= n; ++i)dist[i] = INF;
priority_queue<ii, vector<ii>, greater<ii> > pq;
pq.push(mp(0, 1));
dist[1] = 0;
while(!pq.empty()){
ii cur = pq.top(); pq.pop();
for(int i = 0; i < lst[cur.se].size(); ++i){
int nx = lst[cur.se][i].fi;
int cs = cur.fi + lst[cur.se][i].se;
if(cs < dist[nx]){
dist[nx] = cs;
pq.push(mp(cs, nx));
}
}
}
ii maxdist = mp(-1, -1);
for(int i = 1; i <= n; ++i){
maxdist = max(maxdist, mp(dist[i], i));
}
for(int i = 1; i <= n; ++i)dist[i] = INF;
pq.push(mp(0, maxdist.se));
dist[maxdist.se] = 0;
while(!pq.empty()){
ii cur = pq.top(); pq.pop();
for(int i = 0; i < lst[cur.se].size(); ++i){
int nx = lst[cur.se][i].fi;
int cs = cur.fi + lst[cur.se][i].se;
if(cs < dist[nx]){
dist[nx] = cs;
pq.push(mp(cs, nx));
}
}
}
int ans = -1;
for(int i = 1; i <= n; ++i){
ans = max(ans, dist[i]);
}
last = ans;
cout << ans << '\n';
}
return;
}
map<int, int> mep;
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, q, w; cin >> n >> q >> w;
for(int i = 0; i < n - 1; ++i){
int a, b, c; cin >> a >> b >> c;
vec.pb(mp(c, mp(a, b)));
++mep[c];
}
if(n <= 100 && q <= 100){
solve_sub2(n, q, w);
return 0;
}
int last = 0;
while(q--){
int d, e; cin >> d >> e;
d = (d + last) % (n - 1);
e = (e + last) % w;
--mep[vec[d].fi];
if(mep[vec[d].fi] == 0)mep.erase(vec[d].fi);
++mep[e];
vec[d].fi = e;
auto it = mep.rbegin();
int ans = it->fi;
++it;
ans += it->fi;
last = ans;
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
diameter.cpp: In function 'void solve_sub2(int, int, int)':
diameter.cpp:38:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i = 0; i < lst[cur.se].size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~
diameter.cpp:56:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0; i < lst[cur.se].size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~
# | 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... |