求问,两者有何不同?
查看原帖
求问,两者有何不同?
916276
b9113fced86a32cad0d8楼主2025/2/7 20:16

这是AI写的代码,样例能过:

#include<bits/stdc++.h>
using namespace std;

struct node {
    int l, r;
    int num, tag;
} tree[400005];
int n, m;
int a[100005];

void build(int l, int r, int p) {
    tree[p].l = l;
    tree[p].r = r;
    tree[p].tag = 0;
    if (l == r) {
        tree[p].num = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(l, mid, 2 * p);
    build(mid + 1, r, 2 * p + 1);
    tree[p].num = tree[2 * p].num + tree[2 * p + 1].num;
}

void update(int p) {
    if (tree[p].tag) {
        tree[2 * p].num += tree[p].tag * (tree[2 * p].r - tree[2 * p].l + 1);
        tree[2 * p].tag += tree[p].tag;
        tree[2 * p + 1].num += tree[p].tag * (tree[2 * p + 1].r - tree[2 * p + 1].l + 1);
        tree[2 * p + 1].tag += tree[p].tag;
        tree[p].tag = 0;
    }
}

void add(int l, int r, int p, int x) {
    if (tree[p].l >= l && tree[p].r <= r) {
        tree[p].num += x * (tree[p].r - tree[p].l + 1);
        tree[p].tag += x;
    } else {
        update(p);
        int mid = (tree[p].l + tree[p].r) / 2;
        if (l <= mid)
            add(l, r, 2 * p, x);
        if (r > mid)
            add(l, r, 2 * p + 1, x);
        tree[p].num = tree[2 * p].num + tree[2 * p + 1].num;
    }
}

int getsum(int l, int r, int p) {
    update(p);
    if (tree[p].l > r || tree[p].r < l)
        return 0;
    if (tree[p].l >= l && tree[p].r <= r)
        return tree[p].num;
    return getsum(l, r, 2 * p) + getsum(l, r, 2 * p + 1);
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, n, 1);
    for (int i = 1; i <= m; i++) {
        int t;
        cin >> t;
        if (t == 1) {
            int x, y, k;
            cin >> x >> y >> k;
            add(x, y, 1, k);
        } else if (t == 2) {
            int x, y;
            cin >> x >> y;
            cout << getsum(x, y, 1) << endl;
        }
    }
}

这是我写的代码,只会输出0

#include<bits/stdc++.h>
using namespace std;

struct node{
	long long l,r;
	long long num,tag;
}tree[400005];
long long n,m;
long long a[100005];

void build(long long l,long long r,long long p){
	tree[p].l = l;
	tree[p].r = r;
	tree[p].tag = 0;
	if(l == r){
		tree[p].num = a[l];
		return;
	}
	long long mid = (l + r) / 2;
	build(l,mid,2 * p);
	build(mid + 1,r,2 * p + 1);
	tree[p].num = tree[2 * p].num + tree[2 * p + 1].num;
}
void update(long long p){
	if(tree[p].tag){
		tree[2 * p].num += (tree[2 * p].r - tree[2 * p].l + 1) * tree[p].tag;
		tree[2 * p].tag += tree[p].tag;
		tree[2 * p + 1].num += (tree[2 * p + 1].r - tree[2 * p + 1].l + 1) * tree[p].tag;
		tree[2 * p + 1].tag += tree[p].tag;
		tree[p].tag = 0;
	}
}
void add(long long l,long long r,long long p,long long x){
	if(l <= tree[p].l && tree[p].r <= r){
		tree[p].num += (tree[p].r - tree[p].l + 1) * x;
		tree[p].tag += x;
	}else{
		update(p);
		long long mid = (tree[p].l + tree[p].r) / 2;
		if(l <= mid) add(l,r,2 * p,x);
		if(r > mid) add(l,r,2 * p + 1,x);
		tree[p].num = tree[2 * p].num + tree[2 * p + 1].num;
	}
}
long long getsum(long long l,long long r,long long p){
	update(p); 
	if(tree[p].l > r || tree[p].r < l) return 0;
	if(tree[p].l >= l && tree[p].r <= r){
		return tree[p].num;
	}
	return getsum(l,r,2 * p) + getsum(l,r,2 * p + 1);
}

int main(){
	cin>>n>>m;
	for(long long i = 1;i <= n;i++){
		cin>>a[i];
	}
	build(1,n,1);
	for(long long i = 1;i <= m;i++){
		long long t;
		cin>>t;
		if(t == 1){
			long long x,y,k;
			cin>>x>>y>>k;
			add(x,y,1,k);
		}
		if(t == 2){
			long long x,y;
			cout<<getsum(x,y,1)<<endl;
		}
	}
	return 0;
}

问两者有何不同?

有用必关

2025/2/7 20:16
加载中...