跳到正文

Gemma

758658

“只要想念,就会相见” || ISTJ-A

在洛谷打开
资料、奖项与版本

资料

编号
758658
抓取

奖项

2025 CSP-J
二等奖
2025 CSP-S
二等奖

版本

  1. v3当前
  2. v2
  3. v1

739559114

图论编辑器

工具

。。。

画图

几何

几何挑战

rngdle

盒子

网址大全

?

这双眼睛看得有多远

?!倍倍?!

o.o https://www.luogu.com.cn/article/5kwxervw


神帖
暂定
猎 奇 文 章

高级账号:

d11005f0-cda5-49b1-b313-5890f02aef1a

image


%板\large \% 板

虚树
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
const int N = 250005;
struct node
{
	int y,w;
};
vector<node> g[N];vector<int> gg[N];
int n,m,k,cnt,id[N],fa[N],ds[N],tp[N],sz[N],son[N];
int a[N],top,st[N],minn[N];
bool kp[N];
void dfs1(int x,int f)
{
	fa[x] = f;ds[x] = ds[f] + 1;sz[x] = 1;
	for(int i = 0;i < g[x].size();i++)
	{
		int y = g[x][i].y,w = g[x][i].w;
		if(y == f) continue;
    //
		dfs1(y,x);
		sz[x] += sz[y];
		if(sz[y] > sz[son[x]]) son[x] = y;
	}
}
void dfs2(int x,int t)
{
	tp[x] = t;id[x] = ++cnt;
	if(son[x]) dfs2(son[x],t);
	for(int i = 0;i < g[x].size();i++)
	{
		int y = g[x][i].y;
		if(y != fa[x] && y != son[x]) dfs2(y,y);
	}
}
int lca(int x,int y)
{
	while(tp[x] != tp[y])
	{
		if(ds[tp[x]] < ds[tp[y]]) swap(x,y);
		x = fa[tp[x]];
	}
	if(ds[x] > ds[y]) swap(x,y);
	return x;
}
bool cmp(int x,int y){return id[x] < id[y];}
void build()
{
	top = 1;st[top] = 1;
	int l = lca(st[top],a[i]);
	if(l == st[top])
	{
		if(a[i] != st[top]) st[++top] = a[i];break;
	}
	if(ds[l] > ds[st[top - 1]]) gg[l].push_back(st[top]),st[top] = l;
	else gg[st[top - 1]].push_back(st[top]),top--;
	for(int i = 1;i < top;i++) gg[st[i]].push_back(st[i + 1]);
}
int dfss(int x)
{
	int ans = 0;
	for(int i = 0;i < gg[x].size();i++)
	{
		int y = gg[x][i];
		ans += dfss(y);
	}
	gg[x].clear();
	if(kp[x])
	{
		kp[x] = 0;return minn[x];
	}
	return min(minn[x],ans);
}
signed main()
{
	cin >> n;
	for(int i = 1;i < n;i++)
	{
		int x,y,w;
		cin >> x >> y >> w;
		g[x].push_back({y,w});
		g[y].push_back({x,w});
	}
	dfs1(1,0);
	dfs2(1,1);
	//
	return 0;
}
线段树
C++
void pushup(int x)
{
	ans[x] = ans[x * 2] + ans[x * 2 + 1];
}
void build(int l,int r,int x)
{
	if(l == r)
	{
		ans[x] = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(l,mid,x * 2);
	build(mid + 1,r,x * 2 + 1);
	pushup(x);
}
void update(int l,int r,int x,int p,int d)
{
	if(l == r)
	{
		ans[x] = d;
		return;
	}
	int mid = (l + r) / 2;
	if(p <= mid) update(l,mid,x * 2,p,d);
	else update(mid + 1,r,x * 2 + 1,p,d);
	pushup(x);
}
long long query(int s,int t,int l,int r,int x)
{
	if(s <= l && t >= r) return ans[x];
	long long mid = (l + r) / 2,ans = 0;
	if(s <= mid) ans += query(s,t,l,mid,x * 2);
	if(t > mid) ans += query(s,t,mid + 1,r,x * 2 + 1);
	return ans;
}
树的重心
C++
void dfs(int x,int f)
{
	s[x] = 1;
	t[x] = 0;
	for(int i = 0;i < g[x].size();i++)
	{
		int y = g[x][i];
		if(y == f) continue;
		dfs(y,x);
		s[x] += s[y];
		t[x] = max(t[x],s[y]);
	}
	if(max(t[x],n - s[x]) <= n / 2) ans.push_back(x);
}
树链剖分(其实上面有)
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[100005];
int fa[100005],d[100005],tp[100005],son[100005],sum[100005];
void dfs1(int x,int f)
{
	fa[x] = f;
	d[x] = d[f] + 1;
	sum[x] = 1;
	for(int i = 0;i < g[x].size();i++)
	{
		int y = g[x][i];
		if(y == f) continue;
		dfs1(y,x);
		sum[x] += sum[y];
		if(sum[son[x]] < sum[y]) son[x] = y;
	}
}
void dfs2(int x,int t)
{
	tp[x] = t;
	if(son[x] == 0) return;
	dfs2(son[x],t);
	for(int i = 0;i < g[x].size();i++)
	{
		int y = g[x][i];
		if(y == fa[x] || y == son[x]) continue;
		dfs2(y,y);
	}
}
int lca(int x,int y)
{
	while(tp[x] != tp[y])
	{
		if(d[tp[x]] < d[tp[y]]) swap(x,y);
		x = fa[tp[x]];
	}
	if(d[x] < d[y]) return x;
	return y;
}
int main()
{
	int n,q;
	cin >> n;
	for(int i = 1;i < n;i++)
	{
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs1(1,0);
	dfs2(1,1);
	cin >> q;
	for(int i = 1;i <= q;i++)
	{
		int x,y;
		cin >> x >> y;
		int l = lca(x,y);
		cout << d[x] + d[y] - 2 * d[l] << endl;
	}
	return 0;
}
扫描线
C++
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
int n,len,p[200005];
struct node 
{
	int x1,x2,y,io;
}line[2000005];
bool cmp(node a,node b)
{
	return a.y < b.y;
}
int ans,t[800005],sum[800005];
void tag(int x,int l,int r)
{
	if(t[x]) sum[x] = p[r + 1] - p[l];
	else sum[x] = sum[x * 2] + sum[x * 2 + 1];
}
void upd(int x,int l,int r,int x1,int x2,int io)
{
	if(x1 >= r + 1 || x2 <= l) return;
	if(x1 <= l && r + 1 <= x2)
	{
		t[x] += io;
		tag(x,l,r);
		return;
	}
	int mid = (l + r) / 2;
	upd(x * 2,l,mid,x1,x2,io);
	upd(x * 2 + 1,mid + 1,r,x1,x2,io);
	tag(x,l,r);
}
signed main()
{
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		int x1,y1,x2,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		line[i] = {x1,x2,y1,1};
		line[n + i] = {x1,x2,y2,-1};
		p[i] = x1;
		p[n + i] = x2;
	}
	n = n * 2;
	sort(line + 1,line + n + 1,cmp);
	sort(p + 1,p + n + 1);
	len = unique(p + 1,p + n + 1) - (p + 1);
	for(int i = 1;i < n;i++)
	{
		int l = lower_bound(p + 1,p + len + 1,line[i].x1) - p;
		int r = lower_bound(p + 1,p + len + 1,line[i].x2) - p;
		upd(1,1,len - 1,l,r,line[i].io);
		ans += sum[1] * (line[i + 1].y - line[i].y);
	}
	cout << ans << endl;
	return 0;
}

还没有收录这位用户的文章。