求助 为什么只设每个区间的最大值不行

P4513 小白逛公园

Wjx12wjX @ 2023-12-08 13:59:54

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define lowbit(x) (x & ( -x ))
//#define i64 __int64
//typedef __int64 i64;
typedef long long ll;
typedef unsigned long long ull;
const ll N = 5e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
ll a[N], summ[N], v[N], fa[N], in[N], b[N], num[N];
ll n, m, d, h, x, y, z, k, w, pre, cnt, res, len, tot, ans, sum, now, maxn, minn = 1e18;
priority_queue <ll, vector<ll>, greater<ll> > pq;   //less 降序排列 优先队列
priority_queue <ll, vector<ll>, less<ll> > pq1;
string s, s1, s2;
char ss[N];
vector <ll> vis[N], vis1;
map <ll, ll> mp1;
unordered_map <ll, ll> mpp, mp;
pair <ll, ll> pi[N];
//bool ok[N];
ull qpow(ull a, ull n) {
    ull ans = 1;
    while (n)
    {
        if (n & 1)
            ans = (ans * a) % mod;
        a = (a * a) % mod;
        n >>= 1;
    }
    return ans;
}
// 快速幂
ll ls(ll p) { return p << 1; }
ll rs(ll p) { return p << 1 | 1; }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct Tree {
    ll l, r;
    ll val, maxn = -N;
}tree[N << 2];
void push_up(ll p) { 
    tree[p].val = tree[ls(p)].val + tree[rs(p)].val; 
    tree[p].maxn = max(tree[p].val, max(tree[ls(p)].maxn, tree[rs(p)].maxn));
}
void build(ll p, ll l, ll r) {
    tree[p] = { l,r };
    if (l == r) {
        tree[p] = { l,r,a[l],a[l] };
        return;
    }
    ll mid = l + r >> 1;
    build(ls(p), l, mid), build(rs(p), mid + 1, r);
    push_up(p);
}
void modify(ll p, ll l, ll d) {
    if (tree[p].l == tree[p].r && l == tree[p].l) {
        tree[p].val = tree[p].maxn = d;
        return;
    }
    ll mid = tree[p].l + tree[p].r >> 1;
    if (l <= mid)   modify(ls(p), l, d);
    if (l > mid)    modify(rs(p), l, d);
    push_up(p);
}
ll findd(ll p, ll l, ll r) {
    if (tree[p].l >= l && tree[p].r <= r)   return tree[p].maxn;
    ll mid = tree[p].l + tree[p].r >> 1;
    ll ans = -1e18;
    if (l <= mid)   ans = max(ans, findd(ls(p), l, r));
    if (r > mid)    ans = max(ans, findd(rs(p), l, r));
    return ans;
}
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    build(1, 1, n);
    while (m--) {
        cin >> k >> x >> y;
        if (k == 1) {
            if (x > y)  swap(x, y);
            cout << findd(1, x, y) << "\n\n";
        }
        else    modify(1, x, y);
    }
}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); std::cout.tie(0);
    //int t; cin >> t;  
    //while (t--)
        solve();
    return 0;
}
/*

*/

by Miss_SGT @ 2023-12-08 14:07:23

求得是连续的最大的


by Wjx12wjX @ 2023-12-08 18:58:23

@zhouchenqiao1 对啊 一个区间的和不就是连续的吗


by Miss_SGT @ 2023-12-10 07:17:58

@Wjx12wjX 可是你求得最大值啊


|