性感代码在线求条
查看原帖
性感代码在线求条
373530
Reply_楼主2024/11/22 21:08

样例过了

#include<bits/stdc++.h>
#define ll long long
#define ls(x) x*2
#define rs(x) x*2+1
#define R register
#define F(i,a,b) for(int i = (a);i<=(b);i++)
using namespace std;
inline int read(){R int x=0,t=1;R char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*t;}
const int N=1e5+70;
int n,m,siz[N],fa[N],dep[N],top[N],id[N],tot,c[N],w[N],son[N];
vector<int>g[N];
inline void add(int ui,int vi)
{
	g[ui].push_back(vi);
	return;
}
void dfs1(int u,int Fa)
{
	siz[u]=1;
	fa[u]=Fa;
	dep[u]=dep[Fa]+1;
	for(int i = 0;i<g[u].size();i++){
		int v=g[u][i];
		if(v==Fa) continue;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[son[u]]<siz[v]){
			son[u]=v;
		} 
	}
	return;
}
void dfs2(int u,int topf)
{
	id[u]=++tot;
	c[tot]=w[u];
	top[u]=topf;
	if(son[u]) dfs2(son[u],topf);
	for(int i = 0;i<g[u].size();i++){
		int v=g[u][i];
		if(id[v]) continue;
		dfs2(v,v);
	}
	return;
}
struct Node
{
	int l,r,sum,c,lazy,lc,rc;
};
struct Seg
{
	Node tr[N*4];
	void push_up(int x)
	{
		tr[x].sum=tr[x*2].sum+tr[x*2+1].sum;
		if(tr[ls(x)].rc==tr[rs(x)].lc) tr[x].sum--;
		tr[x].lc=tr[ls(x)].lc;
		tr[x].rc=tr[rs(x)].rc;
		return;
	}
	void build(int x,int l,int r)
	{
		tr[x].l=l;tr[x].r=r;
		if(l==r){
			tr[x].c=c[l];
			tr[x].lc=c[l];
			tr[x].rc=c[l];
			tr[x].sum=1;
			return;
		}
		int mid=l+r>>1;
		build(ls(x),l,mid);
		build(rs(x),mid+1,r);
		push_up(x);
		return;
	}
	void push_down(int x)
	{
		if(tr[x].lazy){
			tr[x*2].c=tr[x].lazy;
			tr[x*2].lc=tr[x].lazy;
			tr[x*2].rc=tr[x].lazy;
			tr[x*2].sum=1;
			tr[x*2].lazy=tr[x].lazy;
			
			tr[x*2+1].c=tr[x].lazy;
			tr[x*2+1].lc=tr[x].lazy;
			tr[x*2+1].rc=tr[x].lazy;
			tr[x*2+1].sum=1;
			tr[x*2+1].lazy=tr[x].lazy;
			
			tr[x].lazy=0;
			return;
		}
	}
	void update(int x,int l,int r,int k)
	{
		if(tr[x].l>=l && tr[x].r<=r){
			tr[x].c=k;
			tr[x].lc=c[x];
			tr[x].rc=c[x];
			tr[x].sum=1;
			tr[x].lazy=k;
			return;
		}
		push_down(x);
		int mid=tr[x].l+tr[x].r>>1;
		if(l<=mid) update(ls(x),l,mid,k);
		if(r>mid) update(rs(x),mid+1,r,k);
		push_up(x);
		return;
	}
	int query1(int x,int pos)
	{
		if(tr[x].l==pos && tr[x].r==pos){
			return tr[x].c;
		}
		push_down(x);
		int mid=tr[x].l+tr[x].r>>1;
		//1--mid mid+1--r
		if(pos<=mid) return query1(ls(x),pos);
		else return query1(rs(x),pos);
		push_up(x);
	}
	int query(int x,int l,int r)
	{
		if(tr[x].l>=l && tr[x].r<=r){
			return tr[x].sum;
		}
		push_down(x);
		int mid=tr[x].l+tr[x].r>>1,ans=0;
		if(l<=mid) ans+=query(ls(x),l,r);
		if(r>mid) ans+=query(rs(x),l,r);
		if(l<=mid && r>mid && tr[ls(x)].rc==tr[rs(x)].lc) ans--;
		push_up(x);
		return ans;
	}
	int qc(int x)
	{
		return query1(1,x);
	}
}tree;
void work(int x,int y,int k)
{
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		tree.update(1,id[top[x]],id[x],k);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	tree.update(1,id[x],id[y],k);
	return;
}
int query_color(int x,int y)
{
	int ans=0;
	int cson,cfa;
	while(top[x]!=top[y]){
//		cout << x << " " << y << "\n";
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		ans+=tree.query(1,id[top[x]],id[x]);
		cson=tree.qc(id[top[x]]);
		//当前链顶
		cfa=tree.qc(id[fa[top[x]]]);
		//链顶的上一个
	//	cout << top[x] << " " << cson << " " << fa[top[x]] << " " << cfa << '\n';
		if(cson==cfa) ans--;
		
	//	cout << x << " " << y << '\n';
	//	cout << ans << '\n';
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	ans+=tree.query(1,id[x],id[y]);
	return ans;
}
signed main()
{
	n=read(),m=read();
	F(i,1,n) w[i]=read();
	F(i,2,n){
		int ui=read(),vi=read();
		add(ui,vi);
		add(vi,ui);
	}
	dfs1(1,0);
	dfs2(1,1);
	tree.build(1,1,n);
//	F(i,1,n) cout << id[i] << ' ';
//	puts("");
//	F(i,1,n) cout << c[i] << ' ';
//	puts("");
	for(int i = 1;i<=m;i++){
		char op;
		cin >> op;
		int ui=read(),vi=read(),ki;
		if(op=='C'){
			ki=read();
			work(ui,vi,ki);
		}
		else{
			cout << query_color(ui,vi) << '\n';
		}
	}
	return 0;
}
2024/11/22 21:08
加载中...