HzuWHF


私信TA

用户名:I7I08I9047

访问量:77332

签 名:

我RUN了

等  级
排  名 18
经  验 20556
参赛次数 13
文章发表 127
年  龄 3
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:

        ( 反正没人看,注释不想写,写花了更难看,跑得挺慢的

参考代码:

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

const int SIZE = 100002;
struct datas {
	int value, left, right, lazy;
} tree[SIZE << 2];

void buildtree(int root, int L, int R) {
	tree[root].left = L, tree[root].right = R;
	if (L == R)
		return;
	int mid = (L + R) >> 1;
	buildtree(root << 1, L, mid);
	buildtree(root << 1 | 1, mid + 1, R);
}

void pushdown(int root){
	tree[root << 1].lazy += tree[root].lazy;
	tree[root << 1 | 1].lazy += tree[root].lazy;
	tree[root << 1].value += (tree[root << 1].right - tree[root << 1].left + 1)
		* tree[root].lazy;
	tree[root << 1 | 1].value += (tree[root << 1 | 1].right - tree[root << 1 | 1].left + 1) 
		* tree[root].lazy;
	tree[root].lazy = 0;
}

void updata(int root, int L, int R, int Add) {
	if (tree[root].left == tree[root].right) {
		tree[root].value += Add; return;
	}
	if (tree[root].left == L && tree[root].right == R) {
		tree[root].value += (R - L + 1) * Add;
		tree[root].lazy += Add;
		return;
	}
	if(tree[root].lazy)
		pushdown(root);

	int mid = (tree[root].left + tree[root].right) >> 1;
	if (L > mid)
		updata(root << 1 | 1, L, R, Add);
	else if (R <= mid)
		updata(root << 1, L, R, Add);
	else {
		updata(root << 1 | 1, mid + 1, R, Add);
		updata(root << 1, L, mid, Add);
	}
}

void query(int root, int L, int R) {
	if (tree[root].lazy && tree[root].left != tree[root].right)
		pushdown(root);
	if (tree[root].left == tree[root].right) {
		printf("%d ", tree[root].value); return;
	}
	int mid = (tree[root].left + tree[root].value) >> 1;
	query(root << 1, L, mid);
	query(root << 1 | 1, mid + 1, R);
}

int main() {
	int total, que; scanf("%d%d", &total, &que);
	buildtree(1, 1, total);
	int L, R, Add;
	while (que--) {
		scanf("%d%d%d", &L, &R, &Add);
		updata(1, L, R, Add);
	}
	query(1, 1, total);
	printf("\n");
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区