Submission #660035

# Submission time Handle Problem Language Result Execution time Memory
660035 2022-11-20T09:10:12 Z 600Mihnea Dynamic Diameter (CEOI19_diameter) C++17
31 / 100
5000 ms 180048 KB
bool home = 0;
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll INF = (ll) 1e18 + 7;


struct SegmentTree {
  vector<ll> tree;
  vector<ll> lazy;
  int n;

  void init(int nn) {
    n = nn;
    tree.resize(4 * (n + 7), 0);
    lazy.resize(4 * (n + 7), 0);
  }

  void push(int v, int tl, int tr) {
    if (lazy[v]) {
      tree[v] += lazy[v];
      if (tl < tr) {
        lazy[2 * v] += lazy[v];
        lazy[2 * v + 1] += lazy[v];
      }
      lazy[v] = 0;
    }
  }

  void add(int v, int tl, int tr, int l, int r, ll x) {
    push(v, tl, tr);
    if (tr < l || r < tl) {
      return;
    }
    if (l <= tl && tr <= r) {
      lazy[v] += x;
      push(v, tl, tr);
      return;
    }
    int tm = (tl + tr) / 2;
    add(2 * v, tl, tm, l, r, x);
    add(2 * v + 1, tm + 1, tr, l, r, x);
    tree[v] = max(tree[2 * v], tree[2 * v + 1]);
  }

  ll getmax(int v, int tl, int tr, int l, int r) {
    push(v, tl, tr);
    if (tr < l || r < tl) {
      return -INF;
    }
    if (l <= tl && tr <= r) {
      return tree[v];
    }
    int tm = (tl + tr) / 2;
    return max(getmax(2 * v, tl, tm, l, r), getmax(2 * v + 1, tm + 1, tr, l, r));
  }

  void add(int l, int r, ll x) {
    add(1, 1, n, l, r, x);
  }

  ll getmax(int l, int r) {
    return getmax(1, 1, n, l, r);;
  }
};

const int N = (int) 1e5 + 7;
const int K = 20;
int n;
int q;
ll w;
int sum_edge[N];
ll val_edge[N];
vector<int> gindex[N];
SegmentTree trees[K];
bool blocked[N];
int sub[N];
int total_under;

void build_sub(int a, int p = -1) {
  sub[a] = 1;
  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (b == p || blocked[b]) {
      continue;
    }
    build_sub(b, a);
    sub[a] += sub[b];
  }
}

int fcen(int a, int p = -1) {
  int mx = total_under - sub[a];
  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (b == p || blocked[b]) {
      continue;
    }
    mx = max(mx, sub[b]);
  }
  if (mx <= total_under / 2) {
    return a;
  }
  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (b == p || blocked[b]) {
      continue;
    }
    if (mx == sub[b]) {
      return fcen(b, a);
    }
  }
}

int low[K][N];
int high[K][N];
int ord[K][N];
int where[K][N];
int vrt[K][N];
int top[K];
multiset<ll> s[N];

vector<ll> all;
vector<int> where2;
vector<pair<int, int>> seg;
int kek = -1;
multiset<int> tog;

void prep(int a, int p, int level) {
  sub[a] = 1;
  ord[level][++top[level]] = a;
  low[level][a] = top[level];
  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (b == p || blocked[b]) {
      continue;
    }
    where[level][I] = kek;
    vrt[level][I] = b;
    prep(b, a, level);
    sub[a] += sub[b];
  }
  high[level][a] = top[level];
}

void add(int i, ll delta) {
  for (int level = 0; level < K; level++) {
    if (where[level][i] == -1) {
      continue;
    }
    int b = vrt[level][i];
    int l = low[level][b], r = high[level][b];

    {
      ll cur = 0;
      if ((int) s[where2[where[level][i]]].size() == 0) {} else
      if ((int) s[where2[where[level][i]]].size() == 1) {
        cur = *s[where2[where[level][i]]].begin();
      } else {
        auto it = s[where2[where[level][i]]].end();
        it--;
        cur += *it;
        it--;
        cur += *it;
      }
      if (tog.count(cur)) {
        tog.erase(tog.find(cur));
      }
    }
    s[where2[where[level][i]]].erase(s[where2[where[level][i]]].find(trees[level].getmax(seg[where[level][i]].first, seg[where[level][i]].second)));
    trees[level].add(l, r, delta);
    all[where[level][i]] = trees[level].getmax(seg[where[level][i]].first, seg[where[level][i]].second);
    s[where2[where[level][i]]].insert(trees[level].getmax(seg[where[level][i]].first, seg[where[level][i]].second));

    {
      ll cur = 0;
      if ((int) s[where2[where[level][i]]].size() == 0) {} else
      if ((int) s[where2[where[level][i]]].size() == 1) {
        cur = *s[where2[where[level][i]]].begin();
      } else {
        auto it = s[where2[where[level][i]]].end();
        it--;
        cur += *it;
        it--;
        cur += *it;
      }
      tog.insert(cur);
    }

  }
}

void build_centroid(int a, int level) {
  build_sub(a);
  total_under = sub[a];
  a = fcen(a);
  blocked[a] = 1;


  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (blocked[b]) {
      continue;
    }

    kek++;
    all.push_back(0);
    vrt[level][I] = b;
    where[level][I] = kek;
    prep(b, a, level);
    seg.push_back({low[level][b], high[level][b]});
    where2.push_back(a);
    s[a].insert(0);
  }

  for (auto &I : gindex[a]) {
    int b = sum_edge[I] - a;
    if (blocked[b]) {
      continue;
    }
    build_centroid(b, level + 1);
  }
}

ll get_diam() {
  ll sol = 0;
  for (auto &s2 : s) {
    auto it = s2.end();
    ll cur = 0;
    it--;
    cur += *it;
    it--;
    cur += *it;
    sol = max(sol, cur);
  }
  return sol;
}

int xx[N], yy[N];

int main() {
  if (home == 0) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  } else {
    freopen ("input.txt", "r", stdin);
  }
  cin >> n >> q >> w;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      where[i][j] = -1;
    }
    trees[i].init(n);
  }
  for (int i = 1; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    xx[i] = a;
    yy[i] = b;
    sum_edge[i] = a + b;
    gindex[a].push_back(i);
    gindex[b].push_back(i);
    val_edge[i] = c;
  }
  build_centroid(1, 0);
  for (int i = 1; i < n; i++) {
    add(i, val_edge[i]);
  }
  ll last = 0;
  for (int iq = 1; iq <= q; iq++) {
    int d, e;
    cin >> d >> e;
    d = (d + last) % (n - 1) + 1;
    e = (e + last) % w;
    add(d, -val_edge[d] + e);
    val_edge[d] = e;
    if (tog.empty()) {
      last = 0;
    } else {
      auto it = tog.end();
      it--;
      last = *it;
    }

    cout << last << "\n";

  }
  return 0;
}

Compilation message

diameter.cpp: In function 'int fcen(int, int)':
diameter.cpp:115:1: warning: control reaches end of non-void function [-Wreturn-type]
  115 | }
      | ^
diameter.cpp: In function 'int main()':
diameter.cpp:247:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  247 |     freopen ("input.txt", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15192 KB Output is correct
3 Correct 8 ms 15236 KB Output is correct
4 Correct 8 ms 15200 KB Output is correct
5 Correct 7 ms 15196 KB Output is correct
6 Correct 8 ms 15316 KB Output is correct
7 Correct 8 ms 15316 KB Output is correct
8 Correct 8 ms 15316 KB Output is correct
9 Correct 8 ms 15320 KB Output is correct
10 Correct 9 ms 15324 KB Output is correct
11 Correct 9 ms 15408 KB Output is correct
12 Correct 8 ms 15316 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 8 ms 15452 KB Output is correct
15 Correct 8 ms 15444 KB Output is correct
16 Correct 9 ms 15444 KB Output is correct
17 Correct 8 ms 15444 KB Output is correct
18 Correct 9 ms 15448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15192 KB Output is correct
3 Correct 8 ms 15236 KB Output is correct
4 Correct 8 ms 15200 KB Output is correct
5 Correct 7 ms 15196 KB Output is correct
6 Correct 8 ms 15316 KB Output is correct
7 Correct 8 ms 15316 KB Output is correct
8 Correct 8 ms 15316 KB Output is correct
9 Correct 8 ms 15320 KB Output is correct
10 Correct 9 ms 15324 KB Output is correct
11 Correct 9 ms 15408 KB Output is correct
12 Correct 8 ms 15316 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 8 ms 15452 KB Output is correct
15 Correct 8 ms 15444 KB Output is correct
16 Correct 9 ms 15444 KB Output is correct
17 Correct 8 ms 15444 KB Output is correct
18 Correct 9 ms 15448 KB Output is correct
19 Correct 36 ms 16980 KB Output is correct
20 Correct 42 ms 17004 KB Output is correct
21 Correct 47 ms 17024 KB Output is correct
22 Correct 53 ms 17048 KB Output is correct
23 Correct 83 ms 23296 KB Output is correct
24 Correct 121 ms 23488 KB Output is correct
25 Correct 125 ms 23608 KB Output is correct
26 Correct 143 ms 24196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15188 KB Output is correct
2 Correct 8 ms 15320 KB Output is correct
3 Correct 9 ms 15208 KB Output is correct
4 Correct 21 ms 15464 KB Output is correct
5 Correct 73 ms 16320 KB Output is correct
6 Correct 9 ms 15200 KB Output is correct
7 Correct 8 ms 15956 KB Output is correct
8 Correct 9 ms 15956 KB Output is correct
9 Correct 10 ms 15956 KB Output is correct
10 Correct 28 ms 16168 KB Output is correct
11 Correct 102 ms 17276 KB Output is correct
12 Correct 17 ms 22356 KB Output is correct
13 Correct 17 ms 22356 KB Output is correct
14 Correct 19 ms 22452 KB Output is correct
15 Correct 41 ms 22740 KB Output is correct
16 Correct 136 ms 23856 KB Output is correct
17 Correct 242 ms 156016 KB Output is correct
18 Correct 231 ms 155964 KB Output is correct
19 Correct 243 ms 156012 KB Output is correct
20 Correct 270 ms 156220 KB Output is correct
21 Correct 531 ms 157648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 16928 KB Output is correct
2 Correct 75 ms 17020 KB Output is correct
3 Correct 247 ms 17572 KB Output is correct
4 Correct 483 ms 18312 KB Output is correct
5 Correct 135 ms 31332 KB Output is correct
6 Correct 210 ms 31532 KB Output is correct
7 Correct 577 ms 32144 KB Output is correct
8 Correct 985 ms 33008 KB Output is correct
9 Correct 881 ms 96164 KB Output is correct
10 Correct 941 ms 96412 KB Output is correct
11 Correct 1511 ms 97000 KB Output is correct
12 Correct 2391 ms 97848 KB Output is correct
13 Correct 2017 ms 178580 KB Output is correct
14 Correct 2178 ms 178544 KB Output is correct
15 Correct 2799 ms 179320 KB Output is correct
16 Correct 3602 ms 180048 KB Output is correct
17 Execution timed out 5089 ms 179784 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5087 ms 144596 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15316 KB Output is correct
2 Correct 8 ms 15192 KB Output is correct
3 Correct 8 ms 15236 KB Output is correct
4 Correct 8 ms 15200 KB Output is correct
5 Correct 7 ms 15196 KB Output is correct
6 Correct 8 ms 15316 KB Output is correct
7 Correct 8 ms 15316 KB Output is correct
8 Correct 8 ms 15316 KB Output is correct
9 Correct 8 ms 15320 KB Output is correct
10 Correct 9 ms 15324 KB Output is correct
11 Correct 9 ms 15408 KB Output is correct
12 Correct 8 ms 15316 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 8 ms 15452 KB Output is correct
15 Correct 8 ms 15444 KB Output is correct
16 Correct 9 ms 15444 KB Output is correct
17 Correct 8 ms 15444 KB Output is correct
18 Correct 9 ms 15448 KB Output is correct
19 Correct 36 ms 16980 KB Output is correct
20 Correct 42 ms 17004 KB Output is correct
21 Correct 47 ms 17024 KB Output is correct
22 Correct 53 ms 17048 KB Output is correct
23 Correct 83 ms 23296 KB Output is correct
24 Correct 121 ms 23488 KB Output is correct
25 Correct 125 ms 23608 KB Output is correct
26 Correct 143 ms 24196 KB Output is correct
27 Correct 8 ms 15188 KB Output is correct
28 Correct 8 ms 15320 KB Output is correct
29 Correct 9 ms 15208 KB Output is correct
30 Correct 21 ms 15464 KB Output is correct
31 Correct 73 ms 16320 KB Output is correct
32 Correct 9 ms 15200 KB Output is correct
33 Correct 8 ms 15956 KB Output is correct
34 Correct 9 ms 15956 KB Output is correct
35 Correct 10 ms 15956 KB Output is correct
36 Correct 28 ms 16168 KB Output is correct
37 Correct 102 ms 17276 KB Output is correct
38 Correct 17 ms 22356 KB Output is correct
39 Correct 17 ms 22356 KB Output is correct
40 Correct 19 ms 22452 KB Output is correct
41 Correct 41 ms 22740 KB Output is correct
42 Correct 136 ms 23856 KB Output is correct
43 Correct 242 ms 156016 KB Output is correct
44 Correct 231 ms 155964 KB Output is correct
45 Correct 243 ms 156012 KB Output is correct
46 Correct 270 ms 156220 KB Output is correct
47 Correct 531 ms 157648 KB Output is correct
48 Correct 19 ms 16928 KB Output is correct
49 Correct 75 ms 17020 KB Output is correct
50 Correct 247 ms 17572 KB Output is correct
51 Correct 483 ms 18312 KB Output is correct
52 Correct 135 ms 31332 KB Output is correct
53 Correct 210 ms 31532 KB Output is correct
54 Correct 577 ms 32144 KB Output is correct
55 Correct 985 ms 33008 KB Output is correct
56 Correct 881 ms 96164 KB Output is correct
57 Correct 941 ms 96412 KB Output is correct
58 Correct 1511 ms 97000 KB Output is correct
59 Correct 2391 ms 97848 KB Output is correct
60 Correct 2017 ms 178580 KB Output is correct
61 Correct 2178 ms 178544 KB Output is correct
62 Correct 2799 ms 179320 KB Output is correct
63 Correct 3602 ms 180048 KB Output is correct
64 Execution timed out 5089 ms 179784 KB Time limit exceeded
65 Halted 0 ms 0 KB -