标签:树状数组
分析:这道题因为是更新是和询问分开的,所以我们先更新,然后在将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\n", (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
标签:树状数组
原文地址:http://blog.csdn.net/shengweisong/article/details/39831339