QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471119#7563. Fun on TreeduckindogCompile Error//C++233.7kb2024-07-10 18:15:282024-07-10 18:15:28

Judging History

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

  • [2024-07-10 18:15:28]
  • 评测
  • [2024-07-10 18:15:28]
  • 提交

answer

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")
#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 = {d[u] - 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 = head[x];
        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] - 2 * d[a];
        
        answer = max(answer, ret);
      }

      x = par[head[x]];
    }

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

Details

In file included from /usr/include/x86_64-linux-gnu/c++/13/bits/gthr.h:148,
                 from /usr/include/c++/13/ext/atomicity.h:35,
                 from /usr/include/c++/13/bits/ios_base.h:39,
                 from /usr/include/c++/13/streambuf:43,
                 from /usr/include/c++/13/bits/streambuf_iterator.h:35,
                 from /usr/include/c++/13/iterator:66,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:54,
                 from answer.code:3:
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:102:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  102 | __gthrw(pthread_once)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:102:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:103:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  103 | __gthrw(pthread_getspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:103:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:104:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  104 | __gthrw(pthread_setspecific)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:104:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:106:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  106 | __gthrw(pthread_create)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:106:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:107:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  107 | __gthrw(pthread_join)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:107:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:108:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  108 | __gthrw(pthread_equal)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:108:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:109:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  109 | __gthrw(pthread_self)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:109:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:110:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  110 | __gthrw(pthread_detach)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:110:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:112:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  112 | __gthrw(pthread_cancel)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:112:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:114:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  114 | __gthrw(sched_yield)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:114:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:116:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  116 | __gthrw(pthread_mutex_lock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:116:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:117:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  117 | __gthrw(pthread_mutex_trylock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:117:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:119:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
  119 | __gthrw(pthread_mutex_timedlock)
      | ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:119:1: error: attribute value ‘tune=native’ was already specified in ‘target’ attribute
/usr/include/x86_64-linux-gnu/c++/13/bits/gthr-default.h:121:1: error: attribute value ‘t...