题意: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;
}