比赛 THUPC 2025 pre 评测结果 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRR
题目名称 waht 先生的法阵 最终得分 0
用户昵称 郑霁桓 运行时间 4.278 s
代码语言 C++ 内存使用 3.31 MiB
提交时间 2026-01-29 19:01:38
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;

struct tr {
    int ax,an,bx,bn,sax,san,lz;
    long long p1,p2,p3,p4,as;
}tree[4000005];

int n, a[500005], m, op, xx, yy, zz;
const int I = 1000000000;

inline void pu(int p) {
    if (p > 1e6) return;
    int l = p * 2;
    if (tree[l].ax == tree[l + 1].ax) {
        tree[p].ax = tree[l].ax;
        tree[p].sax = tree[l].sax + tree[l + 1].sax;
        tree[p].bx = max(tree[l].bx, tree[l + 1].bx);
    } else if (tree[l].ax < tree[l + 1].ax) {
        tree[p].ax = tree[l + 1].ax;
        tree[p].sax = tree[l + 1].sax;
        tree[p].bx = max(tree[l + 1].bx, tree[l].ax);
    } else {
        tree[p].ax = tree[l].ax;
        tree[p].sax = tree[l].sax;
        tree[p].bx = max(tree[l].bx, tree[l + 1].ax);
    }
    
    if (tree[l].an == tree[l + 1].an) {
        tree[p].an = tree[l].an;
        tree[p].san = tree[l].san + tree[l + 1].san;
        tree[p].bn = min(tree[l].bn, tree[l + 1].bn);
    } else if (tree[l].an > tree[l + 1].an) {
        tree[p].an = tree[l + 1].an;
        tree[p].san = tree[l + 1].san;
        tree[p].bn = min(tree[l + 1].bn, tree[l].an);
    } else {
        tree[p].an = tree[l].an;
        tree[p].san = tree[l].san;
        tree[p].bn = min(tree[l].bn, tree[l + 1].an);
    }
    tree[p].as = tree[l].as + tree[l + 1].as;
    return;
}

inline void build(int l, int r, int p) {
    tree[p].lz = tree[p].p1 = tree[p].p2 = tree[p].p3 = tree[p].p4 = 0;
    
    if (l == r) {
        tree[p].ax = tree[p].an = tree[p].as = a[l];
        tree[p].bx = -I;
        tree[p].bn = I;
        tree[p].sax = tree[p].san = 1;
        return;
    }
    int md = (l + r) >> 1;
    build(l, md, p * 2);
    build(md + 1, r, p * 2 + 1);
    pu(p);
    return;
}

