70分求调

P3372 【模板】线段树 1

Twinkling @ 2023-12-14 20:59:49

#include<bits/stdc++.h>
using namespace std;
#define ing long long 
#define lson (root << 1)//(root * 2)
#define rson (root << 1 | 1)//(root * 2 + 1)
#define N 100010
struct node{
    int lft,rgt,sum,lazy;
}tr[N << 2];//N * 2
int n,m,opt,a[N];
void push_up(int root){
    tr[root].sum = tr[lson].sum + tr[rson].sum;
}
void push_down(int root){
    if(!tr[root].lazy)
        return;
    tr[lson].sum += (tr[lson].rgt - tr[lson].lft + 1) * tr[root].lazy;
    tr[rson].sum += (tr[rson].rgt - tr[rson].lft + 1) * tr[root].lazy;
    tr[lson].lazy += tr[root].lazy;
    tr[rson].lazy += tr[root].lazy;
    tr[root].lazy = 0;
}
void build_tree(int root,int lft,int rgt){
    tr[root].lft = lft;
    tr[root].rgt = rgt;
    if(tr[root].lft == tr[root].rgt){
        tr[root].sum = a[lft];
        return;
    }
    int mid = lft + rgt >> 1;//(lft + rgt) / 2 ????? 
    build_tree(lson,lft,mid);
    build_tree(rson,mid+1,rgt);
    push_up(root);
}
int query(int root,int lft,int rgt){
    if(tr[root].lft >= lft && tr[root].rgt <= rgt)
        return tr[root].sum;
    push_down(root);
    int mid = tr[root].lft + tr[root].rgt >> 1;
    int res = 0;
    if(lft <= mid)
        res += query(lson,lft,rgt);
    if(rgt > mid)
        res += query(rson,lft,rgt);
    return res;
}
void update(int root,int lft,int rgt,int k){
    if(tr[root].lft >= lft && tr[root].rgt <= rgt){
        tr[root].sum += (tr[root].rgt - tr[root].lft + 1) * k;
        tr[root].lazy += k;
        return;
    }
    push_down(root);
    int mid = tr[root].lft + tr[root].rgt >> 1;
    if(lft <= mid)
        update(lson,lft,rgt,k);
    if(rgt > mid)
        update(rson,lft,rgt,k);
    push_up(root);
    return;
}
signed main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    build_tree(1,1,n);
    while(m--){
        int l,r,k;
        cin >> opt;
        if(opt == 1){
            cin >> l >> r >> k;
            update(1,l,r,k);
        }
        else{
            cin >> l >> r;
            cout << query(1,l,r) << endl;
        }
    }
    return 0;
}

by _zuoqingyuan @ 2023-12-14 21:14:33

看看你第三行的代码


by _zhx @ 2023-12-14 21:15:39

@Twinkling 我的查询和修改和你不一样,可能是写法的原因,你看看我的吧:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5e5 + 10;
struct Tree {
    int l, r;
    int sum, lazy; 
}tree[maxn << 2];
int n, m, a[maxn];
void push_up(int i) {
    tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
}
void push_down(int i) {
    if(tree[i].lazy) {
        int k = tree[i].lazy; tree[i].lazy = 0;
        tree[i << 1].sum += (tree[i << 1].r - tree[i << 1].l + 1) * k;
        tree[i << 1 | 1].sum += (tree[i << 1 | 1].r - tree[i << 1 | 1].l + 1) * k;
        tree[i << 1].lazy += k, tree[i << 1 | 1].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(i << 1, L, mid), build(i << 1 | 1, 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[i << 1].r >= L) modify(i << 1, L, R, k);
    if(tree[i << 1 | 1].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 ret = 0;
    if(tree[i << 1].r >= L) ret += query(i << 1, L, R);
    if(tree[i << 1 | 1].l <= R) ret += query(i << 1 | 1, L, R);
    return ret; 
}
signed main() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while(m--) {
        int opt; cin >> opt; 
        if(opt == 1) {
            int x, y, k; cin >> x >> y >> k;
            modify(1, x, y, k);
        }
        else {
            int x, y; cin >> x >> y;
            cout << query(1, x, y) << '\n';
        }
    }
    return 0;
}

by _zhx @ 2023-12-14 21:20:20

@zuoqingyuan 然后呢,为什么有个蒟蒻不知道。


by _zuoqingyuan @ 2023-12-14 21:21:22

#define ing long long 

是不是要写成

#define int long long 

by _zuoqingyuan @ 2023-12-14 21:26:59

@_zhx 我的意思是他的代码没问题,单纯有一个语句打错了


by Twinkling @ 2023-12-14 21:35:42

@zuoqingyuan @_zhx

谢谢!!!!!我查了两个晚上都没查出来,确实疏忽了,感谢帮忙

祝您AK IOI


by Twinkling @ 2023-12-14 21:37:06

此贴结,警示:int不是ing


|