7.22 N HDU 1754 I Hate It

来源:互联网 发布:叫停网络售药 编辑:程序博客网 时间:2024/05/29 16:54

题意:

要求维护n个学生的成绩,要求能修改单个学生的成绩,能快速查区间和。

思路:

裸的线段树。

代码:

//coder:OX_louis#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <stack>using namespace std;typedef pair<int, int> pii;typedef long long ll;typedef unsigned long long ull;typedef vector<int> vi;#define MP(a,b) make_pair(a,b)#define pr(x) cout << #x << ": " << x << "  "#define pl(x) cout << #x << ": " << x << endl;#define pri(a) printf("%d\n",(a))#define xx first#define yy second#define sa(n) scanf("%d", &(n))#define sal(n) scanf("%lld", &(n))#define sai(n) scanf("%I64d", &(n))#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++)#define rep(i,a,b) for(int (i)=(a); (i)<(b); (i)++)#define per(i,a,b) for(int (i)=(b)-1; (i)>=(a); (i)--)#define CLR(a,i) memset((a),(i),sizeof(a))int n,m;int _ql,_qr;//查询[ql,qr]中的最大值const int maxn=400001,logn=20;const int INF=1e9;int maxv[maxn*logn];//memsetint query(int o,int L,int R){    int M = L + (R-L)/2 , ans = -INF;    if(_ql <= L && R <= _qr) return maxv[o];    if(_ql <= M) ans = max(ans, query(o*2, L, M));    if(M < _qr)  ans = max(ans, query(o*2+1, M+1, R));    return ans;}int Query(int l,int r){    _ql = l; _qr = r;    return query(1,1,n);}int _p, _v;void update(int o,int L, int R){    int M = L +(R-L)/2;    if(L == R) maxv[o] = _v;//叶节点,直接更新maxv    else { //L < R        if(_p <= M) update(o*2, L, M); else update(o*2+1, M+1, R);        //然后计算本节点的maxv        maxv[o] = max(maxv[o*2], maxv[o*2+1]);    }}void Update(int a,int b){    _p = a; _v = b;    update(1,1,n);}int main(){    ios::sync_with_stdio(0);    while(cin>>n>>m){        rep(i,1,n+1){            int x;            cin>>x;            Update(i,x);        }        rep(i,0,m){            char q;            int x,y;            cin>>q>>x>>y;            if(q=='Q')cout<<(Query(x,y))<<endl;            else Update(x,y);        }    }    return 0;}

0 0