您的位置:首页 > 其它

CodeForces - 292E 线段树

2013-08-11 18:32 996 查看
题意:给a,b两个数组, 然后有m个操作,t 如果t=2 ,接着输入一个k ,然后输出b【k】;如果t =1,接着输入3个数,x,y,k,表示b【y】到b【y+k-1】等于

a【x】到a【x+k-1】。

线段树设置两个变量,flag1 表示这段区间被x占用,flag2表示这段区间从y位置开始,

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define FF freopen("Input.txt","r",stdin)
const int N=1e5+2;
int a
,b
,id,id1;
struct Node
{
int l,r;
int flag1,flag2;
int mid()
{
return (l+r)>>1;
}
}Tree[N*4];
void Build(int rt,int l,int r)
{
Tree[rt].l=l;
Tree[rt].r=r;
Tree[rt].flag1=0;
if(l==r) return ;
int mid=Tree[rt].mid();
Build(rt<<1,l,mid);
Build(rt<<1|1,mid+1,r);
}
void Pushdown(int rt)
{
Tree[rt<<1].flag1=Tree[rt<<1|1].flag1=Tree[rt].flag1;
Tree[rt<<1].flag2=Tree[rt<<1|1].flag2=Tree[rt].flag2;
Tree[rt].flag1=0;
Tree[rt].flag2=0;
}
void Update(int rt,int l,int r,int val1,int val2)
{
if(Tree[rt].l==l&&Tree[rt].r==r)
{
Tree[rt].flag1=val1;
Tree[rt].flag2=val2;
return ;
}
if(Tree[rt].flag1!=0) Pushdown(rt);
int mid=Tree[rt].mid();
if(l>mid) Update(rt<<1|1,l,r,val1,val2);
else if(r<=mid) Update(rt<<1,l,r,val1,val2);
else
{
Update(rt<<1,l,mid,val1,val2);
Update(rt<<1|1,mid+1,r,val1,val2);
}
}
void Query(int rt,int k)
{
if(Tree[rt].flag1!=0) { id=Tree[rt].flag1;id1=Tree[rt].flag2;return ;}
if(Tree[rt].l==Tree[rt].r&&Tree[rt].flag1==0)
{  id=0; return ;}
int mid=Tree[rt].mid();
if(k>mid)  Query(rt<<1|1,k);
else  Query(rt<<1,k);
}
int main()
{
//FF;
int n,m,i;
while(~scanf("%d%d",&n,&m))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
Build(1,1,n);
int x,y,k,w;
while(m--)
{
scanf("%d",&w);
if(w==1)
{
scanf("%d%d%d",&x,&y,&k);
Update(1,y,y+k-1,x,y);
}
else
{
scanf("%d",&k);
Query(1,k);
if(id==0)
printf("%d\n",b[k]);
else
printf("%d\n",a[id+k-id1]);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: