Educational Codeforces Round 29

来源:互联网 发布:安信证券客户经理 知乎 编辑:程序博客网 时间:2024/06/04 01:23

D. Yet Another Array Queries Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a of size n, and q queries to it. There are queries of two types: 

  • 1 li ri — perform a cyclic shift of the segment [li, ri] to the right. That is, for every x such that li ≤ x < ri new value of ax + 1 becomes equal to old value of ax, and new value of ali becomes equal to old value of ari
  • 2 li ri — reverse the segment [li, ri]

There are m important indices in the array b1b2, ..., bm. For each i such that 1 ≤ i ≤ m you have to output the number that will have index bi in the array after all queries are performed.

Input

The first line contains three integer numbers nq and m (1 ≤ n, q ≤ 2·1051 ≤ m ≤ 100). 

The second line contains n integer numbers a1a2, ..., an (1 ≤ ai ≤ 109). 

Then q lines follow. i-th of them contains three integer numbers tiliri, where ti is the type of i-th query, and [li, ri] is the segment where this query is performed (1 ≤ ti ≤ 21 ≤ li ≤ ri ≤ n). 

The last line contains m integer numbers b1b2, ..., bm (1 ≤ bi ≤ n) — important indices of the array. 

Output

Print m numbers, i-th of which is equal to the number at index bi after all queries are done.

Example
input
6 3 51 2 3 4 5 62 1 32 3 61 1 62 2 1 5 3
output
3 3 1 5 2 

题意:

给你n个数a[],给你q个操作,1操作,让l到r里的数都向前转一圈。l变成r,l+1变成l,l+2变成l+1。r变成r-1。如此变化。

2操作,反转l,r里的所有数。

问你m个数b[],求出变化后的a[bi]。

POINT:

虽然这题一开始很容易想到线段树,但其实一点关系都没有。一开始会想到按照q操作来巧妙的求出a[],但其实想不出。

因为m<=100,那么我们就来根据要求的a[bi],来推出他原来是什么a[ans]。

保存这q个操作,对每一个bi进行倒推,每次操作如果影响到了bi,就倒着模拟一次。因为每次操作只影响l到r的范围。所以bi在l-r之外的话就不用管。当然,这个bi是根据倒推的进程在变化的。

时间复杂度为m*q。


#include <iostream>#include <string.h>#include <stdio.h>#include <math.h>#include<algorithm>using namespace std;#define LL long longconst int maxn = 200000+100;int a[maxn];int n,q,m;int ans[111];struct node{    int kind;    int l,r;}opt[maxn];void doit(int temp,int now){    for(int i=q;i>=1;i--){        int k=opt[i].kind;        int l=opt[i].l;        int r=opt[i].r;        if(k==1){            if(l<=temp&&temp<=r){                temp--;                if(temp==l-1)                    temp=r;            }        }        else{            if(l<=temp&&temp<=r){                int c=temp-l;                temp=r-c;            }        }    }    printf("%d",a[temp]);}int main(){    scanf("%d %d %d",&n,&q,&m);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);    }    memset(ans,0,sizeof ans);    for(int i=1;i<=q;i++){        scanf("%d %d %d",&opt[i].kind,&opt[i].l,&opt[i].r);    }    for(int i=1;i<=m;i++){        if(i-1) printf(" ");        int a;        scanf("%d",&a);        doit(a,a);    }    printf("\n");}



原创粉丝点击