Submission #729384

# Submission time Handle Problem Language Result Execution time Memory
729384 2023-04-24T02:12:13 Z jampm Election Campaign (JOI15_election_campaign) C++17
30 / 100
1000 ms 30888 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int Mxn = 1e5 + 1;
const int mxn = 2e5 + 1;


//hld
//nomás porq soy un mamador y es menos código hacer lca así que con binary lifting
int tin[Mxn], tout[Mxn], Sz[Mxn], head[Mxn], P[Mxn], Depth[Mxn], cnt = 1; 
vector<int> g[Mxn];
void get_sz(int node = 1, int parent = 1) {
  Sz[node] = 0; Depth[node] = Depth[parent] + 1; P[node] = parent;
  if (g[node][0] == parent) swap(g[node][0], g[node].back());
  for (auto & child : g[node]) {
    if (child == parent) continue;
    get_sz(child, node); Sz[node] += Sz[child];
    if (Sz[child] > Sz[g[node][0]]) swap(child, g[node][0]);
  }
}
void dfs_hld(int node = 1, int parent = 1) {
  tin[node] = cnt++;
  for (auto child : g[node]) {
    if (child == parent) continue;
    head[child] = (child == g[node][0]) ? head[node] : child;
    dfs_hld(child, node);
  }
  tout[node] = cnt++;
}
void init_hld() {head[1] = 1; Depth[1] = 0; get_sz(), dfs_hld();}
int LCA(int A, int B) {
  for (; head[A] != head[B]; A = P[head[A]]) {
    if (Depth[head[A]] < Depth[head[B]]) swap(A, B);
  }
  return (Depth[A] < Depth[B]) ? A : B;
}

//fenwi
struct fenwi {
  ll BIT[mxn];
  void upd(int i, ll val = 0) {
    for (; i < mxn; i += i&-i) BIT[i] += val;
  }
  ll sum(int i, ll val = 0) {
    for (; i > 0; i -= i&-i) val += BIT[i];
    return val;
  }
  ll query(int a, int b) {
    return sum(tin[a]) + sum(tin[b]) - 2*sum(tin[LCA(a, b)]);
  }

} bit[2];

//solve
struct query {int a, b, c;};
ll dp[Mxn], children_sum[Mxn];
vector<query> Q[Mxn];

void dfs(int node = 1, int parent = 1) {
  children_sum[node] = 0;
  for (auto child : g[node]) {
    if (child == parent) continue;
    dfs(child, node);
    children_sum[node] += dp[child];
  }
  dp[node] = children_sum[node];
  bit[0].upd(tin[node], children_sum[node]);
  bit[0].upd(tout[node], -children_sum[node]);
  for (auto [a, b, c] : Q[node]) {
    dp[node] = max(dp[node], bit[0].query(a, b) + children_sum[node] - bit[1].query(a, b) + c);
  }
  bit[1].upd(tin[node], dp[node]);
  bit[1].upd(tout[node],-dp[node]);
}


int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int n, m; cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y; cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  cin >> m; init_hld();
  for (int i = 0; i < m; i++) {
    int x, y, c; cin >> x >> y >> c;
    Q[LCA(x, y)].push_back({x, y, c});
  }
  dfs();
  cout << dp[1] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 5 ms 5204 KB Output is correct
5 Correct 109 ms 16400 KB Output is correct
6 Correct 58 ms 27212 KB Output is correct
7 Correct 78 ms 23420 KB Output is correct
8 Correct 67 ms 16764 KB Output is correct
9 Correct 89 ms 21200 KB Output is correct
10 Correct 72 ms 16848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5052 KB Output is correct
3 Correct 4 ms 5308 KB Output is correct
4 Correct 104 ms 30580 KB Output is correct
5 Correct 122 ms 30540 KB Output is correct
6 Correct 87 ms 30632 KB Output is correct
7 Correct 86 ms 30524 KB Output is correct
8 Correct 88 ms 30572 KB Output is correct
9 Correct 96 ms 30592 KB Output is correct
10 Correct 91 ms 30484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5052 KB Output is correct
3 Correct 4 ms 5308 KB Output is correct
4 Correct 104 ms 30580 KB Output is correct
5 Correct 122 ms 30540 KB Output is correct
6 Correct 87 ms 30632 KB Output is correct
7 Correct 86 ms 30524 KB Output is correct
8 Correct 88 ms 30572 KB Output is correct
9 Correct 96 ms 30592 KB Output is correct
10 Correct 91 ms 30484 KB Output is correct
11 Correct 10 ms 6048 KB Output is correct
12 Correct 116 ms 30816 KB Output is correct
13 Correct 115 ms 30776 KB Output is correct
14 Correct 105 ms 30868 KB Output is correct
15 Correct 91 ms 30800 KB Output is correct
16 Correct 96 ms 30888 KB Output is correct
17 Correct 100 ms 30864 KB Output is correct
18 Correct 92 ms 30784 KB Output is correct
19 Correct 88 ms 30876 KB Output is correct
20 Correct 101 ms 30796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 19284 KB Output is correct
2 Correct 87 ms 30504 KB Output is correct
3 Execution timed out 1071 ms 17332 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 5 ms 5204 KB Output is correct
5 Correct 109 ms 16400 KB Output is correct
6 Correct 58 ms 27212 KB Output is correct
7 Correct 78 ms 23420 KB Output is correct
8 Correct 67 ms 16764 KB Output is correct
9 Correct 89 ms 21200 KB Output is correct
10 Correct 72 ms 16848 KB Output is correct
11 Correct 6 ms 5204 KB Output is correct
12 Correct 4 ms 5300 KB Output is correct
13 Correct 4 ms 5300 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 5 ms 5204 KB Output is correct
17 Correct 5 ms 5204 KB Output is correct
18 Correct 5 ms 5300 KB Output is correct
19 Correct 5 ms 5204 KB Output is correct
20 Correct 4 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 3 ms 5036 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 5 ms 5204 KB Output is correct
5 Correct 109 ms 16400 KB Output is correct
6 Correct 58 ms 27212 KB Output is correct
7 Correct 78 ms 23420 KB Output is correct
8 Correct 67 ms 16764 KB Output is correct
9 Correct 89 ms 21200 KB Output is correct
10 Correct 72 ms 16848 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5052 KB Output is correct
13 Correct 4 ms 5308 KB Output is correct
14 Correct 104 ms 30580 KB Output is correct
15 Correct 122 ms 30540 KB Output is correct
16 Correct 87 ms 30632 KB Output is correct
17 Correct 86 ms 30524 KB Output is correct
18 Correct 88 ms 30572 KB Output is correct
19 Correct 96 ms 30592 KB Output is correct
20 Correct 91 ms 30484 KB Output is correct
21 Correct 10 ms 6048 KB Output is correct
22 Correct 116 ms 30816 KB Output is correct
23 Correct 115 ms 30776 KB Output is correct
24 Correct 105 ms 30868 KB Output is correct
25 Correct 91 ms 30800 KB Output is correct
26 Correct 96 ms 30888 KB Output is correct
27 Correct 100 ms 30864 KB Output is correct
28 Correct 92 ms 30784 KB Output is correct
29 Correct 88 ms 30876 KB Output is correct
30 Correct 101 ms 30796 KB Output is correct
31 Correct 189 ms 19284 KB Output is correct
32 Correct 87 ms 30504 KB Output is correct
33 Execution timed out 1071 ms 17332 KB Time limit exceeded
34 Halted 0 ms 0 KB -