Hdu 5063 Operation the Sequence

来源:互联网 发布:数据库应用领域 编辑:程序博客网 时间:2024/05/01 06:03

题目链接 

Operation the Sequence

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 359    Accepted Submission(s): 151


Problem Description
You have an array consisting of n integers: a1=1,a2=2,a3=3,,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:
Type1: O 1 call fun1();
Type2: O 2 call fun2();
Type3: O 3 call fun3();
Type4: Q i query current value of a[i], this operator will have at most 50.
Global Variables: a[1…n],b[1…n];
fun1() {
index=1;
  for(i=1; i<=n; i +=2)
    b[index++]=a[i];
  for(i=2; i<=n; i +=2)
    b[index++]=a[i];
  for(i=1; i<=n; ++i)
    a[i]=b[i];
}
fun2() {
  L = 1;R = n;
  while(L<R) {
    Swap(a[L], a[R]);
    ++L;--R;
  }
}
fun3() {
  for(i=1; i<=n; ++i)
    a[i]=a[i]*a[i];
}
 

Input
The first line in the input file is an integer T(1T20), indicating the number of test cases.
The first line of each test case contains two integer n(0<n100000),m(0<m100000).
Then m lines follow, each line represent an operator above.
 

Output
For each test case, output the query values, the values may be so large, you just output the values mod 1000000007(1e9+7).
 

Sample Input
13 5O 1O 2Q 1O 3Q 1
 

Sample Output
24

题意:略。

题解:对于第1种操作来说:假设操作后的位置为x,那么操作前的位置y为:

如果:x<=(n+1)/2,y=(x-1)*2+1

如果:x>(n+1)/2 , y=( x-(n+1)/2 )*2。

对于第2种操作来说:假设操作后的位置为x,那么操作前的位置y为:

y=n-x+1。

由于询问只有50,我们可以记录下每个操作,每次询问的时候从后往前推,推导出当前询问的位置的数初始的时候在哪个位置,在根据操作三的个数,就可以算出答案了。

代码如下:

#include<stdio.h>#include<algorithm>#include<queue>#include<stack>#include<map>#include<set>#include<vector>#include<iostream>#include<string.h>#include<string>#include<math.h>#include<stdlib.h>#define inff 0x3fffffff#define eps 1e-8#define nn 110000#define mod 1000000007typedef __int64 LL;const LL inf64=LL(inff)*inff;using namespace std;int n,m;int op[nn];LL solve(LL x,int y){    int i;    for(i=1;i<=y;i++)    {        x=(x*x)%mod;    }    return x;}int main(){    int t;    char s[5];    int x,i,j;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        int ix=0;        int lv=0;        for(i=1;i<=m;i++)        {            scanf("%s %d",s,&x);            if(s[0]=='O')            {                if(x==3)                    ix++;                else                    op[lv++]=x;            }            else            {                for(j=lv-1;j>=0;j--)                {                    if(op[j]==1)                    {                        if(x<=(n+1)/2)                        {                            x=(x-1)*2+1;                        }                        else                            x=(x-(n+1)/2)*2;                    }                    else                        x=n-x+1;                }                printf("%I64d\n",solve(x,ix));            }        }    }    return 0;}



0 0