Submission #867002

# Submission time Handle Problem Language Result Execution time Memory
867002 2023-10-27T14:58:49 Z NeroZein Dynamic Diameter (CEOI19_diameter) C++17
49 / 100
562 ms 1048576 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 1e5 + 5;

int n; 
long long dis[N];
long long weight[N]; 
int cnt, in[N], out[N]; 
vector<pair<int, int>> g[N]; 

struct node {
  long long mx = 0, mn = 0, mxmn = 0, mnmx = 0, ans = 0, lazy = 0;
}seg[N * 4];

node merge(node& lx, node& rx) {
  node ret;
  ret.mx = max(lx.mx, rx.mx);
  ret.mn = min(lx.mn, rx.mn);
  ret.mxmn = max({lx.mxmn, rx.mxmn, lx.mx - 2LL * rx.mn});
  ret.mnmx = max({lx.mnmx, rx.mnmx, - 2LL * lx.mn + rx.mx}); 
  ret.ans = max({lx.ans, rx.ans, lx.mx + rx.mnmx, lx.mxmn + rx.mx});
  return ret;
}

void push(int nd, int l, int r) {
  if (!seg[nd].lazy) {
    return;
  }
  seg[nd].mx += seg[nd].lazy;
  seg[nd].mn += seg[nd].lazy;
  seg[nd].mxmn -= seg[nd].lazy;
  seg[nd].mnmx -= seg[nd].lazy; 
  if (l != r) {
    int mid = (l + r) >> 1;
    int rs = nd + ((mid - l + 1) << 1);
    seg[nd + 1].lazy += seg[nd].lazy;
    seg[rs].lazy += seg[nd].lazy; 
  }
  seg[nd].lazy = 0;
}

