nyoj 228 士兵杀死(五岁以下儿童)【树状数组】

分析:这个问题问的是,因为它是一个单独的更新。因此,让我们更新,然后在c[i]表现为1~i之间,还原之后看起来像一个。

 
#include <cstdio>
#include <cstring>
#define M 1000005
#define INF 10003
int c[M];
int main(){
	int x, y, z, n, s, q;
	scanf("%d%d%d", &n, &s, &q);
	memset(c, 0, sizeof(c));
	while(s --){
		scanf("%d%d%d", &x, &y, &z);
		c[x] += z;
		c[y+1] -= z;
	}
	for(int i = 1; i <= n; i ++) c[i] += c[i-1];
	for(int i = 1; i <= n; i ++) c[i] = (c[i]+c[i-1])%INF;
	while(q --){
		scanf("%d%d", &x, &y);
		printf("%d
", (c[y]-c[x-1]+INF)%INF); //由于怕c[y]-x[x-1]小于0,所以在加个INF。
	}
	return 0;
}         


题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=228

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4874858.html