线段树90ptsWA#10求调

P1253 扶苏的问题

Nekopedia @ 2023-12-03 12:21:38

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll rd(){
   ll x = 0, f = 1; char c = getchar();
   while(! isdigit(c)){if(c == '-')f = - f; c = getchar();}
   while(isdigit(c)){x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}
   return x * f;
}

const int N = 1e6 + 5;
const ll max0810 = - 0x3f3f3f3f3f;
int n, m;
ll a[N], c[N << 2], tg1[N << 2], tg2[N << 2];
#define ls x << 1
#define rs x << 1 | 1
void upd(int x){
    c[x] = max(c[ls], c[rs]);
}
void build(int x, int l, int r){
    tg1[x] = max0810;
    if(l == r)return(void)(c[x] = a[l]);
    int mid = l + r >> 1;
    build(ls, l, mid); build(rs, mid + 1, r);
    upd(x);
}
void pdtg1(int x){
    if(tg1[x] == max0810)return;
    tg2[ls] = tg2[rs] = 0;
    c[ls] = c[rs] = tg1[ls] = tg1[rs] = tg1[x];
    tg1[x] = max0810;
}
void pd(int x){
    pdtg1(x);
    if(! tg2[x])return;
    c[ls] += tg2[x]; c[rs] += tg2[x];
    tg2[ls] += tg2[x]; tg2[rs] += tg2[x];
    tg2[x] = 0;
}
void mdfy(int x, int l, int r, int L, int R, int op, ll y){
    if(L <= l and r <= R){
        if(op ^ 1){
            pdtg1(x);
            c[x] += y; tg2[x] += y;
        }
        else{
            c[x] = tg1[x] = y;
            tg2[x] = 0;
        }
        return;
    }
    int mid = l + r >> 1; pd(x);
    if(L <= mid)mdfy(ls, l, mid, L, R, op, y);
    if(R > mid)mdfy(rs, mid + 1, r, L, R, op, y);
    upd(x);
}
ll qry(int x, int l, int r, int L, int R){
    if(L <= l and r <= R)return c[x];
    int mid = l + r >> 1; ll res = max0810; pd(x);
    if(L <= mid)res = qry(ls, l, mid, L, R);
    if(R > mid) res = max(res, qry(rs, mid + 1, r, L, R));
    return res;
}

signed main(){
    n = rd(), m = rd();
    for(int i = 1; i <= n; ++i)a[i] = rd();
    build(1, 1, n);
    for(int i = 1; i <= m; ++i){
        int op = rd(), l = rd(), r = rd(); ll y;
        if(op ^ 3){
            y = rd();
            mdfy(1, 1, n, l, r, op, y);
        }
        else printf("%lld\n", qry(1, 1, n, l, r));
    }
    return 0;
}

by max0810 @ 2023-12-04 10:48:13

if(op ^ 1){
  pdtg1(x);
  c[x] += y; tg2[x] += y;
}

这里不能pushdown,因为可能只有一个点了还用ls和rs就会越界(虽然好像已经调出来了)

变量名好评


|