60ptsWA求条

P1253 扶苏的问题

gjiajia @ 2024-09-16 23:32:16

翻出了一道很久之前wa的题

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 5 * 1e6;
const ll MIN = -1e17;
inline ll get()
{
    char c;
    ll sign = 1;
    while((c = getchar()) < '0' || c > '9')
    {
        if(c == '-')
            sign = -1;
    }
    ll res = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        res = res * 10 + c - '0';
    return res * sign;
}
ll ch[N], tag[N], rt[N], a[N], n;
void build(ll u, ll l, ll r)
{
    if(l == r) 
    {
        rt[u] = a[l];
        return;
    }
    ll mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    rt[u] = max(rt[u << 1], rt[u << 1 | 1]);
    //cout << u << " " << l << " " << r << " " << rt[u] << endl;
}
void Add(ll u, ll v)
{
    if(ch[u] != MIN) ch[u] += v;
    else tag[u] += v;
    rt[u] += v;
}
void rech(ll u, ll v)
{
    rt[u] = v;
    tag[u] = 0;
    ch[u] = v;
}
void pushdown(ll u)
{
    if(tag[u] != 0)
    {
        Add(u << 1, tag[u]);
        Add(u << 1 | 1, tag[u]);
        tag[u] = 0;////归零注意一下 
    }
    if(ch[u] != MIN)
    {
        rech(u << 1, ch[u]);
        rech(u << 1 | 1, ch[u]);
        ch[u] = MIN;
    }
}
void modify(ll u, ll l, ll r, ll x, ll y, ll v)
{
//    pushdown(u, l, r);
    if(l >= x && r <= y) 
    {
        if(ch[u] != MIN) ch[u] = MIN;
        return Add(u, v);
    }
    ll mid = l + r >> 1;
    pushdown(u);
    if(mid >= x) modify(u << 1, l, mid, x, y, v);
    if(mid < y) modify(u << 1 | 1, mid + 1, r, x, y, v);
    rt[u] = max(rt[u << 1], rt[u << 1 | 1]);
}
void change(ll u, ll l, ll r, ll x, ll y, ll v)
{
    //注意要清之前的标记ch[u] 
    if(l >= x && r <= y) 
    {
        return rech(u, v);
        //cout << u << " " << rt[u] << " " << l << " " << r << " "  << ch[u] << endl;
    }
    ll mid = l + r >> 1;
    pushdown(u);
    if(mid >= x) change(u << 1, l, mid, x, y, v);
    if(y > mid) change(u << 1 | 1, mid + 1, r, x, y, v);
    rt[u] = max(rt[u << 1], rt[u << 1 | 1]);
    //cout << u << " " << rt[u] << " " << l << " " << r << " "  << ch[u] << endl;
}
ll query(ll u, ll l, ll r, ll x, ll y)
{
    if(l >= x && r <= y) return rt[u];
    ll mid = l + r >> 1, res = MIN;
//    if(ch[u] != MIN) 
//    {
//      rech(u << 1, ch[u]);
//      rech(u << 1 | 1, ch[u]);
//      ch[u] = MIN;
//  }
    pushdown(u);
    if(mid >= x) res = max(res, query(u << 1, l, mid, x, y));
    if(y > mid) res = max(res, query(u << 1 | 1, mid + 1, r, x, y));
    return res;
}
void print(ll u, ll l, ll r)
{
    if(l == r) 
    {
        cout << u << " " << l << " " << r << " " << rt[u] << " " << tag[u] << " " << ch[u] << endl;
        return;
    }
    cout << u << " " << l << " " << r << " " << rt[u] << " " << tag[u] << " " << ch[u] << endl;
    ll mid = l + r >> 1;
    print(u << 1, l, mid);
    print(u << 1 | 1, mid + 1, r);
}
signed main()
{
    //freopen("1253_7.in", "r", stdin);
    ll Q;
    n = get(), Q = get(); 
    for(ll i = 1; i <= n; i++) a[i] = get();
    for(ll i = 0; i < N - 1; i++) rt[i] = ch[i] = MIN;
    //cout << rt[1] << endl;
    build(1, 1, n);
    while(Q--)
    {
        ll opt, l, r, x;
        opt = get(), l = get(), r = get();
        if(opt == 1) 
        {
            x = get();
            change(1, 1, n, l, r, x);
//            cout << "////\n";

            //cout << endl;
        }
        else if(opt == 2)
        {
            x = get();
            modify(1, 1, n, l, r, x);
        }
        else
        {
            cout << query(1, 1, n, l, r) << endl;
        }
//        print(1, 1, n);
//        cout << "/////\n";
    }
    return 0;
 } 
/*
4 3
10 4 -3 -7
1 1 3 0
2  4 -9
3 1 3

*/

by I_like_play_eggy @ 2024-09-16 23:41:40

@gjiajia 都几点了还在卷题.jpg


by I_like_play_eggy @ 2024-09-16 23:43:21

你算好了,我几个月样例没过qwq


by I_like_play_eggy @ 2024-09-16 23:46:07

@gjiajia 下面可供参考


by I_like_play_eggy @ 2024-09-16 23:47:26

我也是一直处理不好样例2


by gjiajia @ 2024-09-17 07:41:44

@I_like_play_eggy 我样例2倒过了


by gjiajia @ 2024-09-17 07:42:26

而且处理了0的赋值


|