这是一道很好的题
开始想到链表,但是在第 (k) 个位置插入却很烦人,因为链表查询时 (O(n)) 的
因为是线段树题,就想到线段树上去
正着好像很难,那么反着可以么?
反着,对于每一个操作,就相当于查找前面空出 (a) 个格子的位置是哪个,直接线段树上搞一下就行了
正难则反的思维还是很精妙的
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
//#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 200010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf
", x + 0.0000000001) ; }
template <class T> void chmin(T &a, T b) { if (a > b) a = b ; }
template <class T> void chmax(T &a, T b) { if (a < b) a = b ; }
void upd(int &a, int b) { (a += b) %= MOD ; }
void mul(int &a, int b) { a = 1ll * a * b % MOD ; }
int n ;
struct node { int ps, val ; } a[N] ;
struct Tree {
int l, r, v, num ;
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define l(x) tr[x].l
#define r(x) tr[x].r
#define sz(x) (tr[x].r - tr[x].l + 1)
#define v(x) tr[x].v
#define num(x) tr[x].num
} tr[N << 2] ;
void pushup(int x) {
v(x) = v(ls(x)) + v(rs(x)) ;
}
void build(int x, int l, int r) {
l(x) = l, r(x) = r ;
if (l == r) {
v(x) = 1 ; // 初始均为空格
return ;
}
int mid = (l(x) + r(x)) >> 1 ;
build(ls(x), l, mid) ;
build(rs(x), mid + 1, r) ;
pushup(x) ;
}
void modify(int x, int pos, int v) {
if (l(x) == r(x) && v(x)) {
num(x) = v ;
v(x) = 0 ;
return ;
}
if (pos <= v(ls(x))) modify(ls(x), pos, v) ;
else modify(rs(x), pos - v(ls(x)), v) ;
pushup(x) ;
}
void query(int x) {
if (l(x) == r(x)) {
printf("%d ", num(x)) ;
return ;
}
query(ls(x)) ; query(rs(x)) ;
}
signed main(){
while (scanf("%d", &n) != EOF) {
rep(i, 1, n) scanf("%d%d", &a[i].ps, &a[i].val) ;
build(1, 1, n) ;
per(i, n, 1) {
modify(1, a[i].ps + 1, a[i].val) ;
}
// 查询 [1,n] 区间中有 x+1 个空格的位置并修改
query(1) ; enter ;
}
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/