CF 292E Copying Data

題目鏈接:http://codeforces.com/problemset/problem/292/E


題目大意:

給定兩個長度爲n(1~10^5)的數組a[]和數組b[],有兩個操作。

1: 1 x y k,令b[y+q]=a[x+q] (0<=q<k)

2: 2 x, 問當前的b[x]的值。

最多操作次數m爲10^5.


題目思路:

croc 2013 round1 的最後一題...呵呵了是個線段樹水題= =,尼瑪啊與round2檫肩而過。

維護兩個值,bx、by分別表示替換時在a[]中的起點和在b[]中的起點,by用來定位替換的位置。

初始值bx和by均爲0。

如果2 x詢問得到的bx爲0,那麼表示沒被替換過。

否則輸出a[bx+x-by]的值。


代碼:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll __int64
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define eps (1e-8)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
template <class T> T _abs(T x){return (x < 0)? -x:x;}
template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;}
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> void getmax(T& x,T y){x=(y > x)? y:x;}
template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;}
int TS,cas=1;
const int M=100000+5;
int n,m;
int a[M],b[M];
int bx[M<<2],by[M<<2];
struct node{
    int x,y;
    node(int _x=0,int _y=0){x=_x,y=_y;}
};

void build(int l,int r,int rt){
    bx[rt]=by[rt]=0;
    if(l==r) return;
    int mid=middle;
    build(lson),build(rson);
}

void pushDown(int rt){
    if(bx[rt] || by[rt]){
        bx[ls]=bx[rs]=bx[rt];
        by[ls]=by[rs]=by[rt];
        bx[rt]=by[rt]=0;
    }
}

void update(int l,int r,int rt,int L,int R,int x,int y){
    if(L<=l && r<=R){
        bx[rt]=x,by[rt]=y;
        return;
    }
    pushDown(rt);
    int mid=middle;
    if(L<=mid) update(lson,L,R,x,y);
    if(mid<R) update(rson,L,R,x,y);
}

node query(int l,int r,int rt,int p){
    if(l==r) return node(bx[rt],by[rt]);
    pushDown(rt);
    int mid=middle;
    if(p<=mid) return query(lson,p);
    else return query(rson,p);
}

void run(){
    int i,j;
    for(i=1;i<=n;i++) scanf("%d",&a[i]);
    for(i=1;i<=n;i++) scanf("%d",&b[i]);
    build(1,n,1);
    int x,y,k;
    while(m--){
        scanf("%d",&i);
        if(i==1){
            scanf("%d%d%d",&x,&y,&k);
            update(1,n,1,y,y+k-1,x,y);
        }else{
            scanf("%d",&x);
            node t=query(1,n,1,x);
            if(t.x == 0) printf("%d\n",b[x]);
            else printf("%d\n",a[t.x+x-t.y]);
        }
    }
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while((~scanf("%d%d",&n,&m))) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章