全 $Wa$ 求助
  • 板块P2068 统计和
  • 楼主_XSOI_
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/6/2 17:30
  • 上次更新2024/6/2 20:19:50
查看原帖
全 $Wa$ 求助
1161545
_XSOI_楼主2024/6/2 17:30
#include <iostream>
#define int long long
using namespace std;
const int MAXX = 1e5 + 11;
int a[MAXX];
struct node {
	int l,r,sum,add; // add 为懒标记
} tree[MAXX * 4];
void pushup(int pos) {
	tree[pos].sum = tree[pos * 2].sum + tree[pos * 2 + 1].sum;
}
void build(int pos,int l,int r) { // 建树
	tree[pos].l = l;
	tree[pos].r = r;
	tree[pos].sum = a[l];
	if (l == r) {
		return ;
	}
	int mid = (l + r) >> 1;
	build(pos * 2,l,mid); // 左子树建树
	build(pos * 2 + 1,mid + 1,r); // 右子树建树
	pushup(pos);
}
void update(int pos,int x,int k) { // 区间修改
	if (tree[pos].l == x and tree[pos].r == x) { // 找到了叶子节点
		tree[pos].sum = k;
		return ;
	}
	int mid = (tree[pos].l + tree[pos].r) >> 1;
	if (x <= mid) {
		update(pos * 2,x,k);
	} else {
		update(pos * 2 + 1,x,k);
	}
	pushup(pos);
}
int query(int pos,int x,int y) { // 区间查询
	if (tree[pos].l >= x and tree[pos].r <= y) {
		return tree[pos].sum;
	}
	int mid = (tree[pos].l + tree[pos].r) >> 1;
	int sum = 0;
	if (x <= mid) {
		sum += query(pos * 2,x,y);
	}
	if (y > mid) {
		sum += query(pos * 2 + 1,x,y);
	}
	return sum;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0) , cout.tie(0);
	int n,m;
	cin >> n >> m;
	build (1,1,n);
	while (m --) {
		char op;
		cin >> op;
		if (op == 'x') {
			int x,k;
			cin >> x >> k;
			update(1,x,k);
		} else {
			int x,y;
			cin >> x >> y;
			cout << query(1,x,y) << "\n";
		}
	}
	return 0;
}

开了 long long 的,样例过了。

2024/6/2 17:30
加载中...