迟点再写注释
#include <iostream>
#include <cstdio>
#include<map>
#include<cstring>
#include<stack>
using namespace std;
const int MAX = 200005;
struct{
int l,r,w;
}node[3*MAX];
int id, pos[MAX],val[MAX],ans[MAX];
void BuildTree(int left, int right, int u)
{
node[u].l = left;
node[u].r = right;
node[u].w = right - left + 1;//储存区间节点个数
// printf("%d %d %d %d\n",node[u].l ,node[u].r,node[u].w,u);
if (left < right){
BuildTree(left, (left+right)>>1,u<<1);//递归左子树
BuildTree(((left+right)>>1)+1,right,(u<<1)+1);//递归右子树
}
}
void updata(int pos, int u)
{
node[u].w--;
printf("%d %d %d\n",node[u].l,node[u].r,u);
if (node[u].l == node[u].r){
id = node[u].l;
return ;
}
if (node[u<<1].w >= pos)
updata(pos, u<<1);
else {
pos -= node[u<<1].w;
updata(pos, (u<<1)+1);
}
}
int main(void)
{
int n, i;
while(~scanf("%d",&n)){
BuildTree(1,n,1);
for (i = 1; i <= n; i++){
scanf("%d%d",&pos[i],&val[i]);
}
for (i = n; i >= 1; i--){
updata(pos[i]+1,1);
ans[id] = val[i];
printf("%d\n",id);
}
for (i = 1; i <= n; i++){
printf("%d ",ans[i]);
}
printf("\n");
}
return 0;
}