Submission #618856

#TimeUsernameProblemLanguageResultExecution timeMemory
618856Je_ODynamic Diameter (CEOI19_diameter)C++17
31 / 100
2024 ms7932 KiB
// #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 = 1e5 + 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;
        queue<int> q;
        q.push(1);
        dist[1] = 0;
        while(!q.empty()){
            int cur = q.front(); q.pop();
            for(int i = 0; i < lst[cur].size(); ++i){
                int nx = lst[cur][i].fi;
                int cs = dist[cur] + lst[cur][i].se;
                if(cs < dist[nx]){
                    dist[nx] = cs;
                    q.push(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;
        q.push(maxdist.se);
        dist[maxdist.se] = 0;
        while(!q.empty()){
            int cur = q.front(); q.pop();
            for(int i = 0; i < lst[cur].size(); ++i){
                int nx = lst[cur][i].fi;
                int cs = dist[cur] + lst[cur][i].se;
                if(cs < dist[nx]){
                    dist[nx] = cs;
                    q.push(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 <= 5000 && q <= 5000){
        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.end();
        --it;
        int ans = it->fi;
        if(it->se > 1)ans += it->fi;
        else{
            --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].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].size(); ++i){
      |                            ~~^~~~~~~~~~~~~~~~~
#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...