hdu 1754 模板

来源:互联网 发布:淘宝客服售后怎么做 编辑:程序博客网 时间:2024/04/28 11:20

       感觉此模板较好,虽然空间是O(4N)的,但操作简单,不用单写一个构树函数。

     代码如下:

#include <cstdio>#include <stack>#include <set>#include <iostream>#include <string>#include <vector>#include <queue>#include <list>#include <functional>#include <cstring>#include <algorithm>#include <cctype>#include <string>#include <map>#include <iomanip>#include <cmath>#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define F first#define S second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define LC rt << 1, l, mid#define RC rt << 1|1, mid + 1, r#define LRT rt << 1#define RRT rt << 1|1#define BitCount(x) __builtin_popcount(x)#define BitCountll(x) __builtin_popcountll(x)#define LeftPos(x) 32 - __builtin_clz(x) - 1#define LeftPosll(x) 64 - __builtin_clzll(x) - 1const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;using namespace std;const double eps = 1e-5;const int MAXN = 300 + 10;const int MOD = 1000007;const double M=1e-8;const int N=1e6+10;typedef pair<int, int> pii;typedef pair<int, string> pis;const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};int n,m,t[N];void updata(int k,int a){    k+=n-1;    t[k]=a;    while(k>0) {        k>>=1;        t[k]=max(t[k<<1],t[k<<1|1]);    }}int query(int a,int b,int rt,int l,int r){    int i,j;    if (b<=l || r<=a) return -1;    if (a<=l && r<=b) return t[rt];    else {        int u=query(a,b,rt<<1,l,(l+r)>>1);        int v=query(a,b,rt<<1|1,(l+r)>>1,r);        return max(u,v);    }}int main(){    int i,j;    while(~scanf("%d%d",&n,&m))    {        int r=n;        n=1;        while(n<r) n*=2;        for (i=1;i<=r;i++) {            int a;            scanf("%d",&a);            updata(i,a);        }       //for (i=1;i<=2*n-1;i++) cout<<t[i]<<" "; cout<<endl;        for (i=0;i<m;i++) {            char c[2];            int a,b;            scanf("%s%d%d",c,&a,&b);            if (c[0]=='Q') {                cout<<query(a,b+1,1,1,n+1)<<endl;  // 查询区间是 [a,b),所以区间右端要加1            }            else {                updata(a,b);            }        }    }}


0 0
原创粉丝点击