PAT(Top Level) Practice 1010 Lehmer Code(35)(35점)
3637 단어 데이터 구조와 알고리즘
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then N distinct numbers are given in the next line.
Output Specification:
For each test case, output in a line the corresponding Lehmer code. The numbers must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.
Sample Input:
6 24 35 12 1 56 23 Sample Output:
3, 3, 1, 0, 1 0. 제목 보니까 하프만 코드 같은 거.원래는 아주 간단한 문제였다. 이 수를 수열한 다음에 몇 개의 수가 이 수보다 작다는 것을 구해라.직접 라인 트리 + 이산화하면 됩니다..(불이산화가 가능한지 모르겠다) 좋은 물이야 이거...
#include
using namespace std;
#define N (int)1e5+10
int arr[N];
int s[N];
#define lc root<<1
#define rc root<<1|1
typedef long long ll;
bool cmp(int a, int b)
{
return a < b;
}
struct seg{
int l, r;
ll he, lazy, tag;
}t[4*N];
void build(int root, int l, int r)
{
t[root].l = l, t[root].r = r;
t[root].lazy = 0;
t[root].tag = -1;
if (l == r)
{
t[root].he = 0;
return;
}
int m = (l + r) >> 1;
build(lc, l, m);
build(rc, m+1, r);
t[root].he = t[lc].he + t[rc].he;
}
void imptag(int root, ll change)
{
t[root].tag = change;
t[root].he = (t[root].r - t[root].l + 1) * change;
t[root].lazy = 0;
}
void implazy(int root, ll change)
{
t[root].lazy += change;
t[root].he += (t[root].r - t[root].l + 1) * change;
}
void pushdown(int root)
{
ll temp1 = t[root].tag;
if (temp1 != -1)
{
imptag(lc, temp1);
imptag(rc, temp1);
t[root].tag = -1;
}
ll temp2 = t[root].lazy;
if (temp2)
{
implazy(lc, temp2);
implazy(rc, temp2);
t[root].lazy = 0;
}
}
void assignment(int root, int l, int r, ll change)
{
if (r < t[root].l || l > t[root].r) return;
if (l <= t[root].l && t[root].r <= r)
{
imptag(root, change);
return;
}
pushdown(root);
assignment(rc, l, r, change);
assignment(lc, l, r, change);
t[root].he = t[lc].he + t[rc].he;
}
void update(int root, int l, int r, ll change)
{
if (r < t[root].l || l > t[root].r) return;
if (l <= t[root].l && t[root].r <= r)
{
implazy(root, change);
return;
}
pushdown(root);
update(rc, l, r, change);
update(lc, l, r, change);
t[root].he = t[lc].he + t[rc].he;
}
ll query(int root, int l, int r)
{
if (r < t[root].l || l > t[root].r) return 0;
if (l <= t[root].l && t[root].r <= r)
{
return t[root].he;
}
pushdown(root);
return query(rc, l, r) + query(lc, l, r);
}
int main(void)
{
int n; int i;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d", &arr[i]);
s[i] = arr[i];
}
sort(s+1, s+1+n, cmp);
int m = unique(s+1, s+1+n)-s;
for (i = 1; i <= n; i++)
{
arr[i] = lower_bound(s+1, s+m, arr[i]) - s;
}
build(1, 1, n);
vector res;
for (i = n; i >= 1; i--)
{
int ans = query(1, 1, arr[i]);
res.push_back(ans);
update(1, arr[i], arr[i], 1);
}
for (i = n - 1; i > 0; i --)
{
printf("%d ", res[i]);
}
printf("%d", res[i]);
}