1110: RMQ with Shifts
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 298 Solved: 66
[ 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
对线段树的理解还是不够深入。
这题的需要的是RMQ区间更新。每次操作还要左移序列,和以前写的更新函数不太一样。我在左移操作之后又Build了一下,虽然运行结果是对的,但是最终还是超时了。
现在先把超时的代码存档。待我再研究研究。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 0x3f3f3f3f
#define N 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int a[N];
int mi[N<<2];
void PushUp(int rt)
{
mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}
void Build(int l,int r,int rt)
{
if(l==r)
{
mi[rt]=a[l];
return;
}
int m=(l+r)>>1;
Build(lson);
Build(rson);
PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return mi[rt];
}
int m=(l+r)>>1;
int ret=INF;
if(L<=m)
ret=min(ret,query(L,R,lson));
if(R>m)
ret=min(ret,query(L,R,rson));
return ret;
}
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)>0)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
Build(1,n,1);
while(q--)
{
char s[30];
scanf("%s",s);
if(s[0]=='q')
{
int a,b;
sscanf(s+5,"(%d,%d)",&a,&b);
printf("%d\n",query(a,b,1,n,1));
}
else
{
int shift[30],k=0,len=strlen(s);
for(int i=6;i<len;i+=2)
shift[k++]=(s[i]-'0');
int temp=a[shift[0]];
for(int i=0;i<k-1;i++)
a[shift[i]]=a[shift[i+1]];
a[shift[k-1]]=temp;
Build(1,n,1);
}
}
}
return 0;
}
/**************************************************************
Problem: 1110
User: 0905130123
Language: C++
Result: Time Limit Exceed
****************************************************************/