inline void pd(int p, int l, int md, int r) {
    if (p > 1e6) return;
    const int pl = p * 2;
    const int pp1 = tree[pl].ax, pp2 = tree[pl + 1].ax;
    long long t1 = tree[pl].p1, t2 = tree[pl].p2;
    long long t3 = tree[pl + 1].p1, t4 = tree[pl + 1].p2;
    
    if (tree[p].p1) {
        tree[pl].as += tree[p].p1 * tree[pl].san;
        if (tree[pl].ax == tree[pl].an) tree[pl].ax += tree[p].p1;
        if (tree[pl].bx == tree[pl].an) tree[pl].bx += tree[p].p1;
        if (tree[pl * 2].an + tree[pl].lz + tree[pl].p1 + (tree[pl * 2].an == tree[pl * 2].ax ? tree[pl].p3 : 0) == tree[pl].an)
            tree[pl].p1 += tree[p].p1;
        if (tree[pl * 2 + 1].an + tree[pl].lz + tree[pl].p2 + (tree[pl * 2 + 1].an == tree[pl * 2 + 1].ax ? tree[pl].p4 : 0) == tree[pl].an)
            tree[pl].p2 += tree[p].p1;
        tree[pl].an += tree[p].p1;
    }
    
    if (tree[p].p2) {
        tree[pl + 1].as += tree[p].p2 * tree[pl + 1].san;
        if (tree[pl + 1].ax == tree[pl + 1].an) tree[pl + 1].ax += tree[p].p2;
        if (tree[pl + 1].bx == tree[pl + 1].an) tree[pl + 1].bx += tree[p].p2;
        if (tree[pl * 2 + 2].an + tree[pl + 1].lz + tree[pl + 1].p1 + (tree[pl * 2 + 2].an == tree[pl * 2 + 2].ax ? tree[pl + 1].p3 : 0) == tree[pl + 1].an)
            tree[pl + 1].p1 += tree[p].p2;
        if (tree[pl * 2 + 3].an + tree[pl + 1].lz + tree[pl + 1].p2 + (tree[pl * 2 + 3].an == tree[pl * 2 + 3].ax ? tree[pl + 1].p4 : 0) == tree[pl + 1].an)
            tree[pl + 1].p2 += tree[p].p2;
        tree[pl + 1].an += tree[p].p2;
    }
    
    if (tree[p].p3) {
        tree[pl].as += tree[p].p3 * tree[pl].sax;
        if (tree[pl].ax == tree[pl].an) tree[pl].an += tree[p].p3;
        if (tree[pl].ax == tree[pl].bn) tree[pl].bn += tree[p].p3;
        if (tree[pl * 2].ax + tree[pl].lz + tree[pl].p3 + (tree[pl * 2].an == tree[pl * 2].ax ? t1 : 0) == pp1)
            tree[pl].p3 += tree[p].p3;
        if (tree[pl * 2 + 1].ax + tree[pl].lz + tree[pl].p4 + (tree[pl * 2 + 1].an == tree[pl * 2 + 1].ax ? t2 : 0) == pp1)
            tree[pl].p4 += tree[p].p3;
        tree[pl].ax += tree[p].p3;
    }
    
    if (tree[p].p4) {
        tree[pl + 1].as += tree[p].p4 * tree[pl + 1].sax;
        if (tree[pl + 1].ax == tree[pl + 1].an) tree[pl + 1].an += tree[p].p4;
        if (tree[pl + 1].ax == tree[pl + 1].bn) tree[pl + 1].bn += tree[p].p4;
        if (tree[pl * 2 + 2].ax + tree[pl + 1].lz + tree[pl + 1].p3 + (tree[pl * 2 + 2].an == tree[pl * 2 + 2].ax ? t3 : 0) == pp2)
            tree[pl + 1].p3 += tree[p].p4;
        if (tree[pl * 2 + 3].ax + tree[pl + 1].lz + tree[pl + 1].p4 + (tree[pl * 2 + 3].an == tree[pl * 2 + 3].ax ? t4 : 0) == pp2)
            tree[pl + 1].p4 += tree[p].p4;
        tree[pl + 1].ax += tree[p].p4;
    }
    
    tree[pl].an += tree[p].lz;
    tree[pl + 1].an += tree[p].lz;
    tree[pl].ax += tree[p].lz;
    tree[pl + 1].ax += tree[p].lz;
    tree[pl].bx += tree[p].lz;
    tree[pl + 1].bx += tree[p].lz;
    tree[pl].bn += tree[p].lz;
    tree[pl + 1].bn += tree[p].lz;
    tree[pl].as += tree[p].lz * (md - l + 1);
    tree[pl + 1].as += tree[p].lz * (r - md);
    tree[pl].lz += tree[p].lz;
    tree[pl + 1].lz += tree[p].lz;
    tree[p].lz = tree[p].p1 = tree[p].p2 = tree[p].p3 = tree[p].p4 = 0;
    return;
}

inline void ad(int x, int y, int l, int r, int p, long long v) {
    int md = (l + r) >> 1;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (x <= l && r <= y) {
        tree[p].ax += v;
        tree[p].an += v;
        tree[p].bx += v;
        tree[p].bn += v;
        tree[p].as += (r - l + 1) * v;
        tree[p].lz += v;
        return;
    }
    if (x <= md) ad(x, y, l, md, p * 2, v);
    if (md + 1 <= y) ad(x, y, md + 1, r, p * 2 + 1, v);
    pu(p);
    return;
}

inline void mx(int x, int y, int l, int r, int p, long long v) {
    int md = (l + r) >> 1;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (v <= tree[p].an) return;
    if (x <= l && r <= y && v < tree[p].bn) {
        tree[p].as += tree[p].san * (v - tree[p].an);
        if (tree[p * 2].an + tree[p].p1 == tree[p].an) tree[p].p1 += (v - tree[p].an);
        if (tree[p * 2 + 1].an == tree[p].an) tree[p].p2 += (v - tree[p].an);
        if (tree[p].ax == tree[p].an) tree[p].ax = v;
        if (tree[p].bx == tree[p].an) tree[p].bx = v;
        tree[p].an = v;
        return;
    }
    if (x <= md) mx(x, y, l, md, p * 2, v);
    if (md + 1 <= y) mx(x, y, md + 1, r, p * 2 + 1, v);
    pu(p);
    return;
}

inline void mn(int x, int y, int l, int r, int p, long long v) {
    int md = (l + r) >> 1;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (v >= tree[p].ax) return;
    if (x <= l && r <= y && v > tree[p].bx) {
        tree[p].as += tree[p].sax * (v - tree[p].ax);
        if (tree[p * 2].ax == tree[p].ax) tree[p].p3 += (v - tree[p].ax);
        if (tree[p].ax == tree[p * 2 + 1].ax) tree[p].p4 += (v - tree[p].ax);
        if (tree[p].ax == tree[p].an) tree[p].an = v;
        if (tree[p].ax == tree[p].bn) tree[p].bn = v;
        tree[p].ax = v;
        return;
    }
    if (x <= md) mn(x, y, l, md, p * 2, v);
    if (md + 1 <= y) mn(x, y, md + 1, r, p * 2 + 1, v);
    pu(p);
    return;
}

inline long long sm(int x, int y, int l, int r, int p) {
    if (x <= l && r <= y) return tree[p].as;
    int md = (l + r) >> 1;
    long long s = 0;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (x <= md) s += sm(x, y, l, md, p * 2);
    if (md + 1 <= y) s += sm(x, y, md + 1, r, p * 2 + 1);
    return s;
}

inline int sx(int x, int y, int l, int r, int p) {
    if (x <= l && r <= y) return tree[p].ax;
    int md = (l + r) >> 1;
    int s = -I;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (x <= md) s = max(s, sx(x, y, l, md, p * 2));
    if (md + 1 <= y) s = max(s, sx(x, y, md + 1, r, p * 2 + 1));
    return s;
}

inline int sn(int x, int y, int l, int r, int p) {
    if (x <= l && r <= y) return tree[p].an;
    int md = (l + r) >> 1;
    int s = I;
    if (tree[p].lz || tree[p].p1 || tree[p].p2 || tree[p].p3 || tree[p].p4) pd(p, l, md, r);
    if (x <= md) s = min(s, sn(x, y, l, md, p * 2));
    if (md + 1 <= y) s = min(s, sn(x, y, md + 1, r, p * 2 + 1));
    return s;
}

inline int read() {
    int xx = 0;
    int f = 1;
    char ch = getchar_unlocked();
    while (ch < '0' || '9' < ch) {
        if (ch == '-') f = -1;
        ch = getchar_unlocked();
    }
    while ('0' <= ch && ch <= '9') xx = (xx << 1) + (xx << 3) + (ch - '0'), ch = getchar_unlocked();
    return f * xx;
}

inline void w(long long x) {
    if (x < 0) putchar_unlocked('-'), x = -x;
    if (x > 9) w(x / 10);
    putchar_unlocked(x % 10 + '0');
    return;
}

int main() {
    n = read();
    for (register int i = 1; i <= n; ++i) a[i] = read();
    build(1, n, 1);
    m = read();
    while (m--) {
        op = read(), xx = read(), yy = read();
        if (op == 1) zz = read(), ad(xx, yy, 1, n, 1, zz);
        if (op == 2) zz = read(), mx(xx, yy, 1, n, 1, zz);
        if (op == 3) zz = read(), mn(xx, yy, 1, n, 1, zz);
        if (op == 4) w(sm(xx, yy, 1, n, 1)), putchar_unlocked('\n');
        if (op == 5) w(sx(xx, yy, 1, n, 1)), putchar_unlocked('\n');
        if (op == 6) w(sn(xx, yy, 1, n, 1)), putchar_unlocked('\n');
    }
    return 0;
}