Submission #658539

# Submission time Handle Problem Language Result Execution time Memory
658539 2022-11-13T12:27:51 Z mychecksedad Dynamic Diameter (CEOI19_diameter) C++17
0 / 100
121 ms 175076 KB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD1 (1000000000+7)
#define MOD (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;


int n, q;
bool case3 = 1, case4 = 1;
pair<int, ll> par[N];
ll w, dp[N];
vector<int> g[N];
vector<pair<int, ll>> edges(N);
multiset<ll> s, dps[N], answers;

ll f(multiset<ll> &a){
    if(a.empty()) return 0;
    if(a.size() == 1) return *(a.begin());
    auto it = a.end(); --it;
    ll x = *it; --it; x += *it;
    return x;
}

ll dfs(int v, int p){
    pair<ll, ll> best = {0, 0};    
    dp[v] = 0;
    ll d = 0;
    for(int E: g[v]){
        auto e = edges[E];
        int u = e.first;
        if(u == p) continue;

        d = max(d, dfs(u, v));

        par[u] = {v, e.second};

        dp[v] = max(dp[v], dp[u] + e.second);
        
        if(best.first < dp[u] + e.second){
            swap(best.first, best.second);
            best.first = dp[u] + e.second;
        }else if(best.second < dp[u] + e.second){
            best.second = dp[u] + e.second;
        }
        if(case4)
            dps[v].insert(dp[u] + e.second);
    }
    if(case4)
        answers.insert(best.first + best.second);

    return max(d, best.first + best.second);
}

void solve(){
    cin >> n >> q >> w;
    assert(0);
    for(int i = 0; i < n - 1; ++i){
        int u, v; ll x; cin >> u >> v >> x;
        g[u].pb(2*i);
        g[v].pb(2*i+1);

        if(u != 1 && v != 1) case3 = 0;
        if(v/2 != u && u/2 != v) case4 = 0;
        s.insert(x);

        edges[i*2] = {v, x};
        edges[i*2+1] = {u, x};
    }
    
    if(case4) dfs(1, 0);

    ll last = 0;
    for(;q--;){
        ll d, x; cin >> d >> x;
        d = (d + last) % (n-1);
        x = (x + last) % w;
        ll old_x = edges[2*d].second;
        edges[2*d].second = edges[2*d+1].second = x;
        
        if(case3){
            s.erase(s.find(old_x));
            s.insert(x);
            auto it = s.end(); --it;
            if(s.size()==1) last = *it;
            else{
                last = *it;
                it--;
                last += *it;
            }
        }else if(case4){
            int v = edges[2*d].first;
            if(par[v].first != edges[2*d+1].first) v = edges[2*d].first;    

            ll cur = 0, delta = x - old_x;

            if(dps[v].size() > 0) cur = *(--dps[v].end());

            par[v].second = x;
            
            while(v != 1){
                cur += par[v].second;
                v = par[v].first;

                answers.erase(answers.find(f(dps[v])));

                dps[v].erase(dps[v].find(cur - delta));
                dps[v].insert(cur);
                
                answers.insert(f(dps[v]));
            }
            last = *(--answers.end());
        }else{
            // answers.clear();
            last = dfs(1, 0);
            // last = *(--answers.end());
        }
        cout << last << '\n';
    }
}





int main(){
    cin.tie(0); ios::sync_with_stdio(0);
    int T = 1, aa;
    // cin >> T;aa=T;
    while(T--){
        // cout << "Case #" << aa-T << ": ";
        solve();
        cout << '\n';
    }
    return 0;
 
}

Compilation message

diameter.cpp: In function 'int main()':
diameter.cpp:137:16: warning: unused variable 'aa' [-Wunused-variable]
  137 |     int T = 1, aa;
      |                ^~
# Verdict Execution time Memory Grader output
1 Runtime error 110 ms 175004 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 110 ms 175004 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 112 ms 175004 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 110 ms 175076 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 121 ms 175000 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 110 ms 175004 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -