板子们

· · 个人记录

代码模板

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lcb(x) ((x) * size - size + 1) 
#define rcb(x) ((x) * size) 
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll                
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1) 
#define LEN (tree[i].r - tree[i].l + 1)  
#define int long long
const int N = 1e6 + 7;
template<typename T>inline void read(T &x){
    bool f=1;x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=!f;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=(f?x:-x);return;
}

void solve() {

}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

快读 update on 2024.3.22

数据结构

线段树

区间修改 + 区间查询

Problem: P3372 【模板】线段树 1

#include <bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define REP(i, a, b) for (int i = a; i < b; i++) 
#define PER(i, a, b) for (int i = a; i > b; i--)
#define lc(x) x << 1 
#define rc(x) x << 1 | 1 
#define int long long
const int N = 5e5 + 7;
struct Tree {
    int l, r;
    int sum, lazy; 
}tree[N << 2];
int n, m, a[N];
void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum;
}
void push_down(int i) {
    if(tree[i].lazy) {
        int k = tree[i].lazy; tree[i].lazy = 0;
        tree[lc(i)].sum += (tree[lc(i)].r - tree[lc(i)].l + 1) * k;
        tree[rc(i)].sum += (tree[rc(i)].r - tree[rc(i)].l + 1) * k;
        tree[lc(i)].lazy += k, tree[rc(i)].lazy += k;
    }
}
void build(int i, int L, int R) {
    tree[i].l = L, tree[i].r = R;
    if(L == R) {
        tree[i].sum = a[L];
        return;
    }
    int mid = (L + R) >> 1;
    build(lc(i), L, mid), build(rc(i), mid + 1, R);
    push_up(i);
}
void modify(int i, int L, int R, int k) {
    if(L <= tree[i].l && tree[i].r <= R) {
        tree[i].sum += (tree[i].r - tree[i].l + 1) * k;
        tree[i].lazy += k;
        return;
    }
    push_down(i);
    if(tree[lc(i)].r >= L) modify(i << 1, L, R, k);
    if(tree[rc(i)].l <= R) modify(i << 1 | 1, L, R, k);
    push_up(i);
}
int query(int i, int L, int R) {
    if(L <= tree[i].l && tree[i].r <= R) return tree[i].sum;
    push_down(i);
    int res = 0;
    if(tree[lc(i)].r >= L) res += query(i << 1, L, R);
    if(tree[rc(i)].l <= R) res += query(i << 1 | 1, L, R);
    return res; 
}
void solve() {
    std::cin >> n >> m;
    for(int i = 1; i <= n; i++) std::cin >> a[i];
    build(1, 1, n);
    while(m--) {
        int opt; std::cin >> opt; 
        if(opt == 1) {
            int x, y, k; std::cin >> x >> y >> k;
            modify(1, x, y, k);
        }
        else {
            int x, y; std::cin >> x >> y;
            std::cout << query(1, x, y) << '\n';
        }
    }
}

signed main() {
    IOS;
    solve(); 
    return 0; 
}

区间乘法

Problem: P3373 【模板】线段树 2

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)  
#define lc(x) x << 1 
#define rc(x) x << 1 | 1
#define int long long 
using ll = long long; 
const int N = 1e5 + 7; 
int n, q, m; 
struct Tree {
    int l, r; 
    int sum, add, mul; 
}tree[N << 2];
int a[N]; 
void push_up(int i) { 
    tree[i].sum = (tree[lc(i)].sum + tree[rc(i)].sum) % m;  
}

void push_down(int i) {
    tree[lc(i)].sum =  (tree[lc(i)].sum * tree[i].mul + tree[i].add * (tree[lc(i)].r - tree[lc(i)].l + 1)) % m; 
    tree[rc(i)].sum =  (tree[rc(i)].sum * tree[i].mul + tree[i].add * (tree[rc(i)].r - tree[rc(i)].l + 1)) % m; 

    tree[lc(i)].mul = (tree[lc(i)].mul * tree[i].mul) % m;
    tree[rc(i)].mul = (tree[rc(i)].mul * tree[i].mul) % m;

    tree[lc(i)].add = (tree[lc(i)].add * tree[i].mul + tree[i].add) % m;
    tree[rc(i)].add = (tree[rc(i)].add * tree[i].mul + tree[i].add) % m;

    tree[i].add = 0; 
    tree[i].mul = 1; 
}

void build(int i, int L, int R) {
    tree[i].l = L, tree[i].r = R;  
    tree[i].mul = 1; 
    if(L == R) {
        tree[i].sum = a[L] % m ; return;  
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R);
    push_up(i);   
}

void modify1(int i, int L, int R, int k) {
    if (L <= tree[i].l && tree[i].r <= R) {
        tree[i].add = (tree[i].add * k) % m;
        tree[i].mul = (tree[i].mul * k) % m;
        tree[i].sum = (tree[i].sum * k) % m;
        return;
    }
    push_down(i);
    if(tree[lc(i)].r >= L) modify1(i << 1, L, R, k);
    if(tree[rc(i)].l <= R) modify1(i << 1 | 1, L, R, k);
    push_up(i);
}

void modify2(int i, int L, int R, int k) {
    if (L <= tree[i].l && tree[i].r <= R) { 
        tree[i].sum = (tree[i].sum + (tree[i].r - tree[i].l + 1) * k) % m;
        tree[i].add = (tree[i].add + k) % m;
        return;     
    }
    push_down(i); 
    if(tree[lc(i)].r >= L) modify2(i << 1, L, R, k); 
    if(tree[rc(i)].l <= R) modify2(i << 1 | 1, L, R, k); 
    push_up(i); 
}

int query(int i, int L, int R) {
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i].sum; 
    }
    push_down(i); 
    int res = 0; 
    if(tree[lc(i)].r >= L) res = (res + query(i << 1, L, R)) % m; 
    if(tree[rc(i)].l <= R) res = (res + query(i << 1 | 1, L, R)) % m; 
    return res;  
}
void solve() {
    std::cin >> n >> q >> m; 
    rep(i, 1, n) {
        std::cin >> a[i];  
    }
    build(1, 1, n); 
    while(q--) {
        int opt, x, y, k; 
        std::cin >> opt >> x >> y ; 
        if(opt == 1) {
            std::cin >> k; 
            modify1(1, x, y, k); 
        }

        if(opt == 2) {
            std::cin >> k; 
            modify2(1, x, y, k);  
        }

        if(opt == 3) {
            std::cout << query(1, x, y) << std::endl; 
        }
    }
}

signed main() {
    IOS;
    solve(); 
    return 0; 
}

区间最大子段和

Problem: SP1716 GSS3 - Can you answer these queries III

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll 
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1) 
#define LEN (tree[i].r - tree[i].l + 1)  
#define int long long
const int N = 1e6 + 7; 
struct Tree {
    int l, r; 
    int lsum, rsum;
    int msum, sum; 
}tree[N << 2]; 
int n, m; 
int a[N]; 
void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum;    
    tree[i].lsum = std::max(tree[lc(i)].lsum, tree[rc(i)].lsum + tree[lc(i)].sum); 
    tree[i].rsum = std::max(tree[rc(i)].rsum, tree[lc(i)].rsum + tree[rc(i)].sum); 
    tree[i].msum = std::max(std::max(tree[lc(i)].msum, tree[rc(i)].msum), tree[lc(i)].rsum + tree[rc(i)].lsum);
}

void build(int i, int L, int R) {
    tree[i].l = L, tree[i].r = R; 
    if(L == R) {
        tree[i].sum = tree[i].lsum = tree[i].rsum = tree[i].msum = a[L]; 
        return ; 
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R); 
    push_up(i); 
}

void modify(int i, int pos, int k) {
    if(tree[i].l == tree[i].r) {
        tree[i].sum = tree[i].lsum = tree[i].rsum = tree[i].msum = k; 
        return ; 
    }
    if(tree[lc(i)].r >= pos) modify(lc(i), pos, k); 
    else modify(rc(i), pos, k);
    push_up(i);
}

Tree merge(Tree a, Tree b) {
    Tree c;
    c.sum = a.sum + b.sum; 
    c.lsum = std::max(a.lsum, b.lsum + a.sum); 
    c.rsum = std::max(a.rsum, a.rsum + b.sum); 
    c.msum = std::max(std::max(a.msum, b.msum), b.lsum + a.rsum); 
    return c; 
}

Tree query(int i, int L, int R) {
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i]; 
    }
    Tree a, b, c; 
    if (R < tree[rc(i)].l) return query(lc(i), L, R);
    if(L > tree[lc(i)].r) return query(rc(i), L, R);
    a = query(lc(i), L, R); 
    b = query(rc(i), L, R); 
    c.sum = a.sum + b.sum; 
    c.lsum = std::max(a.lsum, b.lsum + a.sum); 
    c.rsum = std::max(b.rsum, a.rsum + b.sum); 
    c.msum = std::max(std::max(a.msum, b.msum), b.lsum + a.rsum); 
    return c; 
}

void solve() {
    std::cin >> n; 
    rep(i, 1, n) {
        std::cin >> a[i]; 
    }
    build(1, 1, n); 
    std::cin >> m;
    while(m--) {
        int opt, x, y; 
        std::cin >> opt >> x >> y; 
        if(opt == 0) {
            modify(1, x, y); 
        }
        if(opt == 1) {
            Tree ans = query(1, x, y);
            std::cout << ans.msum << std::endl; 
        }
    }
}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

区间异或

Problem: P3870 [TJOI2009] 开关

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1
#define rc(x) x << 1 | 1
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1)  
#define LEN (tree[i].r - tree[i].l + 1)

using ll = long long; 
const int N = 5e5 + 7; 
int n, m; 
struct Tree {
    int l, r; 
    int sum, lazy; 
}tree[N << 2];
int a[N]; 
void push_down(int i){

    if(tree[i].lazy) {
        tree[lc(i)].sum = LEN_LC - tree[lc(i)].sum; 
        tree[rc(i)].sum = LEN_RC - tree[rc(i)].sum; 
        tree[lc(i)].lazy ^= 1; 
        tree[rc(i)].lazy ^= 1; 
        tree[i].lazy = 0; 
    }
}
void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum; 
}
void build(int i, int L, int R) {
    tree[i].l = L, tree[i].r = R; 
    if(L == R) {
        tree[i].sum = a[L]; 
        return ;  
    }
    int mid = L + R >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R); 
    push_up(i); 
} 

void modify(int i, int L, int R){
    if(L <= tree[i].l && tree[i].r <= R) {
        tree[i].sum = LEN - tree[i].sum; 
        tree[i].lazy ^= 1; 
        return ;
    }
    push_down(i); 
    if(tree[lc(i)].r >= L) modify(lc(i), L, R); 
    if(tree[rc(i)].l <= R) modify(rc(i), L, R); 
    push_up(i); 
} 

int query(int i, int L, int R) {
    if(L <= tree[i].l && tree[i].r <= R) { 
        return tree[i].sum; 
    }
    push_down(i); 
    int res = 0; 
    if(tree[lc(i)].r >= L) res += query(lc(i), L, R); 
    if(tree[rc(i)].l <= R) res += query(rc(i), L, R); 
    return res; 
}

void solve() {
    std::cin >> n >> m; 
    build(1, 1, n); 
    while(m--) {
        int opt, l, r; 
        std::cin >> opt >> l >> r; 
        if(opt == 0) modify(1, l, r); 
        else std::cout << query(1, l, r) << std::endl; 
    }
}

int main() {
    IOS; 
    solve(); 
    return 0; 
}

势能线段树 - 区间开根号

Problem: P4145 上帝造题的七分钟 2 / 花神游历各国

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1 
#define rc(x) x << 1 | 1 
#define int long long
using ll = long long; 
const int N = 1e5 + 7; 
struct Tree{
    int l, r; 
    int Max, sum;  
}tree[N << 2];
int a[N]; 
int n, m; 
void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum; 
    tree[i].Max = std::max(tree[lc(i)].Max, tree[rc(i)].Max); 
} 
void build(int i, int L, int R) { 
    tree[i].l = L, tree[i].r = R;
    if(L == R) {
        tree[i].sum = tree[i].Max = a[L]; 
        return ;  
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid), build(rc(i), mid + 1, R); 
    push_up(i); 
}

void modify(int i, int L, int R) {
    if(tree[i].Max == 1) {
        return ; 
    }

    if(tree[i].l == tree[i].r) {
        tree[i].sum = tree[i].Max = sqrt(tree[i].sum);
        return ;
    }

    if(tree[lc(i)].r >= L) modify(lc(i), L, R); 
    if(tree[rc(i)].l <= R) modify(rc(i), L, R); 
    push_up(i);
}

int query(int i, int L, int R) { 
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i].sum; 
    } 

    int res = 0; 

    if(tree[lc(i)].r >= L) res += query(lc(i), L, R);
    if(tree[rc(i)].l <= R) res += query(rc(i), L, R); 

    return res;  
}
void solve(){
    std::cin >> n; 
    rep(i, 1, n) {
        std::cin >> a[i]; 
    }
    build(1, 1, n); 
    std::cin >> m; 
    while(m--) {
        int opt, x, y; std::cin >> opt >> x >> y; 
        if(x > y) std::swap(x, y); 
        if(opt == 0) {
            modify(1, x, y); 
        }
        if(opt == 1) {
            std::cout << query(1, x, y) << std::endl; 
        }
    }
}  

signed main(){
    IOS; 
    solve(); 
    return 0; 
}

势能线段树 - 区间取模

Problem: CF438D The Child and Sequence

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1 
#define rc(x) x << 1 | 1 
#define int long long 
using ll = long long;
const int N = 1e5 + 7; 
int n, m;
struct Tree {
    int l, r; 
    int Max, sum; 
}tree[N << 2]; 
int a[N]; 
void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum; 
    tree[i].Max = std::max(tree[lc(i)].Max, tree[rc(i)].Max); 
}

void build(int i, int L, int R) {
    tree[i].l = L; tree[i].r = R; 
    if(L == R) {
        tree[i].sum = tree[i].Max = a[L]; 
        return ; 
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R); 
    push_up(i); 
}

void modify1(int i, int L, int R, int k){
    if(tree[i].Max < k) {
         return ; 
    }

    if(tree[i].l == tree[i].r) {
        tree[i].sum %= k; 
        tree[i].Max %= k; 
        return; 
    }

    if(tree[lc(i)].r >= L) modify1(lc(i), L, R, k); 
    if(tree[rc(i)].l <= R) modify1(rc(i), L, R, k); 
    push_up(i); 
}

void modify2(int i, int pos, int k) {
    if(tree[i].l == tree[i].r) {
        tree[i].sum = tree[i].Max = k; 
        return ; 
    }

    if(tree[lc(i)].r >= pos)  modify2(lc(i), pos, k);
    else modify2(rc(i), pos, k); 
    push_up(i); 
}

int query(int i, int L, int R) {
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i].sum; 
    }
    int res = 0; 
    if(tree[lc(i)].r >= L) res += query(lc(i), L, R); 
    if(tree[rc(i)].l <= R) res += query(rc(i), L, R); 
    return res; 
}

void solve() {
    std::cin >> n >> m; 
    rep(i, 1, n)  std::cin >> a[i];  
    build(1, 1, n); 
    while(m--) {
        int opt; 
        std::cin >> opt; 
        if(opt == 1) {
            int l, r; std::cin >> l >> r; 
            std::cout << query(1, l, r) << std::endl; 
        }

        if(opt == 2) {
            int l, r, x; std::cin >> l >> r >> x;
            modify1(1, l, r, x); 
        }

        if(opt == 3) {
            int k, x; 
            std::cin >> k >> x;
            modify2(1, k, x);
        }
    }
}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

势能线段树 - 区间正约数个数

Problem: CF920F SUM and REPLACE

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1 
#define rc(x) x << 1 | 1 
#define int long long
using ll = long long; 
const int N = 3e5 + 7; 
const int M = 1e6; 
struct Tree{
    int l, r; 
    int Max, sum;  
}tree[N << 2];
int a[N]; 
int n, m; 
int d[M + 7];  
bool vis[M + 7]; 
int primes[M];
int num[M + 7];  
int cnt; 
void init() {
    d[1] = 1; 
    vis[1] = true; 
    for (int i = 2; i <= M; i++) {
        if(!vis[i]) {
            primes[++cnt] = i; 
            num[i] = 1; 
            d[i] = 2; 
        }

        for(int j = 1; j <= cnt && i * primes[j] <= M; j++){ 
            vis[i * primes[j]] = 1; 
            if(i % primes[j]) {
                d[i * primes[j]] = d[i] * 2; 
                num[i * primes[j]] = 1; 
            } else {
                d[i * primes[j]] = d[i] / (num[i] + 1) * (num[i] + 2); 
                num[i * primes[j]] = num[i] + 1; 
                break; 
            }
        }
    }
}

void push_up(int i) {
    tree[i].sum = tree[lc(i)].sum + tree[rc(i)].sum; 
    tree[i].Max = std::max(tree[lc(i)].Max, tree[rc(i)].Max); 
} 
void build(int i, int L, int R) { 
    tree[i].l = L, tree[i].r = R;
    if(L == R) {
        tree[i].sum = tree[i].Max = a[L]; 
        return ;  
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid), build(rc(i), mid + 1, R); 
    push_up(i); 
}

void modify(int i, int L, int R) {
    if(tree[i].Max == 1 || tree[i].Max == 2)  {
        return ; 
    }

    if(tree[i].l == tree[i].r) {
        tree[i].sum = tree[i].Max = d[tree[i].Max];
        return ;
    }

    if(tree[lc(i)].r >= L) modify(lc(i), L, R); 
    if(tree[rc(i)].l <= R) modify(rc(i), L, R); 
    push_up(i);
}

int query(int i, int L, int R) { 
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i].sum; 
    } 

    int res = 0; 

    if(tree[lc(i)].r >= L) res += query(lc(i), L, R);
    if(tree[rc(i)].l <= R) res += query(rc(i), L, R); 

    return res;  
}
void solve(){
    std::cin >> n >> m ; 
    rep(i, 1, n) {
        std::cin >> a[i]; 
    }
    init(); 
    build(1, 1, n); 
    while(m--) {
        int opt, l, r; std::cin >> opt; 
        if(opt == 1) {
            std::cin >> l >> r; 
            modify(1, l, r); 
        }
        if(opt == 2) {
            std::cin >> l >> r;
            std::cout << query(1, l, r) << std::endl; 
        }
    }
}  

signed main(){
    IOS; 
    solve(); 
    return 0; 
}

子串个数

Problem: P6373 「StOI-1」IOI计数

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--) 
#define lc(x) x << 1
#define rc(x) x << 1 | 1 
#define int long long 
const int N = 5e5 + 7;  
struct Tree {
    int I, O; 
    int IO, OI; 
    int IOI;
    int l, r;    
    Tree(){
        I = O = IO = OI = IOI = 0; 
    }
}tree[N << 2];
int n, m;  
int a[N]; 
void push_up(int i) {
    tree[i].I = tree[lc(i)].I + tree[rc(i)].I; 
    tree[i].O = tree[lc(i)].O + tree[rc(i)].O; 
    tree[i].IO = tree[lc(i)].IO + tree[rc(i)].IO + (tree[lc(i)].I * tree[rc(i)].O); 
    tree[i].OI = tree[lc(i)].OI + tree[rc(i)].OI + (tree[lc(i)].O * tree[rc(i)].I); 
    tree[i].IOI =  tree[lc(i)].IOI + tree[rc(i)].IOI + (tree[lc(i)].IO * tree[rc(i)].I) 
    + (tree[lc(i)].I * tree[rc(i)].OI);
} 

void build(int i, int L, int R){
    tree[i].l = L, tree[i].r = R; 
    if(L == R) {
        if(a[L] == 1) tree[i].I = 1; 
        else tree[i].O = 1; 
        return ;
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R); 
    push_up(i); 
}

Tree merge(Tree a, Tree b) {
    Tree z; 
    z.I = a.I + b.I;  
    z.O = a.O + b.O; 
    z.IO = a.IO + b.IO + (a.I * b.O); 
    z.OI = a.OI + b.OI + (a.O * b.I); 
    z.IOI = a.IOI + b.IOI + (a.IO * b.I) + (a.I * b.OI); 
    return z; 
}

void modify(int i, int x, int k) {
    if(tree[i].l == tree[i].r) {
         if(k == 1) { 
            tree[i].I = 1; 
            tree[i].O = 0; 
         }
         else {
            tree[i].O = 1;  
            tree[i].I = 0; 
         }

         return ;
    }

    if(tree[lc(i)].r >= x) modify(lc(i), x, k); 
    else modify(rc(i), x, k); 
    push_up(i); 
}


