QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#89168#5249. BanditswhateverRE 0ms0kbC++143.4kb2023-03-19 10:01:132023-03-19 10:01:17

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-19 10:01:17]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-03-19 10:01:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

bool Mbe;
constexpr int K = 17;
constexpr int N = 1e5 + 5;

ll dep[N];
int u[N], v[N], w[N];
int n, q, dn, lg[N], dfn[N], mi[K][N];
struct edge {
  int id, to, val;
};
vector<edge> e[N];
int get(int x, int y) {
  return dep[x] < dep[y] ? x : y;
}
int lca(int u, int v) {
  if(u == v) return u;
  if((u = dfn[u]) > (v = dfn[v])) swap(u, v);
  int d = lg[v - u++];
  return get(mi[d][u], mi[d][v - (1 << d) + 1]);
}
ll dis(int u, int v) {
  return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
void dfs(int id, int ff) {
  mi[0][dfn[id] = ++dn] = ff;
  for(edge _ : e[id]) {
    int it = _.to;
    if(it != ff) dep[it] = dep[id] + _.val, dfs(it, id);
  }
}

int R, sz[N], mx[N], vis[N];
int fa[N], ddep[N];
void findr(int id, int ff, int tot) {
  sz[id] = 1, mx[id] = 0;
  for(edge _ : e[id]) {
    int it = _.to;
    if(it == ff || vis[it]) continue;
    findr(it, id, tot), sz[id] += sz[it];
    mx[id] = max(mx[id], sz[it]);
  }
  mx[id] = max(mx[id], tot - sz[id]);
  if(mx[id] < mx[R]) R = id;
}

vector<pair<ll, int>> buc[N];
map<int, int> mp[N];
void findv(int id, int ff, int anc, ll ds) {
  for(edge _ : e[id]) {
    int it = _.to;
    if(it == ff) continue;
    buc[anc].push_back({ds + _.val, _.id});
    if(!vis[it]) findv(it, id, anc, ds + _.val);
  }
}
vector<int> c[N], cfa[N];
void add(int id, ll x, vector<int> &c) {
  if(x < 0) return;
  x = upper_bound(buc[id].begin(), buc[id].end(), make_pair(x, N)) - buc[id].begin();
  while(x) c[x]++, x -= x & -x;
}
ll query(int id, int x, vector<int> &c) {
  x = mp[id][x];
  int s = 0;
  while(x < sz[id]) s += c[x], x += x & -x;
  return s;
}
void divide(int id) {
  vis[id] = 1;
  c[id].resize(sz[id]);
  cfa[id].resize(sz[id]);
  findv(id, 0, id, 0);
  sort(buc[id].begin(), buc[id].end());
  for(int i = 0; i < buc[id].size(); i++) mp[id][buc[id][i].second] = i + 1;
  for(edge _ : e[id]) {
    int it = _.to;
    if(vis[it]) continue;
    R = 0, findr(it, id, sz[it]);
    fa[R] = id, ddep[R] = ddep[id] + 1, sz[R] = sz[it];
    divide(R);
  }
}

void add(int x, int R) {
  for(int u = x; u; u = fa[u]) {
    add(u, R - dis(x, u), c[u]);
    if(fa[u]) add(u, R - dis(x, fa[u]), cfa[u]);
  }
}
int query(int id) {
  int x = ddep[u[id]] > ddep[v[id]] ? u[id] : v[id];
  int ans = 0;
  for(int u = x; u; u = fa[u]) {
    ans += query(u, id, c[u]);
    if(fa[u]) ans -= query(u, id, cfa[u]);
  }
  return ans;
}

bool Med;
signed main() {
  fprintf(stderr, "%.3lf MB\n", (&Mbe - &Med) / 1048576.0);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  #ifdef ALEX_WEI
    FILE* IN = freopen("1.in", "r", stdin); 
    FILE* OUT = freopen("1.out", "w", stdout);
  #endif
  cin >> n;
  for(int i = 1; i < n; i++) {
    cin >> u[i] >> v[i] >> w[i];
    e[u[i]].push_back({i, v[i], w[i]});
    e[v[i]].push_back({i, u[i], w[i]});
  }
  dfs(1, 0);
  for(int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
  for(int i = 1; i <= lg[n]; i++)
    for(int j = 1; j + (1 << i) - 1 <= n; j++)
      mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << i - 1)]);
  mx[0] = N, findr(1, 0, n);
  divide(R);
  cin >> q;
  for(int i = 1; i <= q; i++) {
    char op;
    int u, v;
    cin >> op >> u;
    if(op == '+') cin >> v, add(u, v);
    else cout << query(u) << "\n";
  }
  cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

100000
2670 75097 4080
87477 75802 1712
51835 36626 2883
19412 25923 5852
23976 19312 2520
82536 19514 2492
27160 66601 4483
99087 15088 3504
47050 58820 2964
37063 5696 9901
7717 1496 4891
79136 5448 4340
22575 81285 9289
96280 3803 9877
41980 32139 2855
44236 64938 3298
5983 99947 9666
95856 62545...

output:


result: