hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询

单点更新 区间查询

#include <bits/stdc++.h>
using namespace std;
#define m ((l+r)/2)
#define ls (rt<<1)
#define rs (rt<<1|1)
const int N = 2e6+10;

int s[N], tr[N];

void build(int rt,int l,int r)
{
    if(l == r) {
        tr[rt] = s[l];
        return ;
    }
    build(ls, l, m);
    build(rs, m+1, r);
    tr[rt] = min(tr[ls], tr[rs]);
}

void update(int rt,int l,int r,int pos,int val)
{
    if(l == r && l == pos) {
        tr[rt] = val;
        return ;
    }
    if(pos <= m)
        update(ls, l, m, pos, val);
    else
        update(rs, m+1, r, pos, val);
    tr[rt] = min(tr[ls], tr[rs]);
}

int query(int rt,int l,int r,int L,int R)
{
    if(L<=l && r<=R) {
        return tr[rt];
    }
    int res = 0x3f3f3f3f;
    if(L <= m)
        res = min(res, query(ls,l,m,L,R));
    if(R > m)
        res = min(res, query(rs,m+1,r,L,R));
    return res;
}
void out(int rt,int l,int r)
{
    printf("%d %d %d\n", l, r, tr[rt]);
    if(l == r ) {
        return ;
    }
    out(ls, l, m);
    out(rs, m+1, r);
}
int main()
{
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n; scanf("%d", &n);
    for(int i=1; i<=n; i++) {
        scanf("%d", &s[i]);
    }
    build(1,1,n);
    // out(1,1,n);
    int t; scanf("%d", &t);
    while(t--) {
        int op,l,r;
        scanf("%d %d %d", &op, &l, &r);
        if(op) {
            update(1,1,n,l,r);
            //out(1,1,n);
        }
        else {
            printf("%d\n", query(1,1,n,l,r));
        }
        // out(1,1,n);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Draymonder/p/10010356.html

时间: 2024-07-29 07:24:02

hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询的相关文章

Uva 12299 RMQ with Shifts(线段树 + 单点更新 )

Uva 12299 RMQ with Shifts (线段树 + 单点更新) 题意: 对于给定的序列 x[i]给出一个操作 shift(a,b,c,d,e) 对应的是将 x[a]与x[b] x[b]与x[c] 这样相邻的两两交换For example, if A={6, 2, 4, 8, 5, 1, 4}then shift(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that,shift(1, 2) yields {8, 6, 4, 5, 4

HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

hihoCoder #1077 RMQ问题再临-线段树

#1077 : RMQ问题再临-线段树 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 上回说到:小Hi给小Ho出了这样一道问题:假设整个货架上从左到右摆放了N种商品,并且依次标号为1到N,每次小Hi都给出一段区间[L, R],小Ho要做的是选出标号在这个区间内的所有商品重量最轻的一种,并且告诉小Hi这个商品的重量.但是在这个过程中,可能会因为其他人的各种行为,对某些位置上的商品的重量产生改变(如更换了其他种类的商品

Hihocoder #1077 : RMQ问题再临-线段树(线段树:结构体建树+更新叶子往上+查询+巧妙使用father[]+线段树数组要开大4倍 *【模板】)

#1077 : RMQ问题再临-线段树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到:小Hi给小Ho出了这样一道问题:假设整个货架上从左到右摆放了N种商品,并且依次标号为1到N,每次小Hi都给出一段区间[L, R],小Ho要做的是选出标号在这个区间内的所有商品重量最轻的一种,并且告诉小Hi这个商品的重量.但是在这个过程中,可能会因为其他人的各种行为,对 某些位置上的商品的重量产生改变(如更换了其他种类的商品). 小Ho提出了两种非常简单的方法,但是都不能

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

HDU1166(线段树单点更新区间查询)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 62483    Accepted Submission(s): 26386 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就

CDOJ 1073 线段树 单点更新+区间查询 水题

H - 秋实大哥与线段树 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit Status Practice UESTC 1073 Appoint description:  System Crawler  (2016-04-24) Description “学习本无底,前进莫徬徨.” 秋实大哥对一旁玩手机的学弟说道. 秋实大哥是一个爱学习的人,今天他刚刚学习了线段树这个数据结构. 为

nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], …, A[R]. Note that the indic

csu 1110 RMQ with Shifts (线段树单点更新)

#include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <iostream> #include <cstdlib> #define lson l,m,rt<<1 #define rson m + 1,r ,rt<<1|1 using namespace s