Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #include <functional>
- #define MOD (int)(1e9+7)
- #define all(x) x.begin(),x.end()
- #define r_all(x) x.rbegin(),x.rend()
- #define lb lower_bound
- #define ub upper_bound
- #define pb push_back
- #define F first
- #define S second
- #define IOfast ios_base::sync_with_stdio(false); cin.tie(NULL);
- #define get_element_by_index(x) find_by_order(x)
- #define get_index(x) order_of_key(x)
- using namespace std;
- using namespace __gnu_pbds;
- typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> indexed_multiset;
- map<int,int64_t> fenwick;
- void update(int index,int64_t value)
- {
- for(int i = index;i<=(int)1e9;i+=i&-i) fenwick[i]+=value;
- }
- int64_t fetch(int index)
- {
- int64_t ans = 0;
- for(int i=index;i;i-=i&-i) if(fenwick.count(i)) ans+=fenwick[i];
- return ans;
- }
- void solve()
- {
- int n,k;
- cin>>n>>k;
- vector<int> arr(n);
- indexed_multiset ms;
- for(auto &item:arr) cin>>item;
- int64_t cnt = 0;
- for(int i=0;i<k;i++) ms.insert(arr[i]), update(arr[i],arr[i]);
- int64_t middle = *ms.get_element_by_index((k-1)/2);;
- //fenwick
- cnt = ms.get_index(middle);
- cout<<cnt*middle - 2*fetch(middle - 1) + fetch((int)1e9) - (k - cnt)*middle<<" ";
- int head = k;
- while(head < n)
- {
- ms.erase(ms.ub(arr[head - k]));
- ms.insert(arr[head]);
- update(arr[head - k],-arr[head - k]);
- update(arr[head],arr[head]);
- head++;
- middle = *ms.get_element_by_index((k-1)/2);
- cnt = ms.get_index(middle);
- cout<<cnt*middle - 2*fetch(middle - 1) + fetch((int)1e9) - (k - cnt)*middle<<" ";
- }
- }
- int main()
- {
- IOfast;
- int q=1;
- while(q--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement