Advertisement
Guest User

CodeForces#179 Div2 C

a guest
Dec 23rd, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int N,M,K,a,b,c;
  4. long long BIT[200001],Auxiliary[200001];
  5. void Update(long long* Tree,int Index,int Value)
  6. {
  7.     for(;Index<=N;Index+=Index&-Index)
  8.         cout<<Index<<endl,Tree[Index]+=Value;
  9.     return;
  10. }
  11. long long CumulativeSum(long long* Tree,int Index)
  12. {
  13.     long long ret=0;
  14.     for(;Index>0;Index-=Index&-Index)
  15.         ret+=Tree[Index];
  16.     return ret;
  17. }
  18. long long RangeQuery(int Index)
  19. {
  20.     return CumulativeSum(BIT,Index)*Index-CumulativeSum(Auxiliary,Index);
  21. }
  22. /// Trying to implement the idea of     http://kartikkukreja.wordpress.com/2013/12/02/range-updates-with-bit-fenwick-tree/
  23. int main()
  24. {
  25.     ios_base::sync_with_stdio(false);
  26.     cin>>N>>M>>K;
  27.     for(int i=1;i<=N;i++)
  28.     {
  29.         cin>>c;
  30.         Update(BIT,i,c);   // Maybe I have Mistaken Here
  31.     }
  32.     for(int i=1;i<=M;i++)
  33.     {
  34.         cin>>a>>b>>c;
  35.         Update(BIT,a,c);
  36.         Update(BIT,b+1,-c);
  37.         Update(Auxiliary,a,c*(a-1));
  38.         Update(Auxiliary,b+1,-c*b);
  39.     }
  40.     for(int i=0;i<K;i++)
  41.     {
  42.         cin>>a>>b;
  43.         cout<<RangeQuery(b)-RangeQuery(a-1)<<endl;
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement