Home BOJ. Race (2517)
Post
Cancel

BOJ. Race (2517)

[Link] https://www.acmicpc.net/problem/2517


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define endl "\n"
#define nulls '\0'
#define dkdk " "

const int MAX = 5e5 + 1;
int n;
int seg[MAX << 1];
vector<int> abilities;

void insert(int ability) {
  for(ability+=MAX; ability > 0; ability>>=1) seg[ability]++;
}

int query(int ability) {
  int rank = 0;
  for(int l = MAX, r = MAX + ability; l < r; l>>=1, r>>=1) {
    if(l & 1) rank += seg[l++];
    if(r & 1) rank += seg[--r];
  }
  return rank;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.txt", "r", stdin);
  cin >> n;
  int cur_rank = 1;

  for0(i, n) {
    int ability; cin >> ability;
    abilities.push_back(ability);
  }
  vector<int> cp = abilities;
  sort(cp.begin(), cp.end(), greater<int>());

  for0(i, n) {
    int rank = lower_bound(cp.begin(), cp.end(), abilities[i], greater<int>()) - cp.begin();
    cout << min(query(rank), i) + 1 << endl;
    insert(rank);
  }

  return 0;
}
This post is licensed under CC BY 4.0 by the author.