您的位置:首页 > 其它

hdu1754I Hate It

2016-04-14 19:31 323 查看

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754

题意:中文题。

分析:线段树练习题。单点更新,区间查询最大值。(那些跑得快的应该是写的zkw线段树吧。)

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=200100;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
char s[3];
int a
,mx[4*N];
void build(int x,int l,int r) {
if (l==r) { mx[x]=a[l];return ; }
int mid=(l+r)>>1;
build(2*x,l,mid);
build(2*x+1,mid+1,r);
mx[x]=max(mx[2*x],mx[2*x+1]);
}
int query(int x,int l,int r,int L,int R) {
if (l==L&&r==R) return mx[x];
int mid=(l+r)>>1;
if (R<=mid) return query(2*x,l,mid,L,R);
else if (L>mid) return query(2*x+1,mid+1,r,L,R);
else return max(query(2*x,l,mid,L,mid),query(2*x+1,mid+1,r,mid+1,R));
}
void updata(int x,int l,int r,int y,int z) {
if (l==r) { mx[x]=z;return ; }
int mid=(l+r)>>1;
if (y<=mid) updata(2*x,l,mid,y,z);
else updata(2*x+1,mid+1,r,y,z);
mx[x]=max(mx[2*x],mx[2*x+1]);
}
int main()
{
int i,n,m,l,r;
while (scanf("%d%d", &n, &m)!=EOF) {
for (i=1;i<=n;i++) scanf("%d", &a[i]);
build(1,1,n);
while (m--) {
scanf("%s%d%d", s, &l, &r);
if (s[0]=='Q') printf("%d\n", query(1,1,n,l,r));
else updata(1,1,n,l,r);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: