Submission #697324

# Submission time Handle Problem Language Result Execution time Memory
697324 2023-02-09T08:51:40 Z NintsiChkhaidze Dynamic Diameter (CEOI19_diameter) C++14
0 / 100
559 ms 1048576 KB
#include <bits/stdc++.h>
#define ll long long
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
#define pb push_back
#define pii pair<int,int>
#define s second
#define f first
using namespace std;
 
const int N = 100005,M=22; // ?
vector <int> v[N],con[N];
multiset <ll> st[N],bigset;
ll value[N],wi[N];
int vis[N],xi[N],yi[N],comp[M],DP[N];
int n,centroid,CH,L;
int tin[M][N],tout[M][N],PAR[M][N],dep[M][N];
ll t[M][4*N],add[M][4*N];
multiset <ll> :: iterator it;
 
void push(int k,int h){
    if (!add[k][h]) return;
    add[k][h*2] += add[k][h];
    t[k][h*2] += add[k][h];
    add[k][h*2+1] += add[k][h];
    t[k][h*2+1] += add[k][h];
    add[k][h] = 0LL;
}
void upd(int k,int h,int l,int r,int L,int R,ll val){
    if (r < L || R < l) return;
    if (L <= l && r <= R){
        t[k][h] += val;
        add[k][h] += val;
        return;
    }
    push(k,h);
    upd(k,left,L,R,val);
    upd(k,right,L,R,val);
    t[k][h] = max(t[k][h*2],t[k][h*2+1]);
}
 
ll get(int k,int h,int l,int r,int L,int R){
    if (r < L || R < l) return 0LL;
    if (L <= l && r <= R) return t[k][h];
    push(k,h);
    return max(get(k,left,L,R),get(k,right,L,R));
}
int dfs(int x,int par,int root,int m){
    int s = 1;
    for (auto q: v[x]){
        int to = x^xi[q]^yi[q];
        if (to != par && !vis[to]) s += dfs(to,x,root, m);
    }
    if (centroid == -1 && (x == root || s*2 >= m)) centroid = x;
    return s;
}
 
void DFS(int x,int par,int id,int cent){
    tin[id][x] = ++comp[id];
    PAR[id][x] = CH;
 
    for (auto q: v[x]){
        int to = x^xi[q]^yi[q];
        if (to == par || vis[to]) continue;
 
        con[q].pb(cent);
        dep[id][to] = dep[id][x] + 1; 
        if (dep[id][x] == 1) CH = to;
 
        DFS(to,x,id,cent);
        upd(id,1,1,n,tin[id][to],tout[id][to],+wi[q]);
    }
 
    tout[id][x] = comp[id];
}
void C(int x,int m,int lvl){
    centroid = -1;
    dfs(x, x, x, m);
    vis[centroid] = 1;
 
    L = max(L,lvl);
    DP[centroid] = lvl;
    dep[lvl][centroid] = 1;
    DFS(centroid,centroid,lvl,centroid);
 
    for (auto q: v[centroid]){
        int to = centroid^xi[q]^yi[q];
        if (!vis[to]) C(to,m/2,lvl + 1);
    }
}
int main(){
    // ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
    
    int q; ll W;
    scanf("%d%d%lld",&n,&q,&W);
    // cin>>n>>q>>W;
 
    for (int i = 1; i < n; i++){
        cin>>xi[i]>>yi[i]>>wi[i];
        v[xi[i]].pb(i);
        v[yi[i]].pb(i);
    }
 
    C(1,n,1);
    for (int id = 1; id <= L; id++){
        for (int root = 1; root <= n; root++){
            if (dep[id][root] != 1) continue;
        
            for (auto q: v[root]){
                int to = root^xi[q]^yi[q];
                if (dep[id][to] != 2) continue;
                st[root].insert(get(id,1,1,n,tin[id][to],tout[id][to]));
            }
 
            if (st[root].size() == 1) value[root] = *st[root].begin();
            else if (st[root].size() > 1){
                it = --st[root].end(); --it;
                value[root] = *(--st[root].end()) + *it;
            }
            bigset.insert(value[root]);
        }
    }
 
    ll last=0LL;
    while (q--){
        int d; ll len;
        scanf("%d%lld",&d,&len);
        // cin>>d>>len;
        d = (d + last)%(n-1) + 1;
        len = (len + last)%W;
 
        int x = xi[d],y =yi[d]; ll delta = len - wi[d]; 
        wi[d] = len;
 
        for (int root: con[d]){
            bigset.erase(bigset.find(value[root]));
            int id = DP[root];
            if (dep[id][y] > dep[id][x]){
                int parent = PAR[id][y];
                st[root].erase(st[root].find(get(id,1,1,n,tin[id][parent],tout[id][parent])));
                upd(id,1,1,n,tin[id][y],tout[id][y],delta);
                st[root].insert(get(id,1,1,n,tin[id][parent],tout[id][parent]));
            }
            else{
                int parent = PAR[id][x]; 
                st[root].erase(st[root].find(get(id,1,1,n,tin[id][parent],tout[id][parent])));
                upd(id,1,1,n,tin[id][x],tout[id][x],delta);
                st[root].insert(get(id,1,1,n,tin[id][parent],tout[id][parent]));
            }
 
            if (st[root].size() == 1) value[root] = *st[root].begin();
            else if (st[root].size() > 1){
                it = --st[root].end(); --it;
                value[root] = *(--st[root].end()) + *it;
            }
            bigset.insert(value[root]);
        }
        last = *(--bigset.end());
        printf("%lld\n",last);
        // cout<<(last = *(--bigset.end()))<<"\n"; 
    }
    
}

Compilation message

diameter.cpp: In function 'int main()':
diameter.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d%d%lld",&n,&q,&W);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
diameter.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         scanf("%d%lld",&d,&len);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 19796 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 19796 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9804 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 17 ms 9872 KB Output is correct
5 Correct 67 ms 10188 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 7 ms 9812 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 7 ms 9812 KB Output is correct
10 Correct 23 ms 9976 KB Output is correct
11 Correct 91 ms 10312 KB Output is correct
12 Correct 10 ms 10836 KB Output is correct
13 Correct 13 ms 10848 KB Output is correct
14 Correct 14 ms 10916 KB Output is correct
15 Correct 34 ms 10952 KB Output is correct
16 Correct 128 ms 11420 KB Output is correct
17 Runtime error 119 ms 48388 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 559 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 313 ms 102260 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 15 ms 19796 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -