Submission #964075

# Submission time Handle Problem Language Result Execution time Memory
964075 2024-04-16T09:33:50 Z LucaLucaM Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 100748 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <queue>
#warning That's not AS, that's my AS
#pragma GCC optimize ("Ofast")

typedef long long ll;

const int NMAX = 2e5;
const int DMAX = 40;

int L;

struct Node {
  int value, lazy;
};

struct segtree {
  std::vector<Node> aint;
  std::vector<int> brute;
  int n;
  segtree() {}
  segtree(int _n) {
    n = 1;
    while (n <= _n) {
      n <<= 1;
    }
    n <<= 1;
    n |= 1;
    aint.resize(n);
    for (int i = 0; i < n; i++) {
      aint[i] = {1, 1};
    }
    n = _n;
    brute.resize(n + 1, 1);
  }

  void push(int node, int tl, int tr) {
    if (aint[node].lazy != 1) {
      aint[node].value = (ll) aint[node].value * aint[node].lazy % L;
      if (tl != tr) {
        aint[2 * node].lazy = (ll) aint[node].lazy * aint[2 * node].lazy % L;
        aint[2 * node + 1].lazy = (ll) aint[node].lazy * aint[2 * node + 1].lazy % L;
      }
      aint[node].lazy = 1;
    }
  }
  void upd(int node, int tl, int tr, int l, int r, int v) {
    assert(1 <= tl && tl <= tr && tr <= n);
    push(node, tl, tr);
    if (l <= tl && tr <= r) {
      aint[node].lazy = v;
    } else {
      int mid = (tl + tr) / 2;
      if (l <= mid) {
        upd(2 * node, tl, mid, l, r, v);
      }
      if (mid < r) {
        upd(2 * node + 1, mid + 1, tr, l, r, v);
      }
      push(2 * node, tl, mid);
      push(2 * node + 1, mid + 1, tr);
      aint[node].value = (ll) aint[2 * node].value * aint[2 * node + 1].value;
    }
  }

  int qry(int node, int tl, int tr, int p) {
    push(node, tl, tr);
    if (tl == tr) {
      return aint[node].value;
    } else {
      int mid = (tl + tr) / 2;
      if (p <= mid) {
        return qry(2 * node, tl, mid, p);
      }
      return qry(2 * node + 1, mid + 1, tr, p);
    }
  }

  void bruteUpdate(int l, int r, int v) {
    for (int i = l; i <= r; i++) {
      brute[i] = (ll) brute[i] * v % L;
    }
  }
  int bruteQuery(int p) {
    return brute[p];
  }

  void update(int l, int r, int v) {
    if (l > r) {
      return;
    }
    if (r - l <= 20) {
      bruteUpdate(l, r, v);
    } else {
      upd(1, 1, n, l, r, v);
    }
  }
  int query(int p) {
    return (ll) bruteQuery(p) * qry(1, 1, n, p) % L;
  }
};

std::vector<int> g[NMAX + 1];
int parent[NMAX + 1];
int Time[NMAX + 1];
int timer;
int lt[NMAX + 1][DMAX + 1];
int rt[NMAX + 1][DMAX + 1];

void bfs() {
  std::queue<int> q;
  q.push(1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    Time[u] = ++timer;
    std::vector<int> newG;
    for (const auto &v : g[u]) {
      if (!Time[v]) {
        parent[v] = u;
        newG.push_back(v);
        q.push(v);
      }
    }
    g[u] = newG;
  }
}

void dfs(int u) {
  lt[u][0] = rt[u][0] = Time[u];
  for (int d = 1; d <= DMAX; d++) {
    lt[u][d] = NMAX + 1;
    rt[u][d] = 0;
  }
  for (const auto &v : g[u]) {
    assert(v != parent[u]);
    dfs(v);
    for (int d = 0; d < DMAX; d++) {
      lt[u][d + 1] = std::min(lt[u][d + 1], lt[v][d]);
      rt[u][d + 1] = std::max(rt[u][d + 1], rt[v][d]);
    }
  }
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);

  int n;
  std::cin >> n >> L;

  for (int i = 1; i < n; i++) {
    int u, v;
    std::cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  bfs();
  dfs(1);

  segtree aint(n);

  for (int i = 1; i <= n; i++) {
    int h;
    std::cin >> h;
    aint.update(Time[i], Time[i], h);
  }

  int q;
  std::cin >> q;

  while (q--) {
    int type;
    std::cin >> type;
    if (type == 1) {
      int u, d, h;
      std::cin >> u >> d >> h;
      std::vector<std::pair<int, int>> ancestors;
      int v = u;
      for (int i = 0; i <= d && v != 0; i++) {
        ancestors.push_back({v, d - i});
        v = parent[v];
      }
      std::reverse(ancestors.begin(), ancestors.end());
      int maxDepth = -1;
      for (const auto &[v, depth] : ancestors) {
        /// i + depth > maxDepth => i > maxDepth - depth
        /// i + d - depth <= d <=> i <= depth
        for (int i = std::max(0, maxDepth - depth + 1); i <= depth; i++) {
          aint.update(lt[v][i], rt[v][i], h);
          maxDepth = i + depth;
        }
      }
    } else {
      int u;
      std::cin >> u;
      std::cout << aint.query(Time[u]) << '\n';
    }
  }

  return 0;
}

