这题算是最基本的线段树的题目
如果刚学习线段树的还是建议自己不要看代码 独立的把代码敲出来。
不过这道题还是给我一点教训尽量别用cin cout 这个会卡一些时间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson l, mid, rt<<1
#define rson mid + 1, r, rt<<1|1
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps = 1e-5;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const ll MOD = 1000000007;
const int mod = 1000000007;
const int MAX = 1000000010;
const double pi = acos(-1.0);
const int MAXN=200000;
int T[MAXN << 2];
void PushUp(int rt)
{
T[rt] = max(T[rt << 1] ,T[rt << 1 |1]);
}
void Build(int l,int r,int rt)
{
if (l == r)
{
scanf("%d",&T[rt]);
return;
}
int mid = (r + l) >>1;
Build(lson);
Build(rson);
PushUp(rt);
}
void Update(int P, int C, int l, int r, int rt)
{
if (l == r)
{
T[rt] = C;
return;
}
int mid = (r + l)>>1;
if(P<=mid) Update(P, C, lson);
else Update(P, C, rson);
PushUp(rt);
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R)
return T[rt];
int mid = (r + l) >>1;
int ans=-1;
if(L<=mid) ans=max(ans,query(L,R,lson));
if(R>mid) ans=max(ans,query(L,R,rson));
return ans;
}
int main()
{
int n, m;
while (~scanf("%d%d",&n,&m))
{
char str[15];
int b,c;
Build(1, n, 1);
while (m--)
{
scanf("%s%d%d",str,&b,&c);
if (str[0] == 'U') Update(b, c, 1, n, 1);
if (str[0] == 'Q') printf("%d\n",query(b, c, 1, n, 1));
}
}
return 0;
}