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

题目链接

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));
        }
    }
}

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

时间: 2024-07-30 13:58:29

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

树状数组模板1——单点修改区间查询

树状数组的模板,修改单点的值,查询某个区间 1 #include<cstdio> 2 #include<cctype> 3 using namespace std; 4 5 int c[500010],n,m; 6 7 int read() 8 { 9 int x=0,f=1; 10 char c=getchar(); 11 while (!isdigit(c)) 12 f=c=='-'?-1:1,c=getchar(); 13 while (isdigit(c)) 14 x=(x

树状数组模板(持续更新)

树状数组题目(持续更新) \(1.\) 树状数组 \(1\) :单点修改,区间查询 \(2.\) 树状数组 \(2\) :区间修改,单点查询 \(3.\) 树状数组 \(3\) :区间修改,区间查询 树状数组单点修改,区间查询和 $View$ $Code$ //省略头文件 using namespace std; inline int read() { int ret=0,f=1; char ch=getchar(); while(ch>'9'||ch='0'&&ch 树状数组区间修

HDU 1556-Color the ball(树状数组-区间修改 单点查询)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15491    Accepted Submission(s): 7731 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ

树状数组模板题(特强浓雾

#include<bits/stdc++.h> using namespace std; int a,b; int c[100005]; void add(int x,int y)//树状数组初始化 { while(x<=a+b) c[x]+=y,x+=x&-x;//露馅了噗哈哈哈哈哈哈蛤哈哈哈哈哈哈 } int ask(int x)//询问 { int ans=0; while(x) ans+=c[x],x-=x&-x; return ans; } int main()

hdu 1541 Stars 树状数组模板题

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int  maxn=15010; const int maxlen=32010; int tree[4*maxn]; int lowbit(int n) { return (n&-n); } int getsum(int i) { int sum=0; while(i>0) { sum+=tree

树状数组模板题 P1904

Description 给定一个长度为n的整数数组A[1].A[2].-.A[n](-10^9 Input 第一行包含两个整数n和m,表示数组有n个元素,m表示有m个操作:接下来的一行包含n个整数,第i个整数表示A[i]:再接下来的m行,每行表示一个操作. Output 按输入顺序输出操作2的结果. Hint 反正要用long long Solution 嗯,这个题,告诉我们,位运算要打括号... #include<cstdio> #include<cstring> #includ

Light bulbs (树状数组模板题)

There are N light bulbs indexed from 00 to N−1. Initially, all of them are off. A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)means to flip all bulbs x such that L≤x≤R. So for example, FLIP(3, 5) means to flip bulbs 3

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T