跳到正文

题解

对/article/ojaqc16n的补充

erjn约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟

版本

  1. v1当前

什么?题解区竟然没有分块题解?那让我来发一发。

题目中的 22 操作分出来的第二个连通分量点权和可以转换为点权总和减去第一个连通分量。

问题就转换为了增加点权或查询一个子树的点权和。

考虑使用 DFSDFS 序,一个子树内的 DFSDFS 序是连续的,树上问题进而转换为序列问题。

题解区有线段树实现和树状数组实现,我就来补一个分块实现。

Code:

C++
#include<bits/stdc++.h>
using namespace std;
#define I love sqrt decomposition//I love sqrt decomposition
#define int long long

#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif

#define ds(x) (x=='\r'||x=='\n'||x==' ')
#define MAX 20
namespace fastIO {
	template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; }
	template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); }
	struct Srr {
		inline Srr operator>>(int& a) { r(a); return{}; }
		inline Srr operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; }
		inline Srr operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; }
		template<typename T>inline Srr operator<<(T& a) { r(a); return{}; }
		inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } }
	}in;
	struct Sww {
		inline Sww operator<<(const int a) { w(a); return{}; }
		inline Sww operator<<(const char ch) { pc(ch); return{}; }
		inline Sww operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; }
		template<typename T>inline Sww operator>>(const T a) { w(a); return{}; }
	}out;
}using fastIO::in; using fastIO::out;
#undef ds
#define eout cerr
int dfn[300005], dep[300005], a[300005], siz[300005],Dfn[300005];
int tot;
vector<int>e[300005];
inline void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1;
	siz[x] = 1;
	dfn[x] = ++tot;
	Dfn[tot] = x;
	for (auto i : e[x]) {
		if (i == fa)continue;
		dfs(i, x);
		siz[x] += siz[i];
	}
}
int X[300005], Y[300005];
int id[300005],len,tag[1005];
inline void add(int x,int w){
	a[x]+=w;
	tag[id[x]]+=w;
}
inline int cha(int l,int r){
	int q=id[l],w=id[r];
	if(q+1>=w){
		int ans=0;
		for(int i=l;i<=r;i++)ans+=a[i];
		return ans;
	}
	int ans=0;
	for(int i=l;id[i]==q;i++)ans+=a[i];
	for(int i=r;id[i]==w;i--)ans+=a[i];
	for(int i=q+1;i<w;i++)ans+=tag[i];
	return ans;
}
void Main() {
	int n;
	in >> n;
	len=sqrt(n*2.0/3);
	if(len==0)len=1;
	for (int i = 1; i <= n; i++){
		a[i] = 1;
		id[i]=i/len;
		tag[id[i]]++;
	}
	for (int i = 1; i < n; i++) {
		int x, y;
		in >> x >> y;
		X[i] = x, Y[i] = y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(1, 0);
	int q;
	in >> q;
	int sum = n;
	for (; q--;) {
		int opt;
		in >> opt;
		if (opt == 1) {
			int x, w;
			in >> x >> w;
			sum += w;
			add(dfn[x], w);
		}
		else {
			int x;
			in >> x;
			x = max(dfn[X[x]], dfn[Y[x]]);
			int k = cha(x, x + siz[Dfn[x]] - 1);
			int ans = sum - k - k;
			if (ans < 0)ans *= -1;
			out << ans << '\n';
		}
	}
}

signed main() {
	Main();
	return 0;
}

时间复杂度:明显 O(n1.5)O(n^{1.5})。

部分代码来自题解,已征得作者的同意。

提交记录

原文