Advertisement
remember_Me

Untitled

Jul 30th, 2020 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #include <functional>
  5. #define MOD                   (int)(1e9+7)
  6. #define all(x)                x.begin(),x.end()
  7. #define r_all(x)              x.rbegin(),x.rend()
  8. #define lb                    lower_bound
  9. #define ub                    upper_bound
  10. #define pb                    push_back
  11. #define F                     first
  12. #define S                     second
  13. #define IOfast                ios_base::sync_with_stdio(false); cin.tie(NULL);
  14. #define get_element_by_index(x) find_by_order(x)
  15. #define get_index(x)            order_of_key(x)
  16. using namespace std;
  17. using namespace __gnu_pbds;
  18.  
  19. typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> indexed_multiset;
  20.  
  21. map<int,int64_t> fenwick;
  22.  
  23. void update(int index,int64_t value)
  24. {
  25.   for(int i = index;i<=(int)1e9;i+=i&-i) fenwick[i]+=value;
  26. }
  27.  
  28. int64_t fetch(int index)
  29. {
  30.   int64_t ans = 0;
  31.   for(int i=index;i;i-=i&-i) if(fenwick.count(i)) ans+=fenwick[i];
  32.   return ans;
  33. }
  34.  
  35. void solve()
  36. {
  37.   int n,k;
  38.   cin>>n>>k;
  39.   vector<int> arr(n);
  40.   indexed_multiset ms;
  41.   for(auto &item:arr) cin>>item;
  42.  
  43.   int64_t cnt = 0;
  44.   for(int i=0;i<k;i++) ms.insert(arr[i]), update(arr[i],arr[i]);
  45.  
  46.   int64_t middle = *ms.get_element_by_index((k-1)/2);;
  47.   //fenwick
  48.   cnt = ms.get_index(middle);
  49.   cout<<cnt*middle - 2*fetch(middle - 1) + fetch((int)1e9) - (k - cnt)*middle<<" ";
  50.   int head = k;
  51.  
  52.   while(head < n)
  53.   {
  54.     ms.erase(ms.ub(arr[head - k]));
  55.     ms.insert(arr[head]);
  56.     update(arr[head - k],-arr[head - k]);
  57.     update(arr[head],arr[head]);
  58.     head++;
  59.     middle = *ms.get_element_by_index((k-1)/2);
  60.     cnt = ms.get_index(middle);
  61.     cout<<cnt*middle - 2*fetch(middle - 1) + fetch((int)1e9) - (k - cnt)*middle<<" ";
  62.   }
  63. }
  64.  
  65.  
  66. int main()
  67. {
  68.   IOfast;
  69.  
  70.   int q=1;
  71.   while(q--)
  72.   {
  73.     solve();
  74.   }
  75.  
  76.   return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement