Submission #857130

# Submission time Handle Problem Language Result Execution time Memory
857130 2023-10-05T12:10:59 Z tvladm2009 Two Currencies (JOI23_currencies) C++17
100 / 100
2704 ms 275948 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll
const int N = 100000 + 7;
const int K = 20;
int n;
int m;
int q;
vector<int> g[N];
pair<int, int> edges[N];
int dep[N];
int par[N];
int l[N];
int r[N];
int tab[K][N];
int tt = 0;
int sub[N];
pair<int, int> updates[N];

void build(int a, int p = 0) {
  l[a] = ++tt;
  tab[0][a] = p;
  for (int k = 1; k < K; k++) {
    tab[k][a] = tab[k - 1][tab[k - 1][a]];
  }
  sub[a] = 1;
  for (auto &b : g[a]) {
    if (b != p) {
      par[b] = a;
      dep[b] = dep[a] + 1;
      build(b, a);
      sub[a] += sub[b];
    }
  }
  r[a] = tt;
}

int lca(int x, int y) {
  if (dep[x] < dep[y]) {
    swap(x, y);
  }
  for (int k = K - 1; k >= 0; k--) {
    if (dep[x] - (1 << k) >= dep[y]) {
      x = tab[k][x];
    }
  }
  if (x == y) {
    return x;
  }
  for (int k = K - 1; k >= 0; k--) {
    if (tab[k][x] != tab[k][y]) {
      x = tab[k][x];
      y = tab[k][y];
    }
  }
  return tab[0][x];
}

bool is_ancestor(int x, int y) { /// x ancestor of y
  return l[x] <= l[y] && r[y] <= r[x];
}

struct PST{
  struct Node {
    int result;
    Node* left;
    Node* right;

    Node (int val = 0) {
      result = val;
      left = right = nullptr;
    }

    void refresh() {
      this->result = this->left->result + this->right->result;
    }
  };

  Node* build(int from, int to) {
    Node *node = new Node(0);
    if (from < to) {
      int mid = (from + to) / 2;
      node->left = build(from, mid);
      node->right = build(mid + 1, to);
    }
    return node;
  }

  Node* initialize(int n) {
    return build(1, n);
  }


  Node* update(Node* base, int from, int to, int x, int val) {
    if (from < to) {
      int mid = (from + to) / 2;
      Node* newNode = new Node();
      (*newNode) = (*base);
      if (x <= mid) {
        newNode->left = update(base->left, from, mid, x, val);
      } else {
        newNode->right = update(base->right, mid + 1, to, x, val);
      }
      newNode->refresh();
      return newNode;
    } else {
      return (new Node(base->result + val));
    }
  }

  int query(Node* node, int from, int to, int x, int y) {
    assert(from <= x && x <= y && y <= to);
    if (from == x && to == y) {
      return node->result;
    } else {
      int mid = (from + to) / 2;
      if (x <= mid && y <= mid) {
        return query(node->left, from, mid, x, y);
      } else if (mid + 1 <= x && mid + 1 <= y) {
        return query(node->right, mid + 1, to, x, y);
      } else {
        return query(node->left, from, mid,x , mid) + query(node->right, mid + 1, to, mid + 1, y);
      }
    }
  }

  Node* version[N];
  int n;

  void buildVersions(int n_) {
    n = n_;
    Node* part = initialize(n);
    version[0] = part;
    for(int i = 1; i <= m; i++) {
      int val = updates[i].first;
      int x = updates[i].second;
      part = update(part, 1, n, l[x], +val);
      if (r[x] + 1 <= n) {
        part = update(part, 1, n, r[x] + 1, -val);
      }
      version[i] = part;
    }
  }

  int sum(int when, int x) {
    return query(version[when], 1, n, 1, l[x]);
  }

  int get_sum(int when, int x, int y) {
    int z = lca(x, y);
    return sum(when, x) + sum(when, y) - 2 * sum(when, z);
  }
};

PST tree1, tree2;

signed main() {
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

/// freopen("input.txt", "r", stdin);

  cin >> n >> m >> q;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
    edges[i] = {x, y};
  }
  build(1);
  for (int i = 1; i <= m; i++) {
    ll road, val;
    cin >> road >> val;
    int x = edges[road].first;
    int y = edges[road].second;
    if (dep[x] < dep[y]) {
      swap(x, y);
    }
    updates[i] = {val, x};
  }
  sort(updates + 1, updates + m + 1);

  tree1.buildVersions(n);
  for (int i = 1; i <= m; i++) {
    updates[i].first = 1;
  }
  tree2.buildVersions(n);


  while (q--) {
    ll s, t, x, y;
    cin >> s >> t >> x >> y;
    int low = 1, high = m, sol = 0;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (tree1.get_sum(mid, s, t) <= y) {
        sol = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    int z = lca(s, t);
    int payed = tree2.get_sum(sol, s, t);
    int total = tree2.get_sum(m, s, t);
    int keep = x - (total - payed);
    if (keep < 0) {
      keep = -1;
    }
    cout << keep << "\n";
  }
  return 0;
}

Compilation message

currencies.cpp: In function 'int main()':
currencies.cpp:206:9: warning: unused variable 'z' [-Wunused-variable]
  206 |     int z = lca(s, t);
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24668 KB Output is correct
2 Correct 4 ms 24868 KB Output is correct
3 Correct 3 ms 24668 KB Output is correct
4 Correct 4 ms 24872 KB Output is correct
5 Correct 14 ms 27992 KB Output is correct
6 Correct 13 ms 27740 KB Output is correct
7 Correct 10 ms 26968 KB Output is correct
8 Correct 13 ms 27996 KB Output is correct
9 Correct 13 ms 27996 KB Output is correct
10 Correct 13 ms 28240 KB Output is correct
11 Correct 13 ms 28068 KB Output is correct
12 Correct 13 ms 27996 KB Output is correct
13 Correct 11 ms 26972 KB Output is correct
14 Correct 11 ms 27228 KB Output is correct
15 Correct 12 ms 27740 KB Output is correct
16 Correct 13 ms 27996 KB Output is correct
17 Correct 13 ms 28160 KB Output is correct
18 Correct 13 ms 28248 KB Output is correct
19 Correct 13 ms 28244 KB Output is correct
20 Correct 12 ms 28252 KB Output is correct
21 Correct 17 ms 28248 KB Output is correct
22 Correct 13 ms 28252 KB Output is correct
23 Correct 14 ms 28252 KB Output is correct
24 Correct 12 ms 28252 KB Output is correct
25 Correct 12 ms 28084 KB Output is correct
26 Correct 11 ms 28240 KB Output is correct
27 Correct 11 ms 27996 KB Output is correct
28 Correct 11 ms 28248 KB Output is correct
29 Correct 13 ms 28240 KB Output is correct
30 Correct 13 ms 28048 KB Output is correct
31 Correct 13 ms 27996 KB Output is correct
32 Correct 13 ms 27996 KB Output is correct
33 Correct 13 ms 26856 KB Output is correct
34 Correct 11 ms 26716 KB Output is correct
35 Correct 11 ms 26620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24668 KB Output is correct
2 Correct 13 ms 27996 KB Output is correct
3 Correct 13 ms 28240 KB Output is correct
4 Correct 12 ms 28252 KB Output is correct
5 Correct 1379 ms 244412 KB Output is correct
6 Correct 1700 ms 215760 KB Output is correct
7 Correct 1586 ms 188020 KB Output is correct
8 Correct 1226 ms 181608 KB Output is correct
9 Correct 1196 ms 193108 KB Output is correct
10 Correct 2204 ms 270484 KB Output is correct
11 Correct 2208 ms 270416 KB Output is correct
12 Correct 2156 ms 270276 KB Output is correct
13 Correct 2295 ms 270520 KB Output is correct
14 Correct 2240 ms 270368 KB Output is correct
15 Correct 2056 ms 262900 KB Output is correct
16 Correct 2029 ms 275948 KB Output is correct
17 Correct 1882 ms 179624 KB Output is correct
18 Correct 2476 ms 270564 KB Output is correct
19 Correct 2476 ms 270224 KB Output is correct
20 Correct 2367 ms 270028 KB Output is correct
21 Correct 2025 ms 269984 KB Output is correct
22 Correct 2051 ms 270056 KB Output is correct
23 Correct 2080 ms 269980 KB Output is correct
24 Correct 2066 ms 270048 KB Output is correct
25 Correct 1453 ms 266252 KB Output is correct
26 Correct 1406 ms 269544 KB Output is correct
27 Correct 1348 ms 272512 KB Output is correct
28 Correct 746 ms 270344 KB Output is correct
29 Correct 738 ms 270416 KB Output is correct
30 Correct 942 ms 270440 KB Output is correct
31 Correct 956 ms 270344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Correct 11 ms 26716 KB Output is correct
3 Correct 11 ms 26788 KB Output is correct
4 Correct 11 ms 26712 KB Output is correct
5 Correct 941 ms 122488 KB Output is correct
6 Correct 923 ms 107588 KB Output is correct
7 Correct 1398 ms 141584 KB Output is correct
8 Correct 1904 ms 166076 KB Output is correct
9 Correct 1911 ms 166092 KB Output is correct
10 Correct 1939 ms 166032 KB Output is correct
11 Correct 1397 ms 167096 KB Output is correct
12 Correct 1382 ms 163920 KB Output is correct
13 Correct 1499 ms 166948 KB Output is correct
14 Correct 777 ms 165820 KB Output is correct
15 Correct 756 ms 165620 KB Output is correct
16 Correct 1059 ms 166120 KB Output is correct
17 Correct 1043 ms 165988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 24668 KB Output is correct
2 Correct 4 ms 24868 KB Output is correct
3 Correct 3 ms 24668 KB Output is correct
4 Correct 4 ms 24872 KB Output is correct
5 Correct 14 ms 27992 KB Output is correct
6 Correct 13 ms 27740 KB Output is correct
7 Correct 10 ms 26968 KB Output is correct
8 Correct 13 ms 27996 KB Output is correct
9 Correct 13 ms 27996 KB Output is correct
10 Correct 13 ms 28240 KB Output is correct
11 Correct 13 ms 28068 KB Output is correct
12 Correct 13 ms 27996 KB Output is correct
13 Correct 11 ms 26972 KB Output is correct
14 Correct 11 ms 27228 KB Output is correct
15 Correct 12 ms 27740 KB Output is correct
16 Correct 13 ms 27996 KB Output is correct
17 Correct 13 ms 28160 KB Output is correct
18 Correct 13 ms 28248 KB Output is correct
19 Correct 13 ms 28244 KB Output is correct
20 Correct 12 ms 28252 KB Output is correct
21 Correct 17 ms 28248 KB Output is correct
22 Correct 13 ms 28252 KB Output is correct
23 Correct 14 ms 28252 KB Output is correct
24 Correct 12 ms 28252 KB Output is correct
25 Correct 12 ms 28084 KB Output is correct
26 Correct 11 ms 28240 KB Output is correct
27 Correct 11 ms 27996 KB Output is correct
28 Correct 11 ms 28248 KB Output is correct
29 Correct 13 ms 28240 KB Output is correct
30 Correct 13 ms 28048 KB Output is correct
31 Correct 13 ms 27996 KB Output is correct
32 Correct 13 ms 27996 KB Output is correct
33 Correct 13 ms 26856 KB Output is correct
34 Correct 11 ms 26716 KB Output is correct
35 Correct 11 ms 26620 KB Output is correct
36 Correct 3 ms 24668 KB Output is correct
37 Correct 13 ms 27996 KB Output is correct
38 Correct 13 ms 28240 KB Output is correct
39 Correct 12 ms 28252 KB Output is correct
40 Correct 1379 ms 244412 KB Output is correct
41 Correct 1700 ms 215760 KB Output is correct
42 Correct 1586 ms 188020 KB Output is correct
43 Correct 1226 ms 181608 KB Output is correct
44 Correct 1196 ms 193108 KB Output is correct
45 Correct 2204 ms 270484 KB Output is correct
46 Correct 2208 ms 270416 KB Output is correct
47 Correct 2156 ms 270276 KB Output is correct
48 Correct 2295 ms 270520 KB Output is correct
49 Correct 2240 ms 270368 KB Output is correct
50 Correct 2056 ms 262900 KB Output is correct
51 Correct 2029 ms 275948 KB Output is correct
52 Correct 1882 ms 179624 KB Output is correct
53 Correct 2476 ms 270564 KB Output is correct
54 Correct 2476 ms 270224 KB Output is correct
55 Correct 2367 ms 270028 KB Output is correct
56 Correct 2025 ms 269984 KB Output is correct
57 Correct 2051 ms 270056 KB Output is correct
58 Correct 2080 ms 269980 KB Output is correct
59 Correct 2066 ms 270048 KB Output is correct
60 Correct 1453 ms 266252 KB Output is correct
61 Correct 1406 ms 269544 KB Output is correct
62 Correct 1348 ms 272512 KB Output is correct
63 Correct 746 ms 270344 KB Output is correct
64 Correct 738 ms 270416 KB Output is correct
65 Correct 942 ms 270440 KB Output is correct
66 Correct 956 ms 270344 KB Output is correct
67 Correct 4 ms 24664 KB Output is correct
68 Correct 11 ms 26716 KB Output is correct
69 Correct 11 ms 26788 KB Output is correct
70 Correct 11 ms 26712 KB Output is correct
71 Correct 941 ms 122488 KB Output is correct
72 Correct 923 ms 107588 KB Output is correct
73 Correct 1398 ms 141584 KB Output is correct
74 Correct 1904 ms 166076 KB Output is correct
75 Correct 1911 ms 166092 KB Output is correct
76 Correct 1939 ms 166032 KB Output is correct
77 Correct 1397 ms 167096 KB Output is correct
78 Correct 1382 ms 163920 KB Output is correct
79 Correct 1499 ms 166948 KB Output is correct
80 Correct 777 ms 165820 KB Output is correct
81 Correct 756 ms 165620 KB Output is correct
82 Correct 1059 ms 166120 KB Output is correct
83 Correct 1043 ms 165988 KB Output is correct
84 Correct 1460 ms 248344 KB Output is correct
85 Correct 1652 ms 217756 KB Output is correct
86 Correct 1309 ms 161632 KB Output is correct
87 Correct 2507 ms 270044 KB Output is correct
88 Correct 2507 ms 270488 KB Output is correct
89 Correct 2404 ms 270308 KB Output is correct
90 Correct 2614 ms 270372 KB Output is correct
91 Correct 2398 ms 270196 KB Output is correct
92 Correct 2133 ms 206864 KB Output is correct
93 Correct 2055 ms 183296 KB Output is correct
94 Correct 2704 ms 270192 KB Output is correct
95 Correct 2617 ms 270220 KB Output is correct
96 Correct 2688 ms 269596 KB Output is correct
97 Correct 2657 ms 270008 KB Output is correct
98 Correct 2161 ms 270364 KB Output is correct
99 Correct 2168 ms 270032 KB Output is correct
100 Correct 2240 ms 269772 KB Output is correct
101 Correct 2192 ms 270204 KB Output is correct
102 Correct 1768 ms 272908 KB Output is correct
103 Correct 1819 ms 272752 KB Output is correct
104 Correct 1841 ms 272924 KB Output is correct
105 Correct 921 ms 270456 KB Output is correct
106 Correct 794 ms 270408 KB Output is correct
107 Correct 1030 ms 270124 KB Output is correct
108 Correct 1033 ms 270164 KB Output is correct