codeforces 292E. Copying Data

6547 단어 세그먼트 트리
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.
More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types:
Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 ≤ q < k). The given operation is correct — both subsegments do not touch unexistent elements.
Determine the value in position x of array b, that is, find value bx.
For each query of the second type print the result — the value of the corresponding element of array b.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| ≤ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| ≤ 109).
Next m lines contain the descriptions of the queries. The i-th line first contains integer ti — the type of the i-th query (1 ≤ ti ≤ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 ≤ xi, yi, ki ≤ n) — the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 ≤ xi ≤ n) — the position in array b.
All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
Output
For each second type query print the result on a single line.
Sample test(s)
input
5 10
1 2 0 -1 3
3 1 5 -2 0
2 5
1 3 3 3
2 5
2 4
2 1
1 2 1 4
2 1
2 4
1 4 2 1
2 2

output
0
3
-1
3
2
3
-1
  :        a[] b[],     ,       xi,   k a[]       yi   ,   k b[],           b[x]      。
              ,           ,     ,       ,       a[]        ,      ,         a[]      stra  b[]      strb,     ,        a[x1+strb-stra]。stra         a[]    ,      ,  stra=0,    ,   a[]        a[stra]~a[stra+k-1],strb  ,       b[]     。
    1,  x1,y1,k,      [y1,y1+k-1] stra  x1,strb  y1.
    2,  x1,       ,     ( [x1,x1]) stra 0,   c[x1],    a[x1+strb-stra].
 
    
#include<stdio.h>
#include<string.h>
#define maxn 100006
int a[maxn],c[maxn];
int st1,st2,x1,y1,k;
struct node
{
	int l,r,stra,strb;
}b[4*maxn];


void build(int l,int r,int i)
{
	int mid;
	b[i].l=l;b[i].r=r;b[i].stra=b[i].strb=0;
	if(l==r)return;
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
}


void update(int l,int r,int i)  //stra=x1,strb=y1;
{
	int mid;
	if(b[i].stra==x1 && b[i].strb==y1)return;
	if(b[i].l==l && b[i].r==r){
		b[i].stra=x1;b[i].strb=y1;return;
	}
	if(b[i].stra!=-1){
		b[i*2].stra=b[i*2+1].stra=b[i].stra;
		b[i*2].strb=b[i*2+1].strb=b[i].strb;
		b[i].stra=b[i].strb=-1;
	}
	mid=(b[i].l+b[i].r)/2;
	if(r<=mid)update(l,r,i*2);
	else if(l>mid) update(l,r,i*2+1);
	else {
		update(l,mid,i*2);update(mid+1,r,i*2+1);
	}
}


void question(int id,int i)
{
	int mid;
	if(b[i].stra!=-1){
		st1=b[i].stra;st2=b[i].strb;return;
	}
	mid=(b[i].l+b[i].r)/2;
	if(id<=mid) question(id,i*2);
	else question(id,i*2+1);
}


int main()
{
	int n,m,i,j,h,d,e,f,x;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=1;i<=n;i++){
			scanf("%d",&a[i]);
		}
		for(i=1;i<=n;i++){
			scanf("%d",&c[i]);
		}
		build(1,n,1);
		while(m--){
			scanf("%d",&h);
			if(h==1){
				scanf("%d%d%d",&x1,&y1,&k);
				update(y1,y1+k-1,1);
			}
			else if(h==2){
				scanf("%d",&x);
				st1=st2=0;
				question(x,1);
				if(st1==0){
					printf("%d
",c[x]);continue; } else { printf("%d
",a[x-st2+st1]);continue; } } } } return 0; }

좋은 웹페이지 즐겨찾기