跳到正文

个人记录

线段树板子

__Tao__约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年7月6日
收录
2026年4月27日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

真的太浮夸

C++
#include <bits/stdc++.h>
#define se int k,int l,int r
#define lc k*2,l,(l+r)/2
#define rc k*2+1,(l+r)/2+1,r
#define lk k*2
#define rk k*2+1
#define len (r-l+1)
#define bh x<=l&&r<=y
#define fl r<x||y<l
using namespace std;
const int N=3e5+5,p=1e9+9;

namespace SGT{
	struct Node{
		int a,b,sum1,sum2;
	}e[N*8];

	void build(se){
		if(l==r){

			return;
		}
		build(lc);
		build(rc);
	}

	int clac(int a,int b,int len){

	}

	void go(se,a,b){

	}

	void pushdown(se){
		if(a==0&&b==0){
			return;
		}
		go(lc,e[k].a,e[k].b);
		go(lc,e[k].a,e[k].b);
		a=b=0;
	}

	void modi(se){
		if(fl){
			return;
		}
		if(bh){
			go(......)
			return;
		}
		pushdown(k,l,r);
		modi(lc);
		modi(rc);
	}

	int query(se){
		if(fl){
			return 0;
		}
		if(bh){
			return sum1+sum2;
		}
		pushdown(k,l,r);
		return query(lc)+query(rc);
	}
}

int main() {
	return 0;
}

树链剖分

C++
int rdfn[N];
namespace SP {
	int dep[N], wc[N], siz[N], fa[N], dfn[N], top[N], tim;

	void dfs1(int x) {
		siz[x] = 1;//易忘
		g(x) {
			int y = e[i].to;
			if (y != fa[x]) {
				fa[y] = x;
				dep[y] = dep[x] + 1;
				dfs1(y);
				siz[x] += siz[y];
				if (siz[y] > siz[wc[x]]) {
					wc[x] = y;
				}
			}
		}
	}

	void dfs2(int x) {
		if (!top[x]) {
			top[x] = x;
		}
		dfn[x] = ++tim;
		rdfn[tim] = x;
		if (wc[x]) {
			top[wc[x]] = top[x];
			dfs2(wc[x]);
		}
		g(x) {
			int y = e[i].to;
			if (y != fa[x] && y != wc[x]) {
				dfs2(y);
			}
		}
	}

	void upd(int x, int y, ll va) {
//		printf("upd:%d %d %lld\n", x, y, va);
		while (top[x] != top[y]) {
			if (dep[top[x]] < dep[top[y]]) {
				swap(x, y);
			}
			SGT::add(1, 1, n, dfn[top[x]], dfn[x], va);
			x = fa[top[x]];
		}
		if (dep[x] < dep[y]) {
			swap(x, y);
		}
		SGT::add(1, 1, n, dfn[y], dfn[x], va);//dfn!!!
	}

	ll que(int x, int y) {
		ll ans = 0;
		while (top[x] != top[y]) {
			if (dep[top[x]] < dep[top[y]]) {
				swap(x, y);
			}
			ans += SGT::query(1, 1, n, dfn[top[x]], dfn[x]);
			ans %= p;
			x = fa[top[x]];
		}
		if (dep[x] < dep[y]) {
			swap(x, y);
		}
		ans += SGT::query(1, 1, n, dfn[y], dfn[x]);
		return ans % p;
	}
}
原文