CF911G Mass Change Queries(线段树+暴力)

cf机子真的快。

其实这个题的维护的信息还是很巧妙的。

首先,观察到题目中涉及到,区间修改这个操作,然后最后只查询一次,我们不妨用线段树来维护这个过程。

但是貌似直接维护每个位置的值可能不太好维护。
这时候我们考虑

每一个节点维护一个 t o to to数组,其中 t o [ i ] to[i] to[i]表示这个节点对应的区间里面, i i i这个值将会变成哪个值。

一开始,每个节点的 t o [ i ] = i to[i]=i to[i]=i

由于最后是单点询问,所以不用 u p up up操作。

现在考虑 p u s h d o w n pushdown pushdown应该怎么写。
对于当前节点的 t o [ i ] to[i] to[i]应该等于他的父亲的 t o [ t o [ i ] ] to[to[i]] to[to[i]],就是他原本的i指向的点,在父亲里会指向哪里。

void pushdown(int root,int l,int r)
{
	for (rint i=1;i<=100;++i) 
	{
		f[root<<1].to[i]=f[root].to[f[root<<1].to[i]];
		f[root<<1|1].to[i]=f[root].to[f[root<<1|1].to[i]];
	}
	for (rint i=1;i<=100;++i)
	  f[root].to[i]=i;
}

然后 u p d a t e update update的时候,直接暴力扫一遍即可。
将所有 t o [ i ] = x to[i]=x to[i]=x的to,都修改成 y y y.

询问的时候,直接询问 t o to to就可以,直接给代码了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define mk make_pair
#define ll long long
#define rint register int

using namespace std;

inline int read()
{
  int x=0,f=1;char ch=getchar();
  while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
  while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
  return x*f;
}

const int maxn = 2e5+1e2;

int n;
int val[maxn];

struct Node{
	int to[102];
};

Node f[4*maxn];

void pushdown(int root,int l,int r)
{
	for (rint i=1;i<=100;++i) 
	{
		f[root<<1].to[i]=f[root].to[f[root<<1].to[i]];
		f[root<<1|1].to[i]=f[root].to[f[root<<1|1].to[i]];
	}
	for (rint i=1;i<=100;++i)
	  f[root].to[i]=i;
}

void build(int root,int l,int r)
{
    for (rint i=1;i<=100;++i) f[root].to[i]=i;
	if (l==r)
	{
		return;
	}
	int mid = l+r >>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
}

void update(int root,int l,int r,int x,int y,int p,int pp)
{
	if (x<=l && r<=y)
	{
		for (int i=1;i<=100;i++)
		{
			if (f[root].to[i]==p) f[root].to[i]=pp;
		}
		return;
	}
	pushdown(root,l,r);
	int mid = l+r >>1;
	if (x<=mid) update(root<<1,l,mid,x,y,p,pp);
	if (y>mid) update(root<<1|1,mid+1,r,x,y,p,pp);
}

int query(int root,int l,int r,int x,int p)
{
	if (l==r)
	{
		return f[root].to[p];
	}
	pushdown(root,l,r);
	int mid = l+r >> 1;
	if (x<=mid) return query(root<<1,l,mid,x,p);
	if (x>mid) return query(root<<1|1,mid+1,r,x,p);
}

int main()
{
  n=read();
  for (rint i=1;i<=n;++i) val[i]=read();
  build(1,1,n);
 // for (rint i=1;i<=n;++i)
  //{
  //	 cout<<query(1,1,n,i,val[i])<<" ";
  //}
//  cout<<endl;
  int q=read();
  for (rint i=1;i<=q;++i)
  {
  	 int l=read(),r=read(),x=read(),y=read();
  	 update(1,1,n,l,r,x,y);
  	// cout<<i<<":"<<endl;
  // for (rint i=1;i<=n;++i)
 // {
  //	 cout<<query(1,1,n,i,val[i])<<" ";
  //}
 // cout<<endl;
  }
  for (rint i=1;i<=n;++i)
  {
  	 cout<<query(1,1,n,i,val[i])<<" ";
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值