hzwer的迷の数列
题目描述 Description
hzwer找了一个人畜无害的迷の数列……
现在hzwer希望对这个数列进行一些操作,请你来回答hzwer的问题。
操作一:查询第i个数的大小
操作二:把第i个数的大小改成x
操作三:将整个序列反转。即把第i个数放到第n-i+1个。
输入描述 Input Description
输入数据第一行两个数n,m,表示数列长度和操作数。
第二行n个数,表示n个元素初始值。
以下m行,每行开头一个数opr,表示操作种类。
opr=1,则后面接一个数i,表示查询第i个数大小。
opr=2,则后面接两个数I,x,表示第i个数改成x。
opr=3,表示序列反转。
输出描述 Output Description
对于每个询问,输出答案。
样例输入 Sample Input
4 6
1 3 2 4
1 3
2 2 6
3
2 1 3
1 3
1 1
样例输出 Sample Output
2
6
3
数据范围及提示 Data Size & Hint
对于20%数据,1<=n,m<=10.
对于40%数据,1<=n,m<=100.
对于60%数据,1<=n,m<=1000.
对于100%数据,1<=n,m<=100000,1<=a[i]<=100000.
C++编程实现
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int *a=new int[n];
int *b=new int[m];
for(int i=0;i<n;i++)
cin>>a[i];
int op,q=0;
int times=0;
bool flag=false;
while(times<m)
{
cin>>op;
if(op==1)
{ int i;cin>>i;
if(flag==true)b[q++]=a[n-i];
else b[q++]=a[i-1];}
if(op==2)
{ int i,x;cin>>i>>x;
if(flag==true) a[n-i]=x;
else a[i-1]=x;}
if(op==3) flag=!flag;
times++;
}
if(q!=0)
for(int i=0;i<q;i++)
cout<<b[i]<<endl;
delete[]a,b;
return 0;
}