Tree query(int i, int L, int R){
    if(L <= tree[i].l && tree[i].r <= R) {
        return tree[i]; 
    }
    Tree ans; 
    if(tree[lc(i)].r >= L) ans = merge(ans, query(lc(i), L, R)); 
    if(tree[rc(i)].l <= R) ans = merge(ans, query(rc(i), L, R)); 
    return ans; 
}

void solve() { 
    std::cin >> n >> m; 
    rep(i, 1, n) {
        char ch; std::cin >> ch;
        if(ch == 'I') {
            a[i] = 1; 
        } else { 
            a[i] = 0; 
        }
    } 
    build(1, 1, n);
    while(m--){
        int opt; 
        std::cin >> opt; 
        if(opt == 1) {
             int x, k; 
             char ch; 
             std::cin >> x >> ch; 
             if(ch == 'I') {
                 k = 1; 
             } else {
                 k = 0; 
             }
             modify(1, x, k); 
        }

        if(opt == 2) {
            int l, r; 
            std::cin >> l >> r; 
            std::cout << query(1, l, r).IOI << std::endl; 
        }
    }
}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

KTT

Problem: P5693 EI 的第六分块

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll 
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1) 
#define LEN (tree[i].r - tree[i].l + 1)  
#define int long long
const int N = 4e5 + 7; 
const int inf = LONG_LONG_MAX; 
template<typename T>inline void read(T &x){
    bool f=1;x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=!f;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=(f?x:-x);return;
}
int A[N];
int n, q; 
struct F{
    int k, b; 
    friend F operator+(const F &a, const F &b) {
        return (F){a.k + b.k, a.b + b.b}; 
    }
    void fun(int v) {
        b += k * v; 
    }
};

std::pair<F, int> max(F a, F b) {
    if(a.k < b.k || a.k == b.k && a.b < b.b) {
        std::swap(a, b); 
    }
    if(a.b >= b.b) {
        return std::make_pair(a, inf);
    }
    return std::make_pair(b, (b.b - a.b) / (a.k - b.k)); 
}

struct Tree {
    F lsum, rsum, sum, msum; 
    int val;
    int lazy; 
    friend Tree operator+ (Tree a, Tree b) {
        Tree c; 
        std::pair<F, int> pi;
        c.val = std::min(a.val, b.val);
        pi = max(a.lsum, b.lsum + a.sum); 
        c.lsum = pi.first, c.val = std::min(c.val, pi.second);
        pi = max(b.rsum, a.rsum + b.sum);
        c.rsum = pi.first, c.val = std::min(c.val, pi.second);
        pi = max(a.msum, b.msum); c.val = std::min(c.val, pi.second); 
        pi = max(pi.first, a.rsum + b.lsum); 
        c.msum = pi.first; c.val = std::min(c.val, pi.second);
        c.sum = a.sum + b.sum;
        return c; 
    }
}tree[N << 2];

void push_up(int i) {
    tree[i] = tree[lc(i)] + tree[rc(i)]; 
}
void add_tag(int i, int k) {
    tree[i].lazy += k;
    tree[i].val -= k;
    tree[i].lsum.fun(k);
    tree[i].rsum.fun(k); 
    tree[i].sum.fun(k);
    tree[i].msum.fun(k);
}

void push_down(int i) {
    if(tree[i].lazy) {
        int k = tree[i].lazy;
        add_tag(lc(i), k); 
        add_tag(rc(i), k);  
        tree[i].lazy = 0; 
    }
}
void build(int i, int L, int R) {
    if(L == R) {
        tree[i].lsum = tree[i].rsum = tree[i].sum = tree[i].msum = {1, A[L]};
        tree[i].val = inf; 
        return ; 
    }
    int mid = (L + R) >> 1; 
    build(lc(i), L, mid); 
    build(rc(i), mid + 1, R); 
    push_up(i);
}

void dfs(int i, int l, int r, int k) {
    if(k > tree[i].val) {
        int mid = (l + r) >> 1; 
        int now = tree[i].lazy + k;
        tree[i].lazy = 0; 
        dfs(lc(i), l, mid, now);
        dfs(rc(i), mid + 1, r, now); 
        push_up(i); 
    } else {
        add_tag(i, k); 
    }
}
void modify(int i, int l, int r, int L, int R, int k) {
    if(L <= l && r <= R) {
        dfs(i, l, r, k);
        return ;
    }
    push_down(i);
    int mid = (l + r) >> 1; 
    if(L <= mid) modify(lc(i), l, mid, L, R, k); 
    if(R > mid) modify(rc(i), mid + 1, r, L, R, k); 
    push_up(i); 
}

Tree query(int i, int l, int r, int L, int R) {
    if(L <= l && r <= R) {
        return tree[i]; 
    }
    push_down(i); 
    int mid = (l + r) >> 1; 
    if(R <= mid) return query(lc(i), l, mid, L, R);
    if(L > mid) return query(rc(i), mid + 1, r, L, R);
    return query(lc(i), l, mid, L, mid) + query(rc(i), mid + 1, r, mid + 1, R); 
}
void solve() {
    read(n), read(q);
    rep(i, 1, n) {
        read(A[i]); 
    }
    build(1, 1, n); 
    while(q--) {
        int opt; read(opt);
        if(opt == 1) {
            int l, r, k; read(l), read(r), read(k); 
            modify(1, 1, n, l, r, k); 
        }
        if(opt == 2) {
            int l, r; 
            read(l), read(r); 
            std::cout << std::max(0ll,query(1, 1, n, l, r).msum.b) << std::endl; 
        }
    }
}

signed main() {
    IOS; 
    solve(); 
    return 0; 
}

分块

区间修改 + 区间查询

Problem: P3372 【模板】线段树 1

#include <bits/stdc++.h> 
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr) 
#define rep(i, a, b) for (int i = a; i <= b; i++) 
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lc(x) x << 1ll 
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1) 
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1) 
#define LEN (tree[i].r - tree[i].l + 1)  
#define int long long
const int N = 1e5 + 7;  
template<typename T>inline void read(T &x){
    bool f=1;x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=!f;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=(f?x:-x);return;
}
int st[N], ed[N]; 
int pos[N];
int a[N], sum[N], add[N]; 
void modify(int l, int r, int k) {
    int p = pos[l], q = pos[r];
    if(p == q) {
        rep(i, l, r) {
            a[i] += k; 
        }
        sum[p] += k * (r - l + 1); 
    } else {
        rep(i, p + 1, q - 1) add[i] += k; 
        rep(i, l, ed[p]) a[i] += k; 
        sum[p] += k * (ed[p] - l + 1); 
        rep(i, st[q], r) a[i] += k; 
        sum[q] += k * (r - st[q] + 1);

    }
}

int query(int l, int r) {
    int p = pos[l], q = pos[r]; 
    int res = 0; 
    if(p == q){
        rep(i, l, r) res += a[i]; 
        res += add[p] * (r - l + 1);
    } else {
        rep(i, p + 1, q - 1) res += sum[i] + add[i] * (ed[i] - st[i] + 1); 
        rep(i, l, ed[p]) res += a[i]; 
        res += add[p] * (ed[p] - l + 1); 
        rep(i, st[q], r) res += a[i]; 
        res += add[q] * (r - st[q] + 1); 
    }
    return res; 
}
void solve() {
    int n, m; 
    read(n), read(m);
    rep(i, 1, n) read(a[i]); 
    int block = (int)sqrt((double)n); 
    int t = n / block; 
    if(n % block) t++; 
    rep(i, 1, t) {
        st[i] = (i - 1) * block + 1; 
        ed[i] = i * block; 
    }
    ed[t] = n; 
    rep(i, 1, n) {
        pos[i] = (i - 1) / block + 1; 
    }

    rep(i, 1, t) {
        rep(j, st[i], ed[i]) {
            sum[i] += a[j];
        }
    }

    while(m--) {
        int opt; read(opt); 
        if(opt == 1) {
            int l, r, k; read(l), read(r), read(k); 
            modify(l, r, k); 
        }
        if(opt == 2) {
            int l, r; read(l), read(r); 
            std::cout << query(l, r) << std::endl; 
        }
    }
} 

signed main() {
    IOS; 
    solve(); 
    return 0; 
}