Please debug the sample
查看原帖
Please debug the sample
637788
kimi0705楼主2024/7/4 23:30

code of seg

#include <bits/stdc++.h>
#include <climits>
#define fi first
#define se second
#define multi false
#define gc getchar
#define pc putchar
#define pb push_back
#define ls (o * 2)
#define rs (o * 2 + 1)
#define int long long
#define pii pair <int, int>
#define debug(x) cout << #x << " " << x << '\n'
#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rof(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return a * b / gcd(a, b);}
int read (char ch = ' ', int x = 0, int w = 1) { while (!isdigit(ch)) { if (ch == '-') w = -1; ch = gc(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'), ch = gc(); } return x * w; }
void read(int &x) { x = read(); }
void read(int n, int a[]) {For (i, 1, n) a[i] = read();}
void read(int n, int a[], int b[]) {For (i, 1, n) a[i] = read(), b[i] = read();}
void write(int x) { if (x < 0) x = -x, pc('-'); if (x > 9) write(x / 10); pc(x % 10 + '0'); }
void write(int n, int a[]) {For (i, 1, n) write(a[i]), pc(' '); }
/*=============================================================================================================================================================================================*/
const int N = 1e5 + 10;
const int M = N << 2;
const int MIN = -1e15;
int t = 1, n, m, q, l, r, deta;
int ans[N];
vector <pii> vec[N]; // first -> id; second -> deta
struct que {
    int l, r, id;
};
vector <que> Q[N];
struct seg{
    struct node{
        int Left = 0, Right = 0, Max = 0, Sum = 0;
    } Tree[M];
    node Add (node x, node y) {
        node ans = {MIN, MIN, MIN, MIN};
        ans.Left = max(x.Left, x.Sum + y.Left);
        ans.Right = max(x.Right + y.Sum, y.Right);
        ans.Sum = x.Sum + y.Sum;
        ans.Max = max(max(ans.Left, ans.Right), x.Right + y.Left);
        return ans;
    }
    void pushup(int o) {
        Tree[o] = Add(Tree[ls], Tree[rs]);
        return;
    }
    void add(int o, int l, int r, int x, int deta) {
        if (l <= x && x <= r);
        else return;
        if (l == r) {
            Tree[o].Left = Tree[o].Right = Tree[o].Max = Tree[o].Sum = Tree[o].Sum + deta;
            return;
        }
        int mid = (l + r) >> 1;
        add(ls, l, mid, x, deta);
        add(rs, mid + 1, r, x, deta);
        pushup(o);
    }
    node query(int o, int l, int r, int L, int R) { // [L, R] query 
        if ((l <= L && L <= r) || (l <= R && R <= r));
        else return {MIN, MIN, MIN, MIN};
        if (l == r) return {Tree[o].Left, Tree[o].Left, Tree[o].Left, Tree[o].Left};
        int mid = (l + r) >> 1;
        return Add(query(ls, l, mid, L, R), query(rs, mid + 1, r, L, R));
    }
} Tree;
void solve() {
    read(n), read(m);
    For (i, 1, m) {
        read(l), read(r), read(deta);
        vec[l].push_back({i, deta});
        vec[r + 1].push_back({i, -deta});
    }
    read(q);
    For (i, 1, q) Q[read()].pb({read(), read(), i});
    For (i, 1, n) {
        for (auto [id, val] : vec[i]) Tree.add(1, 1, m, id, val);
        for (auto [l, r, id] : Q[i]) ans[id] = Tree.query(1, 1, m, l, r).Max;
    }
    For (i, 1, q) write(ans[i]), pc(10);
    return;
}
signed main() { if (multi) read(t); while (t--) solve(); return 0; }

2024/7/4 23:30
加载中...