QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#471066#7563. Fun on TreeduckindogWA 1524ms60848kbC++233.5kb2024-07-10 17:36:292024-07-10 17:36:29

Judging History

你现在查看的是最新测评结果

  • [2024-07-10 17:36:29]
  • 评测
  • 测评结果:WA
  • 用时:1524ms
  • 内存:60848kb
  • [2024-07-10 17:36:29]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 200'000 + 10;
int n, q;
int a[N];
vector<pair<int, int>> ad[N];

int par[N], d[N], sz[N];
void dfs0(int u) { 
  for (const auto& [v, w] : ad[u]) { 
    par[v] = u;
    d[v] = d[u] + w;
    dfs0(v);
    sz[u] += sz[v] + 1;
  }
}

int head[N], st[N], ed[N], rvs[N], it;
void dfs1(int u) { 
  if (!head[u]) head[u] = u;
  st[u] = ++it; rvs[it] = u;

  sort(ad[u].begin(), ad[u].end(), [&](const auto& a, const auto& b) { 
    return sz[a.first] > sz[b.first];
  });

  for (const auto& [v, w] : ad[u]) {
    if (make_pair(v, w) == ad[u][0]) head[v] = head[u];
    dfs1(v);
  }

  ed[u] = it;
}

struct IT { 
  pair<int, int> f[N << 2];
  int lazy[N << 2];
  void push(int s) { 
    f[s << 1].first += lazy[s];   f[s << 1 | 1].first += lazy[s];
    lazy[s << 1] += lazy[s];      lazy[s << 1 | 1] += lazy[s];
    lazy[s] = 0;
  }
  void update(int s, int l, int r, int u, int v, int x) { 
    if (v < l || u > r) return;
    if (u <= l && r <= v) { 
      f[s].first += x;
      lazy[s] += x;
      return;
    }
    push(s);
    int mid = l + r >> 1;
    update(s << 1, l, mid, u, v, x); update(s << 1 | 1, mid + 1, r, u, v, x);
    f[s] = max(f[s << 1], f[s << 1 | 1]);
  }
  void assign(int s, int l, int r, int i, pair<int, int> x) {
    if (i < l || i > r) return;
    if (l == r) {
      f[s] = x;
      return;
    }
    push(s);
    int mid = l + r >> 1;
    assign(s << 1, l, mid, i, x); assign(s << 1 | 1, mid + 1, r, i, x);
    f[s] = max(f[s << 1], f[s << 1 | 1]);
  }
  pair<int, int> query(int s, int l, int r, int u, int v) { 
    if (v < l || u > r) return {-1'000'000'000'000'000, 0};
    if (u <= l && r <= v) return f[s];
    push(s);
    int mid = l + r >> 1;
    return max(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
  }
} T, D;

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int v = 2; v <= n; ++v) { 
    int u, w; cin >> u >> w;
    ad[u].push_back({v, w});
  }

  dfs0(1);
  dfs1(1);
  
  for (int i = 1; i <= n; ++i) T.assign(1, 1, n, st[i], {d[i] - a[i], -i});
  
  for (int u = 1; u <= n; ++u) { 
    pair<int, int> ma = {-a[u], -u};
    for (const auto& [v, w] : ad[u]) { 
      if (v == ad[u][0].first) continue;
      ma = max(ma, T.query(1, 1, n, st[v], ed[v]));
    }
    ma.first -= 2 * d[u];
    D.assign(1, 1, n, st[u], ma);
  }

  for (int i = 1; i <= q; ++i) { 
    int x, y, w; cin >> x >> y >> w;
    int start = x;

    T.update(1, 1, n, st[y], ed[y], -w);
    D.update(1, 1, n, st[y], ed[y], -w);

    while (par[head[y]]) { 
      int a = par[head[y]], b = ad[a][0].first;
      auto ret = max(T.query(1, 1, n, st[a], st[b] - 1), T.query(1, 1, n, ed[b] + 1, ed[a]));
      ret.first -= 2 * d[a];
      D.assign(1, 1, n, st[a], ret);
      y = par[head[y]];
    }
    
    auto answer = T.query(1, 1, n, st[x], ed[x]);
    answer.first -= d[x];
    while (x) { 
      {
        int l = st[head[x]], r = st[x] - 1;
        auto ret = D.query(1, 1, n, l, r);
        ret.first += d[start];
        answer = max(answer, ret);
      }

      if (par[head[x]]) {
        int a = par[head[x]], b = ad[a][0].first;
        auto ret = max(T.query(1, 1, n, st[a], st[b] - 1), T.query(1, 1, n, ed[b] + 1, ed[a]));
        ret.first += d[start];
        answer = max(answer, ret);
      }

      x = par[head[x]];
    }

    cout << -answer.second << " " << answer.first << "\n";
  }
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 39624kb

input:

6 6
1 1 4 5 1 4
1 5
2 0
3 2
4 1
5 6
3 2 -100000
1 2 100000
1 1 0
2 2 66
3 1 5
4 4 -3

output:

6 100005
6 10
6 10
1 4
1 -1
1 1

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 38516kb

input:

5 6
-10 0 2 -4 8
1 7
1 1
2 2
2 -2
1 1 100
2 1 -100
1 1 0
4 3 10
2 5 3
5 2 2

output:

4 -87
1 17
4 13
1 19
1 17
1 15

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 3ms
memory: 37736kb

input:

6 3
0 0 0 0 0 0
1 10
1 10
1 -100
4 10
4 11
1 1 0
4 1 0
1 4 1000

output:

2 10
6 11
2 10

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 3ms
memory: 39580kb

input:

2 0
1 1
1 3

output:


result:

ok 0 lines

Test #5:

score: -100
Wrong Answer
time: 1524ms
memory: 60848kb

input:

200000 200000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...

output:

65204 15000000000
119017 16000000000
65204 15000000000
65204 15000000000
119017 16000000000
119017 14000000000
119017 15000000000
65204 17000000000
65204 16000000000
65204 12000000000
65204 17000000000
119017 15000000000
119017 13000000000
119017 16000000000
119017 17000000000
119017 15000000000
119...

result:

wrong answer 1st lines differ - expected: '119017 15000000000', found: '65204 15000000000'