1010 Lehmer Code (35)(35 分)
According to Wikipedia: “In mathematics and in particular in combinatorics, the Lehmer code is a particular way to encode each possible permutation of a sequence of n numbers.” To be more specific, for a given permutation of items {A1, A2, …, An}, Lehmer code is a sequence of numbers {L1, L2, …, Ln} such that Li is the total number of items from Ai to An which are less than Ai. For example, given {24, 35, 12, 1, 56, 23}, the second Lehmer code L2 is 3 since from 35 to 23 there are three items, {12, 1, 23}, less than the second item, 35.
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]);
}