码迷,mamicode.com
首页 > 编程语言 > 详细

HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

时间:2020-02-16 01:39:45      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:--   getchar   har   ref   pid   pre   order   out   cos   

题目链接

ac代码(注意字符读入前需要注意回车的影响)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define mp make_pair
#define pi acos(-1)
#define pii pair<int, int>
#define pll pair<long long , long long>
#define ll long long
#define ld long double
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
using namespace std;
int N, M;
int s, c[maxn], d[maxn];
char order;
int lowbit(int x)
{
    return x & (-x);
}
void update(int x)
{
    while(x <= N)
    {
        d[x] = c[x];
        int lx = lowbit(x);
        for(int i = 1; i < lx; i <<= 1)
            d[x] = max(d[x], d[x - i]);
        x += lowbit(x);
    }
}
int getmax(int l, int r)
{
    int ans = 0;
    while(r >= l)
    {
        ans = max(ans, c[r]);
        --r;
        while(r - lowbit(r) >= l)
        {
            ans = max(ans, d[r]);
            r -= lowbit(r);
        }
    }
    return ans;
}
int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    while(scanf("%d %d", &N, &M) != EOF)
    {
        memset(d, 0, sizeof(d));
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &c[i]);
            update(i);
        }
        int a, b;
        while(M--)
        {
            getchar();
            scanf("%c %d %d", &order, &a, &b);
            if(order == 'U')
            {
                c[a] = b;
                update(a);
            }
            else if(order == 'Q')
                printf("%d\n", getmax(a, b));
        }
    }
}

HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

标签:--   getchar   har   ref   pid   pre   order   out   cos   

原文地址:https://www.cnblogs.com/KeepZ/p/12315280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!