Greg and Array
time limit per test 2 seconds
memory limit per test 56 megabytes
input standard input
output standard output
Greg has an array a = a1, a2, ..., an andm operations. Each operation looks as: li,ri,di,(1 ≤ li ≤ ri ≤ n). To apply operationi to the array means to increase all array elements with numbersli, li + 1, ..., ri by valuedi.
Greg wrote down k queries on a piece of paper. Each query has the following form:xi,yi,(1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbersxi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line containsn integers:a1, a2, ..., an(0 ≤ ai ≤ 105) — the initial array.
Next m lines contain operations, the operation numberi is written as three integers:li,ri,di,(1 ≤ li ≤ ri ≤ n),(0 ≤ di ≤ 105).
Next k lines contain the queries, the query numberi is written as two integers:xi,yi,(1 ≤ xi ≤ yi ≤ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers inC++. It is preferred to use thecin, cout streams of the%I64d specifier.
Sample test(s)
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20
這題真是太遺憾了,有一個地方下標應該從1開始寫成了0,結果浪費了5分多鐘的時間找bug,結果比賽就結束了,遺憾啊。 考察點:樹狀數組 思路:建立兩個樹狀數組,分別統計操作實行的次數和每次操作數值的變化
[cpp]
#include <stdio.h>
#include <string.h>
#include <math.h>
__int64 tree1[110000],tree2[110000];
struct num {
int l,r,val; }a[110000];
__int64 b[1000000],sum[110000];
__int64 n,m; int main()
{ void build(__int64 k,__int64 val);
void build2(__int64 k,__int64 val);
__int64 search(__int64 k);
__int64 search2(__int64 k);
__int64 i,j,s,t;
__int64 x,y,k;
while(scanf("%I64d %I64d %I64d",&n,&m,&k)!=EOF)
{ for(i=1;i<=n;i++)
{ scanf("%I64d",&b[i]); }
for(i=1;i<=m;i++)
{ scanf("%d %d %d",&a[i].l,&a[i].r,&a[i].val);
} memset(tree2,0,sizeof(tree2));
memset(tree1,0,sizeof(tree1)); for(i=1;i<=k;i++)
{ scanf("%I64d %I64d",&x,&y);
build(y,1);
build(x-1,-1); }
memset(sum,0,sizeof(sum));
for(i=1;i<=m;i++)
{ t=search(i);
sum[i]=sum[i]+t;
} for(i=1;i<=m;i++)
{ build2(a[i].r,sum[i]*a[i].val);
build2(a[i].l-1,-1*sum[i]*a[i].val); }
for(i=1;i<=n;i++) {
t=search2(i); b[i]+=t;
if(i==1) {
printf("%I64d",b[i]);
}else {
printf(" %I64d",b[i]);
} } printf("\n");
} return 0; } int f(int k) {
return (k&-k); } void build(__int64 k,__int64 val) {
while(k>0) { tree2[k]+=val;
k-=f(k); } } __int64 search(__int64 k) {
__int64 s=0; while(k<=m)
{ s+=tree2[k];
k+=f(k); }
return s; } void build2(__int64 k,__int64 val)
{
while(k>0)
{
tree1[k]+=val;
k-=f(k);
} } __int64 search2(__int64 k) {
__int64 s=0; while(k<=n)
{ s+=tree1[k];
k+=f(k); }
return s; }