POJ 2828Buy Tickets

时间:2021-08-12 16:55:38

POJ 2828

题目大意是说有n个插入操作,每次把B插入到位置A,原来A以后的全部往后移动1,球最后的序列

tree里保存的应该是这整个区间还有多扫个位置可以插入数据,那么线段树里从后往前扫描依次插入数据

比如现在吧B插入到A位置,如果整个区间左侧还有<A个位置可以插入数据,那么就只能将其放到整个区间的右侧,递归下去就可以了

 void update(int k, int L, int R, int x)
{
if(L == R) { pre[k] = r; ans[L] = val; return ; } int mid = (L+R)>>; if(x <= pre[k<<]) update(lson, x);//左侧的多余x else update(rson, x-pre[k<<]);//否则往右 pre[k] = pre[k<<] + pre[k<<|];
}
 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; } typedef __int64 LL;
//typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const LL MOD = ; int N, pre[MAXN<<];
int id[MAXN], num[MAXN], ans[MAXN];
int r, val; int buildTree(int k, int L, int R)
{
if(L == R) return pre[k] = ;
int mid = (L+R)>>;
return pre[k] = buildTree(lson) + buildTree(rson);
} void update(int k, int L, int R, int x)
{
if(L == R) { pre[k] = r; ans[L] = val; return ; } int mid = (L+R)>>; if(x <= pre[k<<]) update(lson, x); else update(rson, x-pre[k<<]); pre[k] = pre[k<<] + pre[k<<|];
} int main()
{
while(~scanf("%d", &N))
{
buildTree(, , N);
for(int l=;l<=N;l++)
scanf("%d %d", &id[l], &num[l]);
r = ;
for(int i=N;i>;i--)
{
val = num[i];
update(, , N, id[i] + );
}
for(int i=;i<=N;i++) printf("%d%c", ans[i], i==N?'\n':' ');
}
return ;
}