QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#787271#6404. Shuttle TourSymbolizeRE 0ms0kbC++204.0kb2024-11-27 10:44:192024-11-27 10:44:21

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 10:44:21]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-27 10:44:19]
  • 提交

answer

/*
	Luogu name: Symbolize
	Luogu uid: 672793
*/
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define rep1(i,l,r) for(register int i=l;i<=r;++i)
#define rep2(i,l,r) for(register int i=l;i>=r;--i)
#define rep3(i,x,y,z) for(register int i=x[y];~i;i=z[i])
#define rep4(i,x) for(auto i:x)
#define debug() puts("----------")
const int N=2e5+10;
const int inf=0x3f3f3f3f3f3f3f3f;
using namespace std;
int n,q,h[N],e[N<<1],ne[N<<1],w[N<<1],idx,fa[N][21],dep[N],dist[N],dfn[N],kase,id[N],top[N],cnt=1;
bool a[N];
struct Segment_Tree
{
	int id;
	struct node
	{
		int l,r;
		int Min,Max;
	}tr[N<<2];
	int ls(int p){return p<<1;}
	int rs(int p){return p<<1|1;}
	void push_up(int p)
	{
		tr[p].Min=min(tr[ls(p)].Min,tr[rs(p)].Min);
		tr[p].Max=max(tr[ls(p)].Max,tr[rs(p)].Max);
		return;
	}
	void build(int p,int l,int r)
	{
		tr[p]={l,r,inf,-inf};
		if(tr[p].l==tr[p].r)
		{
			if(a[l]&&top[l]==id) tr[p].Min=tr[p].Max=dfn[l];
			return;
		}
		int mid=l+r>>1;
		build(ls(p),l,mid);
		build(rs(p),mid+1,r);
		push_up(p);
		return;
	}
	void update(int p,int x)
	{
		if(tr[p].l==tr[p].r)
		{
			if(a[x]&&top[x]==id) tr[p].Min=tr[p].Max=dfn[x];
			else tr[p].Min=inf,tr[p].Max=-inf;
			return;
		}
		int mid=tr[p].l+tr[p].r>>1;
		if(x<=mid) update(ls(p),x);
		else update(rs(p),x);
		push_up(p);
		return;
	}
	pii query(int p,int l,int r)
	{
		pii ans={inf,-inf};
		if(tr[p].l>=l&&tr[p].r<=r) return make_pair(tr[p].Min,tr[p].Max);
		int mid=tr[p].l+tr[p].r>>1;
		if(l<=mid)
		{
			pii now=query(ls(p),l,r);
			ans.x=min(ans.x,now.x);
			ans.y=max(ans.y,now.y);
		}
		if(r>mid)
		{
			pii now=query(rs(p),l,r);
			ans.x=min(ans.x,now.x);
			ans.y=max(ans.y,now.y);
		}
		return ans;
	}
}seg[55];
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return f*x;
}
void add(int x,int y,int z)
{
	e[idx]=y;
	ne[idx]=h[x];
	w[idx]=z;
	h[x]=idx++;
	return;
}
void dfs(int x,int Fa)
{
	fa[x][0]=Fa;
	dep[x]=dep[Fa]+1;
	top[x]=cnt;
	dfn[x]=++kase;
	id[kase]=x;
	bool res=0;
	rep3(i,h,x,ne)
	{
		int to=e[i];
		if(to==Fa) continue;
		dist[to]=dist[x]+w[i];
		if(res) ++cnt;
		res=1;
		dfs(to,x);
	}
	return;
}
void init()
{
	rep1(j,1,20) rep1(i,1,n) fa[i][j]=fa[fa[i][j-1]][j-1];
	return;
}
void plc(int &x,int k)
{
	int kase=0;
	while(k)
	{
		if(k&1) x=fa[x][kase];
		k>>=1;
		++kase;
	}
	return;
}
int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	plc(x,dep[x]-dep[y]);
	if(x==y) return x;
	rep2(i,20,0)
	{
		if(fa[x][i]!=fa[y][i])
		{
			x=fa[x][i];
			y=fa[y][i];
		}
	}
	return fa[x][0];
}
int dis(int x,int y){return dist[x]+dist[y]-2*dist[lca(x,y)];}
bool cmp(int x,int y){return dfn[x]<dfn[y];}
signed main()
{
	freopen("data.in","r",stdin);
	freopen("test.out","w",stdout);
	memset(h,-1,sizeof h);
	n=read();
	q=read();
	rep1(i,1,n)
	{
		char ch;
		cin>>ch;
		a[i]=ch-'0';
	}
	rep1(i,1,n-1)
	{
		int u=read();
		int v=read();
		int w=read();
		add(u,v,w);
		add(v,u,w);
	}
	dfs(1,0);
//	rep1(i,1,n) cout<<top[i]<<" ";
//	putchar('\n');
	rep1(i,1,cnt) seg[i].id=i,seg[i].build(1,1,n);
	init();
	while(q--)
	{
		int opt=read();
		if(opt==1)
		{
			int x=read();
			a[x]^=1;
			seg[top[x]].update(1,x);
		}
		else
		{
			int l=read();
			int r=read();
			vector<int> v;
			rep1(i,1,cnt) 
			{
				pii now=seg[i].query(1,l,r);
				if(now.x>=1&&now.x<=n) v.push_back(id[now.x]);
				if(now.y>=1&&now.y<=n&&now.x!=now.y) v.push_back(id[now.y]);
			}
			if(!v.size())
			{
				puts("-1");
				continue;
			}
			sort(v.begin(),v.end(),cmp);
//			rep4(x,v) cout<<x<<" ";
//			putchar('\n');
			int len=v.size()-1,ans=dis(v[len],v[0]);
			rep1(i,1,len) ans+=dis(v[i-1],v[i]);
			cout<<ans<<"\n";
		}
	}
	return 0;
}
/*
5 1
10010
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 1
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

5 6
10110
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 5
1 3
2 1 5
2 2 4
2 5 5
2 1 1

output:


result: