题目链接
http://poj.org/problem?id=2828
题意
有n个操作,(x y)表示把y放入x位置,如果这个位置有其他数,则这个数以及后面紧挨着的数全部后移,问最后的序列是什么样的,输出这个序列
题解
可以考虑从后往前操作,这样就能保证所插入的数能放到确定的位置,并且之后避免了移动的操作,不过与之前不同的是,从后往前操作的(x y)表示把y放入当前的从左往右数第x个空位;用线段树保存区间和,如果当前位置为空,就置1,否则置0,那么我们就将区间和与第x个空位的位置联系在了一起,只要找到第一个前缀和为x的区间的右端点即可;在线段树的查询过程中执行二分,如果左孩子sum大于x,就往左,否则就往右孩子去,并将x置为x=x-左孩子sum,以此递归下去,最终递归到叶子,返回位置即可;
此题也可以用树状数组+二分解决,树状数组存前缀和
代码
线段树
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;
#define inf 0x7f7f7f7f
#define maxn 100005
#define mod 1000000007
#define N 200005
#define P 2
typedef long long ll;
typedef struct {
int l, r, max, sum;
} Tree;
Tree tree[4 * N];
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
void push_up(int x) {
tree[x].sum = tree[x << 1].sum + tree[x << 1 | 1].sum;
}
void build(int x, int l, int r) {
tree[x].l = l, tree[x].r = r;
tree[x].sum = 0;
if (l == r) {
tree[x].sum = 1;
return ;
}
int mid = (l + r) >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
//tree[x].max = max(tree[x << 1].max, tree[x << 1 | 1].max);
push_up(x);
}
int query(int x, int len) {
int res = 0;
if (tree[x].l == tree[x].r) {
tree[x].sum = 0;
return tree[x].l;
}
if (tree[x << 1].sum >= len) res = query(x << 1, len);
else res = query(x << 1 | 1, len - tree[x << 1].sum);
//tree[x].max = max(tree[x << 1].max, tree[x << 1 | 1].max);
push_up(x);
return res;
}
void update(int x, int pos, int val) {
int l = tree[x].l, r = tree[x].r;
if (l == r) {
tree[x].sum = val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)update(x << 1, pos, val);
else if (pos > mid)update(x << 1 | 1, pos, val);
push_up(x);
}
int n, a[N], b[N], c[N];
int main() {
while (scanf("%d", &n) != EOF) {
build(1,1,n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]++;
}
for (int i = n; i >=1; i--) {
int pos = query(1, a[i]);
c[pos] = b[i];
}
for (int i = 1; i <= n-1; i++) {
cout << c[i] << " ";
}
cout << c[n] << endl;
}
}
树状数组+二分
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
//#include<unordered_map>
using namespace std;
#pragma GCC optimize(2)
#define maxn 200090
#define inf 0x3f3f3f3f
#define PI acos(-1)
typedef long long ll;
struct edge {
int u, v, next, w;
} e[2];
int head[1], cnt;
void add(int x, int y, int w) {
e[cnt].u = x;
e[cnt].v = y;
e[cnt].w = w;
e[cnt].next = head[x];
head[x] = cnt++;
e[cnt].u = y;
e[cnt].v = x;
e[cnt].w = w;
e[cnt].next = head[y];
head[y] = cnt++;
}
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n, d[maxn], a[maxn], b[maxn], c[maxn], pos;
void insert(int x, int val) {
while (x <= n + 2) {
c[x] += val;
x += x & -x;
}
}
int sum(int x) {
int res = 0;
while (x) {
res += c[x];
x -= x & -x;
}
return res;
}
int find(int val) {
int l = 1, r = n;
while (l < r) {
int mid = (l + r) >> 1;
int s = sum(mid);
if (s >= val)r = mid;
else if (s < val)l = mid + 1;
}
return l;
}
int main() {
while (cin >> n) {
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
a[i] = read() + 1, b[i] = read();
insert(i, 1);
}
for (int i = n; i > 0; i--) {
pos = find(a[i]);
//cout<<pos<<" "<<b[i]<<endl;
d[pos] = b[i];
insert(pos, -1);
}
for (int i = 1; i <= n - 1; i++)
cout << d[i] << " ";
cout << d[n] << endl;
}
return 0;
}