Codeforces Round #179 (Div. 2) C. Greg and Array

来源:互联网 发布:小漠淘宝店服装店 编辑:程序博客网 时间:2024/05/24 01:48
                                                                                                                      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 31 2 31 2 11 3 22 3 41 21 32 3
Output
9 18 17
Input
1 1 111 1 11 1
Output
2
Input
4 3 61 2 3 41 2 12 3 23 4 41 21 32 31 21 32 3
Output
5 18 31 20   这题真是太遗憾了,有一个地方下标应该从1开始写成了0,结果浪费了5分多钟的时间找bug,结果比赛就结束了,遗憾啊。
   考察点:树状数组
   思路:建立两个树状数组,分别统计操作实行的次数和每次操作数值的变化
#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;}

原创粉丝点击