Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define all(x) x.begin(), x.end()
- #define siz(x) (int)x.size()
- #define FOR(x, y, z) for(int x = (y); x < (z); x++)
- #define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
- #define F0R(x, z) FOR(x, 0, z)
- #define R0F(x, z) ROF(x, z, 0)
- #define trav(x, y) for(auto&x:y)
- using ll = int64_t;
- using vi = vector<int>;
- using vl = vector<ll>;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using str = string;
- using vpii = vector<pii>;
- using vpll = vector<pll>;
- const int mxN = 2e5 + 10;
- int ans[mxN], bit[mxN], n, a[mxN], cnt[mxN]; vi q[mxN];
- int qry(int r){
- int res = 0;
- for(; r; r-=r&-r){
- res += bit[r];
- }
- return res;
- }
- void update(int x, int v){
- for(; v < mxN; v += v & -v){
- bit[v] += x;
- }
- }
- signed main(){
- ifstream cin("haircut.in"); ofstream cout("haircut.out");
- cin >> n;
- FOR(i, 1, n+1) {cin >> a[i]; q[a[i]].push_back(i);}
- FOR(i, 1, n+1){
- if(a[i])
- cnt[i] = qry(n)-qry(a[i]);
- else
- cnt[i] = qry(n);
- if(a[i])
- update(1, a[i]);
- }
- int res = 0;
- FOR(i, 1, n+1) res += cnt[i];
- ROF(i, n, 1){
- trav(v, q[i]){
- res -= cnt[v];
- }
- ans[i] = res;
- }
- F0R(i, n) cout << ans[i] << endl;
- }
- /*
- * Stuff I should try:
- * Is it Monotonic? -> Binary Search
- * Is N small? -> Bitsets or other exponential
- * Edge Cases? Look Carefully
- * Can't figure anything out? Brute Force and see patterns
- * Try to prove greedies
- * Prefix Sums, Amortization, DP, Data Structures
- */
Advertisement
Add Comment
Please, Sign In to add comment