Thursday, 25 February 2016

***E. Copying Data

E. Copying Data
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:
  1. Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, executeby + q = ax + q for all integer q (0 ≤ q < k). The given operation is correct — both subsegments do not touch unexistent elements.
  2. 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). Ifti = 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.
Examples
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

-----------------------------------------------editorial-------------------------------------------------------------
this  problem can be done in o(nlogn) complexity if we use seg tree, we can see  the constrains , we cant copy actually in the array so we some how need to contain details about    for any index i ans is from first array or from second array and if from the first array than at what index from  the first array ,
struct st
{
int state ;
 
int start;
 
}  seg[1000000],lzy[1000000];

in this structure state is whether for any index ,  answer belong from the 2nd array or from the first array , state=0 means from the second array , and state =1 means from the first array , 
also start will contain the  if ans is from 1st array that at what index from the first array ....

as we can see that there can be multiple times update in the same range we need to keep lzy[]so that need not to apply unnecessary updates .......


--------------------------------------------CODE----------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#include<iostream>
lli ans=0;
#include<string.h>
struct st
{
int state ;
 
int start;
 
}  seg[1000000],lzy[1000000];

long long int arr[1000005],brr[1000000];

using namespace std;
#define inf 100000001
 int x,y,len;
 int diff;
 
void  qry(int index,int start,int end,int qs,int qe)
  {
      if(lzy[index].state!=0)
 {
           seg[index].start=lzy[index].start;
           seg[index].state=1;
          if(start!=end)
            {
           int mid=(start+end)/2;
           int lf=mid-start+1;
           int rt=end-(mid);
           lzy[2*index].state=1;
           lzy[2*index].start=lzy[index].start;
           lzy[2*index+1].state=1;
           lzy[2*index+1].start=lzy[index].start+lf;
           
           }
          lzy[index].state=0;
         }
         
         if(start>end || end<qs || start>qe)
         {
           return ;
          }
        if(start>=qs && end<=qe)
         {
           if(seg[index].state==1)
            {
            ans=arr[seg[index].start];
           
}
else ans=brr[start];
          return ;
         }
        qry(2*index,start,(start+end)/2,qs,qe);
       qry(2*index+1,((start+end)/2)+1,end,qs,qe);
  }
  
     
  
void update(int index,int start,int end,int ups,int upe)
{
    if(start>end || start>upe || end<ups) 
   {
    return ;
   }
    
     if(lzy[index].state!=0)
 {
           seg[index].start=lzy[index].start;
           seg[index].state=1;
          if(start!=end)
            {
           int mid=(start+end)/2;
           int lf=mid-start+1;
           int rt=end-(mid);
           lzy[2*index].state=1;
           lzy[2*index].start=lzy[index].start;
           lzy[2*index+1].state=1;
           lzy[2*index+1].start=lzy[index].start+lf;
           
           }
          lzy[index].state=0;
         }
   if(start>=ups && end<=upe)
    {
        seg[index].state=1;
    seg[index].start=start+diff;
        if(start!=end)
         {
          int mid=(start+end)/2;
          int lf=mid-start+1;
           lzy[2*index].state=1;
           lzy[2*index].start=start+diff;
           
           lzy[2*index+1].state=1;
           
           lzy[2*index+1].start=start+lf+diff;
         }
         return;
    }
    
   
      int mid=(start+end)/2;
     
     update(2*index,start,mid,ups,upe);
     update(2*index+1,((start+end)/2)+1,end,ups,upe);
 }
 
int main()
 {
    int n,m ;
cin>>n>>m;
for(int i=0;i<n;i++)
 {
  scanf("%lld",&arr[i]);
}
for(int i=0;i<n;i++)
 {
  scanf("%lld",&brr[i]);
}
 
for(int i=0;i<=n+10;i++)
 {
  seg[i].state=0;
  seg[i].start=i;
 }
for(int i=0;i<m;i++)
 {
  int type ;
  scanf("%d",&type);
  if(type==1)
   {
   
    scanf("%d %d %d",&x,&y,&len);
    x--;
    y--;
     diff=x-y;
    update(1,0,n-1,y,y+len-1);
 }
 else
 {
  ans=0;
  int idx;
  scanf("%d",&idx);
  idx--;
  qry(1,0,n-1,idx,idx);
  printf("%lld\n",ans);
 }
 }
 return 0;
 }

No comments:

Post a Comment