Loading

P10560 [ICPC2024 Xi'an I] The Last Cumulonimbus Cloud 题解

好好玩的题。

思路

对于一个图上邻域问题,我们有一个很经典的做法:根号分治。

考虑根号分治的本质是什么。

我们把点分成两类,平衡每一种点的时间,也就是度数大的与度数小的点。

所以对于这道题,我们有了更加好的做法。

发现题目给的图的性质就是一个天然的划分方案。

我们每次找到图中度数最小的点,它一定对应着小于等于 \(10\) 个点。

然后我们把它删掉,循环往复的做此操作。

那么每个点都会将自己的邻居分为我对应邻居与邻居对应我。

在修改时,我们把所有对应的点的答案直接加上。

在查询时,我们也只要查这个点对应的点的答案在加上自己的答案即可。

也就是设 \(sn_x\)\(x\) 对应的点,\(a_x,b_x\) 分别为权值与累加的答案。

修改 \(x\),则 \(a_x\rightarrow a_x+v,i\in sn_x,b_i\rightarrow b_i+v\)

查询 \(x\),回答 \(b_x+\sum_{i\in sn_x} a_i\)

时间复杂度:\(O(10q+n+m)\)

Code

本题稍微有一点卡常,所以记得做常数优化。

#include <bits/stdc++.h>
using namespace std;

#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)

const int N = 300010;

int n, m, q, a[N], b[N], du[N], vs[N];

vector<int> to[N];
vector<int> sn[N];

inline int read() {
  int asd = 0; char zxc;
  while (!isdigit(zxc = getchar()));
  while (isdigit(zxc)) asd = asd * 10 + zxc - '0', zxc = getchar();
  return asd;
}

signed main() {
  n = read(), m = read(), q = read();
  fro(i, 1, m) {
    int x = read(), y = read();
    to[x].push_back(y);
    to[y].push_back(x);
    du[x]++, du[y]++;
  }
  queue<int> res;
  fro(i, 1, n) if (du[i] <= 10) res.push(i);
  while (res.empty() == 0) {
    int x = res.front(); res.pop(), vs[x] = 1;
    for (auto i : to[x]) {
      if (vs[i]) continue;
      sn[x].push_back(i);
      if (--du[i] == 10) res.push(i);
    }
  }
  fro(i, 1, n) {
    a[i] = read();
    for (auto j : sn[i]) b[j] += a[i];
  }
  fro(i, 1, q) {
    int op = read(), x, v;
    if (op == 1) {
      x = read(), v = read(), a[x] += v;
      for (auto j : sn[x]) b[j] += v;
    } else {
      x = read(); int res = b[x];
      for (auto j : sn[x]) res += a[j];
      printf("%d\n", res);
    }
  }
  return 0;
}
posted @ 2024-06-08 09:38  JiaY19  阅读(10)  评论(0编辑  收藏  举报