void upd(int nd, int l, int r, int s, int e, long long v) {
  push(nd, l, r); 
  if (l >= s && r <= e) {    
    seg[nd].lazy = v;
    push(nd, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  int rs = nd + ((mid - l + 1) << 1);
  if (mid >= e) {
    upd(nd + 1, l, mid, s, e, v);
    push(rs, mid + 1, r); 
  } else {
    if (mid < s) {
      push(nd + 1, l, mid);
      upd(rs, mid + 1, r, s, e, v); 
    } else {
      upd(nd + 1, l, mid, s, e, v);
      upd(rs, mid + 1, r, s, e, v); 
    }
  }
  seg[nd] = merge(seg[nd + 1], seg[rs]); 
}

void dfs(int v, int pe) {
  cnt++; 
  for (auto [u, i] : g[v]) {
    if (i == pe) {
      continue;
    }
    dis[u] = dis[v] + weight[i]; 
    in[i] = cnt; 
    dfs(u, i); 
    out[i] = cnt - 1;
    upd(0, 0, 2 * n - 2, in[i], out[i], weight[i]); 
    cnt++; 
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int q;
  long long lim;
  cin >> n >> q >> lim;
  for(int i = 0; i < n - 1; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    weight[i] = w; 
    g[u].emplace_back(v, i);
    g[v].emplace_back(u, i); 
  }
  dfs(1, n); 
  long long last = 0;
  while (q--) {
    long long d, c;
    cin >> d >> c;
    d = (d + last) % (n - 1);
    c = (c + last) % lim;
    upd(0, 0, 2 * n - 2, in[d], out[d], -weight[d]);
    weight[d] = c; 
    upd(0, 0, 2 * n - 2, in[d], out[d], +weight[d]);
    last = max(seg[0].mx, seg[0].ans);
    cout << last << '\n';  
  } 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4488 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4488 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 5 ms 4696 KB Output is correct
20 Correct 6 ms 4700 KB Output is correct
21 Correct 6 ms 4700 KB Output is correct
22 Correct 7 ms 4924 KB Output is correct
23 Correct 9 ms 5708 KB Output is correct
24 Correct 9 ms 5724 KB Output is correct
25 Correct 10 ms 5724 KB Output is correct
26 Correct 12 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 8 ms 4700 KB Output is correct
5 Correct 52 ms 5576 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 2 ms 4572 KB Output is correct
9 Correct 3 ms 4700 KB Output is correct
10 Correct 12 ms 4832 KB Output is correct
11 Correct 69 ms 5956 KB Output is correct
12 Correct 4 ms 5724 KB Output is correct
13 Correct 4 ms 5724 KB Output is correct
14 Correct 6 ms 5724 KB Output is correct
15 Correct 17 ms 5976 KB Output is correct
16 Correct 71 ms 7004 KB Output is correct
17 Correct 61 ms 28904 KB Output is correct
18 Correct 79 ms 28808 KB Output is correct
19 Correct 63 ms 28952 KB Output is correct
20 Correct 98 ms 29300 KB Output is correct
21 Correct 197 ms 30532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4700 KB Output is correct
2 Correct 10 ms 4920 KB Output is correct
3 Correct 61 ms 5444 KB Output is correct
4 Correct 84 ms 5976 KB Output is correct
5 Correct 8 ms 7000 KB Output is correct
6 Correct 20 ms 7108 KB Output is correct
7 Correct 66 ms 7692 KB Output is correct
8 Correct 119 ms 8508 KB Output is correct
9 Correct 37 ms 16984 KB Output is correct
10 Correct 62 ms 16968 KB Output is correct
11 Correct 106 ms 17692 KB Output is correct
12 Correct 188 ms 18540 KB Output is correct
13 Correct 75 ms 29356 KB Output is correct
14 Correct 101 ms 29532 KB Output is correct
15 Correct 154 ms 30172 KB Output is correct
16 Correct 231 ms 31060 KB Output is correct
17 Correct 189 ms 30744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 562 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4440 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4488 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 5 ms 4696 KB Output is correct
20 Correct 6 ms 4700 KB Output is correct
21 Correct 6 ms 4700 KB Output is correct
22 Correct 7 ms 4924 KB Output is correct
23 Correct 9 ms 5708 KB Output is correct
24 Correct 9 ms 5724 KB Output is correct
25 Correct 10 ms 5724 KB Output is correct
26 Correct 12 ms 5980 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 2 ms 4444 KB Output is correct
30 Correct 8 ms 4700 KB Output is correct
31 Correct 52 ms 5576 KB Output is correct
32 Correct 1 ms 4440 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 2 ms 4572 KB Output is correct
35 Correct 3 ms 4700 KB Output is correct
36 Correct 12 ms 4832 KB Output is correct
37 Correct 69 ms 5956 KB Output is correct
38 Correct 4 ms 5724 KB Output is correct
39 Correct 4 ms 5724 KB Output is correct
40 Correct 6 ms 5724 KB Output is correct
41 Correct 17 ms 5976 KB Output is correct
42 Correct 71 ms 7004 KB Output is correct
43 Correct 61 ms 28904 KB Output is correct
44 Correct 79 ms 28808 KB Output is correct
45 Correct 63 ms 28952 KB Output is correct
46 Correct 98 ms 29300 KB Output is correct
47 Correct 197 ms 30532 KB Output is correct
48 Correct 3 ms 4700 KB Output is correct
49 Correct 10 ms 4920 KB Output is correct
50 Correct 61 ms 5444 KB Output is correct
51 Correct 84 ms 5976 KB Output is correct
52 Correct 8 ms 7000 KB Output is correct
53 Correct 20 ms 7108 KB Output is correct
54 Correct 66 ms 7692 KB Output is correct
55 Correct 119 ms 8508 KB Output is correct
56 Correct 37 ms 16984 KB Output is correct
57 Correct 62 ms 16968 KB Output is correct
58 Correct 106 ms 17692 KB Output is correct
59 Correct 188 ms 18540 KB Output is correct
60 Correct 75 ms 29356 KB Output is correct
61 Correct 101 ms 29532 KB Output is correct
62 Correct 154 ms 30172 KB Output is correct
63 Correct 231 ms 31060 KB Output is correct
64 Correct 189 ms 30744 KB Output is correct
65 Runtime error 562 ms 1048576 KB Execution killed with signal 9
66 Halted 0 ms 0 KB -