CF292E Copying Data【线段树】

题意:a[]、b[]两数组,有两个操作:1.用a数组的一段去替换b数组的 2.问b数组中的某个位置


思路:线段树,每个结构体记录它被覆盖的a的位置(z1),和从哪个位置开始被覆盖(z2)。查询的时候查到底层,没被覆盖,直接b[x];被覆盖,a[x-z2+z1]


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2e5+5;
#define lson(x) 2*x
#define rson(x) 2*x+1
struct data
{
	int l,r,z1,z2;
}node[maxn*4];
int a[maxn],b[maxn];

void build(int x,int y, int cnt)
{
	node[cnt].l = x;
	node[cnt].r = y;
	node[cnt].z1 = node[cnt].z2 = 0;
	if(x == y)
		return;
	int mid = (x+y) / 2;
	build(x,mid,lson(cnt));
	build(mid+1,y,rson(cnt));
}

void down(int cnt)
{
	if(node[cnt].z1 == 0)
		return;
	node[lson(cnt)].z1 = node[rson(cnt)].z1 = node[cnt].z1;
	node[lson(cnt)].z2 = node[rson(cnt)].z2 = node[cnt].z2;
	node[cnt].z1 = node[cnt].z2 = 0; 
}

void up(int x,int y,int cnt,int f1,int f2)
{
	if(x == node[cnt].l && y == node[cnt].r)
	{
		node[cnt].z1 = f1;
		node[cnt].z2 = f2;
		return;
	}
	down(cnt);
	int mid = (node[cnt].l + node[cnt].r) / 2;
	if(y <= mid)
		up(x,y,lson(cnt),f1,f2);
	else if(x >= mid+1)
		up(x,y,rson(cnt),f1,f2);
	else
	{
		up(x,mid,lson(cnt),f1,f2);
		up(mid+1,y,rson(cnt),f1,f2);
	}
}

void fid(int x,int y,int cnt)
{
	
	if(x == node[cnt].l && y == node[cnt].r)
	{
		if(node[cnt].z1 != 0)
			printf("%d\n",a[x-node[cnt].z2+node[cnt].z1]);
		else
			printf("%d\n",b[x]);	
		return;
	}
	down(cnt);
	int mid = (node[cnt].l + node[cnt].r) / 2;
	if(y <= mid)
		fid(x,y,lson(cnt));
	else if(x >= mid+1)
		fid(x,y,rson(cnt));
}

int main(void)
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(int i = 1; i <= n; i++)
			scanf("%d",&a[i]);
		for(int i = 1; i <= n; i++)
			scanf("%d",&b[i]);
		build(1,n,1);
		while(m--)
		{
			int op,x,y,k;
			scanf("%d",&op);
			if(op == 2)
			{
				scanf("%d",&x);
				fid(x,x,1);
			}
			else
			{
				scanf("%d%d%d",&x,&y,&k);
				int tp = min(n,y+k-1);
				up(y,tp,1,x,y);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值