1110: RMQ with Shifts
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 369 Solved: 93
[ Submit][ Status][ Web Board]
Description
Input
There will be only one test case, beginning with two integers n, q (1<=n<=100,000, 1<=q<=120,000), the number of integers in array A, and the number of operations. The next line contains n positive integers not greater than 100,000, the initial elements in array A. Each of the next q lines contains an operation. Each operation is formatted as a string having no more than 30 characters, with no space characters inside. All operations are guaranteed to be valid. Warning: The dataset is large, better to use faster I/O methods.
Output
For each query, print the minimum value (rather than index) in the requested range.
Sample Input
7 5
6 2 4 8 5 1 4
query(3,7)
shift(2,4,5,7)
query(1,4)
shift(1,2)
query(2,2)
Sample Output
1
4
6
HINT
Source
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1110
//一看 是线段树 就写了
//结果无限 RE 真TMD 无语 不就是 单点更新 区间查询吗
//后面发现没初始化 没初始化也不可能是RE啊 WA的想不通
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXN 100010
#define inf 0x3f3f3f3f
int sum[MAXN*4];
int val[MAXN];
void pushup(int rt)
{
sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=val[l];
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=val[p];
return;
}
int m=(l+r)>>1;
if(p<=m) update(p,lson);
else update(p,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return sum[rt];
int ret=inf;
int m=(l+r)>>1;
if(L<=m) ret=min(ret,query(L,R,lson));
if(R>m) ret=min(ret,query(L,R,rson));
return ret;
}
int main()
{ char str[50][50];
int zs[50];char op[100];
int i,j,n,m,k,kk,s,a,b;
scanf("%d%d",&n,&m);
for(i=1; i<=n; i++)
scanf("%d",&val[i]);
build(1,n,1);
for(i=0; i<m; i++)
{
memset(str,'\0',sizeof(str));//初始化
kk=0;
k=0;
scanf("%s",op);
if(op[0]=='q')
{
for(j=6; op[j]!='\0'; j++)
{
if(op[j]==','||op[j]==')')
{kk++;
k=0;
continue;}
str[kk][k]=op[j];
k++;
}
s=0;
for(j=0; j<kk; j++)
{
zs[s++]=atoi(str[j]);
}
a=zs[0];
b=zs[1];
printf("%d\n",query(a,b,1,n,1));
}
else
{
for(j=6; op[j]!='\0'; j++)
{
if(op[j]==','||op[j]==')')
{kk++;
k=0;
continue;}
str[kk][k]=op[j];
k++;
}
s=0;
for(j=0; j<kk; j++)
{
zs[s++]=atoi(str[j]);
}
int tt=val[zs[0]];
for(j=0; j<s-1; j++)
val[zs[j]]=val[zs[j+1]];//交换值
val[zs[s-1]]=tt;
for(j=0; j<s; j++) //一个个更新
update(zs[j],1,n,1);
}
}
return 0;
}
//字符串一旦用了第二次一定用考虑 '\0' 很多字符串函数都必须以它结尾