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