小A盗墓 upc(线段树)

/*
upc
问题 E: 小A盗墓
时间限制: 5 Sec  内存限制: 128 MB
题目描述
小A终于通过了保安的考验,来到了古墓门前,古墓门前有n根柱子,第i根柱子的高度是整数。古墓的门上会弹出一些暗号,机智小A猜到这个暗号表示询问第l到第r根柱子的高度在升序排序后是否构成一段连续且上升的序列。并且这些柱子的高度还可能在弹出暗号的过程中出现变化。

现在小A需要回答出每个暗号的答案

输入
第一行两个整数,表示柱子的个数n以及操作的个数m。

第二行n个整数,第i个整数表示第i根柱子的高度。

接下来m行,每行三个整数opt, x, y。当opt=1时,表示把第x根柱子的高度改为y;当opt=2时,表示询问第x到第y根柱子的高度在升序排序后是否构成一段连续且上升的序列。若是,则输出Yes,否则输出No。

输出
对于每个询问输出一行Yes或No。

样例输入
样例数据
5 5
1 2 3 4 5
2 1 5
2 2 3
2 3 3
1 3 6
2 3 5
样例输出
Yes
Yes
Yes
Yes
*/

#include<bits/stdc++.h>

#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
struct node {
    int left, right;
    int mx, mn;
    ll sum;
} rt[maxn * 4];
int co[maxn];

int tl(int x) { return x << 1; }

int tr(int x) { return x << 1 | 1; }

void pushup(int x) {
    rt[x].sum = rt[tl(x)].sum + rt[tr(x)].sum;
    rt[x].mx = max(rt[tl(x)].mx, rt[tr(x)].mx);
    rt[x].mn = min(rt[tl(x)].mn, rt[tr(x)].mn);
}

void build(int x, int L, int R) {
    rt[x].left = L;
    rt[x].right = R;
    if (L == R) {
        rt[x].sum = rt[x].mn = rt[x].mx = co[L];
        return;
    }
    int mid = (L + R) / 2;
    build(tl(x), L, mid);
    build(tr(x), mid + 1, R);
    pushup(x);
}

void updata(int x, int pos, int val) {
    if (rt[x].left == rt[x].right) {
        rt[x].sum = rt[x].mn = rt[x].mx = val;
        return;
    }
    int mid = (rt[x].left + rt[x].right) / 2;
    if (pos <= mid)updata(tl(x), pos, val);
    else updata(tr(x), pos, val);
    pushup(x);
}

ll query(int x, int L, int R) {
    if (L == rt[x].left && R == rt[x].right) {
        return rt[x].sum;
    }
    int mid = (rt[x].left + rt[x].right) / 2;
    if (R <= mid) return query(tl(x), L, R);
    else if (L > mid) return query(tr(x), L, R);
    else return query(tl(x), L, mid) + query(tr(x), mid + 1, R);
}

ll getmin(int x, int L, int R) {
    if (L == rt[x].left && R == rt[x].right) {
        return rt[x].mn;
    }
    int mid = (rt[x].left + rt[x].right) / 2;
    ll ret = INF;
    if (R <= mid) return min(ret, getmin(tl(x), L, R));
    else if (L > mid) return min(ret, getmin(tr(x), L, R));
    else return min(getmin(tl(x), L, mid), getmin(tr(x), mid + 1, R));
}

ll getmax(int x, int L, int R) {
    if (L == rt[x].left && R == rt[x].right) {
        return rt[x].mx;
    }
    int mid = (rt[x].left + rt[x].right) / 2;
    ll ret = -INF;
    if (R <= mid) return max(ret, getmax(tl(x), L, R));
    else if (L > mid) return max(ret, getmax(tr(x), L, R));
    else return max(getmax(tl(x), L, mid), getmax(tr(x), mid + 1, R));
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", co + i);
    build(1, 1, n);
    ll po, wo, lo;
    while (m--) {
        int o, x, y;
        scanf("%d%d%d", &o, &x, &y);
        if (o == 1) updata(1, x, y);
        else {
            po = query(1, x, y);
            lo = getmax(1, x, y);
            wo = getmin(1, x, y);
            if ((po == (lo + wo) * (y - x + 1) / 2) && (lo == wo + (y - x)))
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值