D. Restore Permutation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2],[1],[1,2,3,4,5][3,1,2],[1],[1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2],[1,1],[2,3,4][2],[1,1],[2,3,4].

There is a hidden permutation of length nn.

For each index ii, you are given sisi, which equals to the sum of all pjpj such that j<ij<i and pj<pipj<pi. In other words, sisi is the sum of elements before the ii-th element that are smaller than the ii-th element.

Your task is to restore the permutation.

Input

The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the size of the permutation.

The second line contains nn integers s1,s2,…,sns1,s2,…,sn (0≤si≤n(n−1)20≤si≤n(n−1)2).

It is guaranteed that the array ss corresponds to a valid permutation of length nn.

Output

Print nn integers p1,p2,…,pnp1,p2,…,pn — the elements of the restored permutation. We can show that the answer is always unique.

Examples
input

Copy
3
0 0 0
output

Copy
3 2 1
input

Copy
2
0 1
output

Copy
1 2
input

Copy
5
0 1 1 1 10
output

Copy
1 4 3 2 5
Note

In the first example for each ii there is no index jj satisfying both conditions, hence sisi are always 00.

In the second example for i=2i=2 it happens that j=1j=1 satisfies the conditions, so s2=p1s2=p1.

In the third example for i=2,3,4i=2,3,4 only j=1j=1 satisfies the conditions, so s2=s3=s4=1s2=s3=s4=1. For i=5i=5 all j=1,2,3,4j=1,2,3,4 are possible, so s5=p1+p2+p3+p4=10s5=p1+p2+p3+p4=10.

题目链接:http://codeforces.com/contest/1208/problem/D

题解:首先我们需要建立一个权值线段树,节点的权值分别是1~n,然后我就只需要逆序遍历si数组,找到大于s[i] + 1的数,这个数就是当前位置的值。注意的是,如果我们找到了那个数,就要将那个位置的权值赋为0,表示该数已经用过了。

#include <iostream>
#include <cstdio> using namespace std; const int maxn = 2e5+; typedef long long ll; ll s[maxn], ans[maxn], tree[maxn << ]; void update(int root, int l, int r, int pos, ll val) {
if(l == r) {
tree[root]= val;
return;
}
int mid = (l + r) >> ;
if(pos <= mid) {
update(root << , l, mid, pos, val);
} else {
update(root << | , mid + , r, pos, val);
}
tree[root] = tree[root << ] + tree[root << | ];
} int query(int root, int l, int r, ll val) {
if(l == r) {
return tree[root];
}
int mid = (l + r) >> ;
if(tree[root << ] >= val) {
return query(root << , l, mid, val);
} else {
return query(root << | , mid + , r, val - tree[root << ]);
}
} int main() {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%I64d", &s[i]);
update(, , n, i, i * 1LL); //建立一颗权值线段树
}
for(int i = n; i >= ; i--) {
ans[i] = query(, , n, s[i] + 1LL); //找到大于si + 1的这个数
update(, , n, ans[i], 0LL); //权值赋0
}
for(int i = ; i <= n; i++) {
printf("%I64d ", ans[i]);
}
return ;
}

D. Restore Permutation(权值线段树)的更多相关文章

  1. hdu 5592 ZYB's Premutation (权值线段树)

    最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  3. 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP

    2892: 强袭作战 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 45  Solved: 30[Submit][Status][Discuss] D ...

  4. BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)

    题目大意:有一些位置.这些位置上能够放若干个数字. 如今有两种操作. 1.在区间l到r上加入一个数字x 2.求出l到r上的第k大的数字是什么 思路:这样的题一看就是树套树,关键是怎么套,怎么写.(话说 ...

  5. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

  6. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  7. 【BZOJ3685】【zkw权值线段树】普通van Emde Boas树

    原题传送门 因为马上要开始搞树套树了,所以学了一波权值线段树...毕竟是会点zkw线段树的,所以zkw线段树大法好! 解题思路: 介绍一下权值线段树吧,其实感觉就是线段树的本义,就是你用线段树维护了数 ...

  8. BZOJ_2161_布娃娃_权值线段树

    BZOJ_2161_布娃娃_权值线段树 Description 小时候的雨荨非常听话,是父母眼中的好孩子.在学校是老师的左右手,同学的好榜样.后来她成为艾利斯顿第二 代考神,这和小时候培养的良好素质是 ...

  9. BZOJ_3685_普通van Emde Boas树_权值线段树

    BZOJ_3685_普通van Emde Boas树_权值线段树 Description 设计数据结构支持: 1 x  若x不存在,插入x 2 x  若x存在,删除x 3    输出当前最小值,若不存 ...

随机推荐

  1. (八)CXF之用spring添加拦截器

    一.案例 本章案例是基于CXF之自定义拦截器基础之上改造的,目的是在服务端中用spring添加拦截器 配置web.xml <?xml version="1.0" encodi ...

  2. (四)Decorator设计模式解决GET/POST请求的乱码问题(转)

    一.Decorator设计模式 1.1.Decorator设计模式介绍 当某个对象的方法不适应业务需求时,通常有2种方式可以对方法进行增强: 编写子类,覆盖需增强的方法. 使用Decorator设计模 ...

  3. 货币转换C

    描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

  4. POJ2729 Robocode(离散化与模拟-----提醒曾经爱玩游戏的自己没做出这个

    题目链接 :http://poj.org/problem?id=2729 题目很长,有不少也是废话.类似小时候玩的坦克大战.每个坦克速度为10,炮弹速度为20.子弹出界就消失,坦克出不了界限.相向的子 ...

  5. Django 配置JWT验证

    1.setting.py配置 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentica ...

  6. VBA输入框(InputBox)(六)

    InputBox函数提示用户输入值.当输入值后,如果用户单击确定 按钮或按下键盘上的ENTER 键,InputBox函数将返回文本框中的文本.如果用户单击“取消” 按钮,该函数将返回一个空字符串(&q ...

  7. VBA消息框(MsgBox)(五)

    MsgBox函数显示一个消息框,并等待用户点击一个按钮,然后根据用户点击的按钮执行相关的操作. 语法 MsgBox(prompt[,buttons][,title][,helpfile,context ...

  8. JavaScript--常用对象的属性及方法(3)

    String对象(字符串) 字符串在本质上也是数组 都可以通过str[i]访问内容 但是数组创建后可以修改 而字符串一旦创建内容不可更改 属性:length 作用与数组相同 获取字符串的长度 方法: ...

  9. Redis面试题记录--缓存双写情况下导致数据不一致问题

    转载自:https://blog.csdn.net/lzhcoder/article/details/79469123 https://blog.csdn.net/u013374645/article ...

  10. TDDL生成全局ID原理

    TDDL 在分布式下的SEQUENCE原理 TDDL大家应该很熟悉了,淘宝分布式数据层.很好的为我们实现了分库分表.Master/Salve.动态数据源配置等功能. 那么分布式之后,数据库自增序列肯定 ...