Compilation message

sprinkler.cpp:7:2: warning: #warning That's not AS, that's my AS [-Wcpp]
    7 | #warning That's not AS, that's my AS
      |  ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 3 ms 9320 KB Output is correct
5 Correct 4 ms 9396 KB Output is correct
6 Correct 5 ms 9332 KB Output is correct
7 Correct 4 ms 9304 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 9048 KB Output is correct
10 Correct 2 ms 9048 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9048 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 2 ms 9052 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9052 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9052 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 3 ms 9048 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 2 ms 9048 KB Output is correct
24 Correct 2 ms 9048 KB Output is correct
25 Correct 2 ms 9148 KB Output is correct
26 Correct 3 ms 9052 KB Output is correct
27 Correct 3 ms 9048 KB Output is correct
28 Correct 2 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 333 ms 85612 KB Output is correct
3 Correct 425 ms 82524 KB Output is correct
4 Correct 449 ms 94056 KB Output is correct
5 Correct 375 ms 84072 KB Output is correct
6 Correct 388 ms 84056 KB Output is correct
7 Correct 390 ms 84468 KB Output is correct
8 Correct 383 ms 84912 KB Output is correct
9 Correct 441 ms 100748 KB Output is correct
10 Correct 470 ms 96388 KB Output is correct
11 Correct 361 ms 85512 KB Output is correct
12 Correct 395 ms 82404 KB Output is correct
13 Correct 496 ms 83600 KB Output is correct
14 Correct 489 ms 83332 KB Output is correct
15 Correct 416 ms 83464 KB Output is correct
16 Correct 432 ms 83488 KB Output is correct
17 Correct 427 ms 83996 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 3 ms 9052 KB Output is correct
20 Correct 2 ms 9052 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 2 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 333 ms 85612 KB Output is correct
3 Correct 425 ms 82524 KB Output is correct
4 Correct 449 ms 94056 KB Output is correct
5 Correct 375 ms 84072 KB Output is correct
6 Correct 388 ms 84056 KB Output is correct
7 Correct 390 ms 84468 KB Output is correct
8 Correct 383 ms 84912 KB Output is correct
9 Correct 441 ms 100748 KB Output is correct
10 Correct 470 ms 96388 KB Output is correct
11 Correct 361 ms 85512 KB Output is correct
12 Correct 395 ms 82404 KB Output is correct
13 Correct 496 ms 83600 KB Output is correct
14 Correct 489 ms 83332 KB Output is correct
15 Correct 416 ms 83464 KB Output is correct
16 Correct 432 ms 83488 KB Output is correct
17 Correct 427 ms 83996 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 3 ms 9052 KB Output is correct
20 Correct 2 ms 9052 KB Output is correct
21 Correct 3 ms 9052 KB Output is correct
22 Correct 2 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 324 ms 85556 KB Output is correct
25 Correct 421 ms 82436 KB Output is correct
26 Correct 495 ms 98376 KB Output is correct
27 Correct 407 ms 84084 KB Output is correct
28 Correct 415 ms 84224 KB Output is correct
29 Correct 424 ms 84184 KB Output is correct
30 Correct 443 ms 84872 KB Output is correct
31 Correct 429 ms 96040 KB Output is correct
32 Correct 536 ms 96448 KB Output is correct
33 Correct 352 ms 85588 KB Output is correct
34 Correct 458 ms 82492 KB Output is correct
35 Correct 2 ms 9048 KB Output is correct
36 Correct 2 ms 9052 KB Output is correct
37 Correct 2 ms 9052 KB Output is correct
38 Correct 3 ms 9052 KB Output is correct
39 Correct 3 ms 9140 KB Output is correct
40 Correct 2 ms 9052 KB Output is correct
41 Correct 2 ms 9052 KB Output is correct
42 Correct 2 ms 9052 KB Output is correct
43 Correct 3 ms 9304 KB Output is correct
44 Correct 2 ms 9048 KB Output is correct
45 Correct 2 ms 9048 KB Output is correct
46 Correct 2 ms 9048 KB Output is correct
47 Correct 2 ms 9052 KB Output is correct
48 Correct 2 ms 9052 KB Output is correct
49 Correct 2 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 641 ms 97712 KB Output is correct
3 Correct 1745 ms 93436 KB Output is correct
4 Correct 839 ms 94844 KB Output is correct
5 Correct 1032 ms 82400 KB Output is correct
6 Correct 2001 ms 82900 KB Output is correct
7 Correct 1380 ms 83084 KB Output is correct
8 Correct 388 ms 83708 KB Output is correct
9 Correct 512 ms 93384 KB Output is correct
10 Correct 1672 ms 96004 KB Output is correct
11 Correct 774 ms 82992 KB Output is correct
12 Correct 3288 ms 82532 KB Output is correct
13 Execution timed out 4029 ms 83116 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 597 ms 95488 KB Output is correct
3 Correct 1513 ms 90324 KB Output is correct
4 Correct 827 ms 93000 KB Output is correct
5 Correct 1068 ms 84264 KB Output is correct
6 Correct 1985 ms 84256 KB Output is correct
7 Correct 1582 ms 84096 KB Output is correct
8 Correct 382 ms 84800 KB Output is correct
9 Correct 510 ms 99568 KB Output is correct
10 Correct 1645 ms 96696 KB Output is correct
11 Correct 653 ms 85648 KB Output is correct
12 Correct 3414 ms 82524 KB Output is correct
13 Execution timed out 4030 ms 89756 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 3 ms 9320 KB Output is correct
5 Correct 4 ms 9396 KB Output is correct
6 Correct 5 ms 9332 KB Output is correct
7 Correct 4 ms 9304 KB Output is correct
8 Correct 3 ms 9308 KB Output is correct
9 Correct 2 ms 9048 KB Output is correct
10 Correct 2 ms 9048 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9048 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 2 ms 9052 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9052 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9052 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 3 ms 9048 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 2 ms 9048 KB Output is correct
24 Correct 2 ms 9048 KB Output is correct
25 Correct 2 ms 9148 KB Output is correct
26 Correct 3 ms 9052 KB Output is correct
27 Correct 3 ms 9048 KB Output is correct
28 Correct 2 ms 9052 KB Output is correct
29 Correct 2 ms 9052 KB Output is correct
30 Correct 333 ms 85612 KB Output is correct
31 Correct 425 ms 82524 KB Output is correct
32 Correct 449 ms 94056 KB Output is correct
33 Correct 375 ms 84072 KB Output is correct
34 Correct 388 ms 84056 KB Output is correct
35 Correct 390 ms 84468 KB Output is correct
36 Correct 383 ms 84912 KB Output is correct
37 Correct 441 ms 100748 KB Output is correct
38 Correct 470 ms 96388 KB Output is correct
39 Correct 361 ms 85512 KB Output is correct
40 Correct 395 ms 82404 KB Output is correct
41 Correct 496 ms 83600 KB Output is correct
42 Correct 489 ms 83332 KB Output is correct
43 Correct 416 ms 83464 KB Output is correct
44 Correct 432 ms 83488 KB Output is correct
45 Correct 427 ms 83996 KB Output is correct
46 Correct 2 ms 9052 KB Output is correct
47 Correct 3 ms 9052 KB Output is correct
48 Correct 2 ms 9052 KB Output is correct
49 Correct 3 ms 9052 KB Output is correct
50 Correct 2 ms 9052 KB Output is correct
51 Correct 2 ms 9052 KB Output is correct
52 Correct 324 ms 85556 KB Output is correct
53 Correct 421 ms 82436 KB Output is correct
54 Correct 495 ms 98376 KB Output is correct
55 Correct 407 ms 84084 KB Output is correct
56 Correct 415 ms 84224 KB Output is correct
57 Correct 424 ms 84184 KB Output is correct
58 Correct 443 ms 84872 KB Output is correct
59 Correct 429 ms 96040 KB Output is correct
60 Correct 536 ms 96448 KB Output is correct
61 Correct 352 ms 85588 KB Output is correct
62 Correct 458 ms 82492 KB Output is correct
63 Correct 2 ms 9048 KB Output is correct
64 Correct 2 ms 9052 KB Output is correct
65 Correct 2 ms 9052 KB Output is correct
66 Correct 3 ms 9052 KB Output is correct
67 Correct 3 ms 9140 KB Output is correct
68 Correct 2 ms 9052 KB Output is correct
69 Correct 2 ms 9052 KB Output is correct
70 Correct 2 ms 9052 KB Output is correct
71 Correct 3 ms 9304 KB Output is correct
72 Correct 2 ms 9048 KB Output is correct
73 Correct 2 ms 9048 KB Output is correct
74 Correct 2 ms 9048 KB Output is correct
75 Correct 2 ms 9052 KB Output is correct
76 Correct 2 ms 9052 KB Output is correct
77 Correct 2 ms 9052 KB Output is correct
78 Correct 2 ms 9052 KB Output is correct
79 Correct 641 ms 97712 KB Output is correct
80 Correct 1745 ms 93436 KB Output is correct
81 Correct 839 ms 94844 KB Output is correct
82 Correct 1032 ms 82400 KB Output is correct
83 Correct 2001 ms 82900 KB Output is correct
84 Correct 1380 ms 83084 KB Output is correct
85 Correct 388 ms 83708 KB Output is correct
86 Correct 512 ms 93384 KB Output is correct
87 Correct 1672 ms 96004 KB Output is correct
88 Correct 774 ms 82992 KB Output is correct
89 Correct 3288 ms 82532 KB Output is correct
90 Execution timed out 4029 ms 83116 KB Time limit exceeded
91 Halted 0 ms 0 KB -