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位置开始,
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; }
相关文章推荐
- 【CodeForces】240F - TorCoder 线段树
- 【CodeForces】 19D Points(线段树|单点更新)
- codeforces 580 E. Kefa and Watch (字符串hash + 线段树)
- 线段树 CodeForces 61E 逆序对
- Codeforces 276C Little Girl and Maximum Sum(线段树的区间更新)
- Codeforces 482B. Interesting Array 线段树
- 【扫描线+贪心+线段树】Codeforces 458C Elections
- CodeForces - 668D Little Artem and Time Machine(线段树||树状数组)
- codeforces 799C(线段树区间更新)
- codeforces 383C Propagating tree 线段树
- CodeForces 91B Queue (线段树单点操作)
- codeforces 558E A Simple Task 线段树
- Codeforces 903G Yet Another Maxflow Problem 线段树+网络流
- CodeForces 173E Camping Groups 离线线段树 树状数组
- codeforces 111 160E 线段树
- Codeforces 677D 二维线段树 + 重要技巧
- CodeForces 12D Ball 多级排序 + 离散 + 线段树
- CodeForces 145E Lucky Queries (线段树)
- codeforces 100792B Banana Brain's Bracelet Z-Box算法 - 线段树
- CodeForces 91B Queue【线段树】