I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 54679    Accepted Submission(s): 21396

Problem Description

Input

Output

Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5

Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin

Author
linle

Source

通过建立一颗区间最大值树，来查找最大值和对节点进行更新，简单的线段树操作！

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int tree[800005];
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
void build(int rt,int l,int r)
{
if(l==r)
{
scanf("%d",&tree[rt]);
return;
}
int mid=(l+r)>>1,pt=rt<<1;
build(pt,l,mid);
build(pt|1,mid+1,r);
tree[rt]=max(tree[pt],tree[pt|1]);
}
void updata(int rt,int l,int r,int a,int b)
{
if(l==r)
{
tree[rt]=b;
return;
}
int mid=(l+r)>>1,pt=rt<<1;
if(a<=mid)
{
updata(pt,l,mid,a,b);
}
if(a>mid)
{
updata(pt|1,mid+1,r,a,b);
}
tree[rt]=max(tree[pt],tree[pt|1]);
}
int query(int rt,int l,int r,int a,int b)
{
if(a<=l&&b>=r)
{
return tree[rt];
}
int mid=(l+r)>>1,pt=rt<<1,s=-INF;
if(a<=mid)
{
s=max(query(pt,l,mid,a,b),s);
}
if(b>mid)
{
s=max(query(pt|1,mid+1,r,a,b),s);
}
return s;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(1,1,n);
getchar();
for(int i=1;i<=m;i++)
{
int a,b;
char c;
scanf("%c%d%d",&c,&a,&b);
getchar();
if(c=='Q')
{
printf("%d\n",query(1,1,n,a,b));
}
else if(c=='U')
{
updata(1,1,n,a,b);
}
}
}
return 0;
}


03-15 716

09-14 6265

12-11 2640

12-16 865

07-01 1074

09-08 227

10-29 180

07-04 66

08-09 1716

11-18 1660