您的位置:首页 > 其它

Croc Champ 2013 - Round 1——E

2013-04-16 22:33 411 查看
E. Copying Data

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

线段树裸题。可惜没想到用区间更新的方法将a映照到b上。参照ZEROm牛的代码。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid (l+r)>>1
using namespace std;
const int maxn=100000+5;
int a[maxn],b[maxn];
bool hash[maxn<<2];//判断该点是否参与更新
int col[maxn<<2];
int n;
void PushDown(int rt)
{
if(col[rt]!=n+1)
{
col[rt<<1]=col[rt<<1|1]=col[rt];
hash[rt<<1]=hash[rt<<1|1]=1;
col[rt]=n+1;
}
}
void build(int l,int r,int rt)
{
col[rt]=n+1;
hash[rt]=0;
if(r==l) return ;
int m=mid;
build(lson);
build(rson);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]=c;
hash[rt]=1;
return ;
}
PushDown(rt);
int m=mid;
if(m>=L) update(L,R,c,lson);
if(m<R) update(L,R,c,rson);
}
int query(int p,int l,int r,int rt)
{
if(l==r)
{
if(hash[rt]) return a[l+col[rt]];
else return b[l];
}
PushDown(rt);
int m=mid;
if(p<=m) return query(p,lson);
else query(p,rson);
}
int main()
{
int m;
scanf("%d%d",&n,&m);
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 t,x,y,k;
scanf("%d",&t);
if(t==1)
{
scanf("%d%d%d",&x,&y,&k);
update(y,min(y+k-1,n),x-y,1,n,1);
}
else
{
scanf("%d",&x);
printf("%d\n",query(x,1,n,1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: