###### [BZOJ2161]布娃娃（扫描线+线段树）

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define erp(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
const int MAXN = 100005;
const int mo = 19921228;
int N;
int P[MAXN], C[MAXN], L[MAXN], R[MAXN];
int dat[MAXN], dn;

void genarr(int*a)
{
a[1] = first % mod;
rep(i, 2, N) a[i] = (1ll*a[i-1]*prod+add+i)%mod;
}

struct doll {
int t, c, tp, k; //tp=0:insert   tp=1:delete   tp=1:query.
doll () {}
doll (int q,int w,int e,int f) : t(q),c(w),tp(e),k(f){}
bool operator < (const doll&b) const {
if (t^b.t) return t < b.t;
return tp < b.tp;
}
} q[MAXN*3];
int qn;

#define lch(a) tr[a].lch
#define rch(a) tr[a].rch
struct Node {
int lch, rch, cnt;
} tr[MAXN*4];
int ncnt, root;
void ins(int&x, int v, int l=1, int r=dn)
{
if (!x) x = ++ncnt;
tr[x].cnt ++;
if (l==r) return;
int mid = (l+r)>>1;
if (v<=mid) ins(lch(x), v, l, mid);
else ins(rch(x), v, mid+1, r);
}
void del(int x, int v, int l=1, int r=dn)
{
tr[x].cnt --;
if (l==r) return;
int mid = (l+r)>>1;
if (v<=mid) del(lch(x), v, l, mid);
else del(rch(x), v, mid+1, r);
}
int getkth(int x, int k, int l=1, int r=dn)
{
if (l==r) return l;
int mid = (l+r)>>1;
if (tr[rch(x)].cnt>=k) return getkth(rch(x), k, mid+1, r);
return getkth(lch(x), k-tr[rch(x)].cnt, l, mid);
}

int solve()
{
sort(q+1, q+qn+1);
int ans = 0;
rep(i, 1, qn)
{
if (q[i].tp==0) ins(root, q[i].c);
else if (q[i].tp==1) del(root, q[i].c);
else if (tr[root].cnt >= q[i].k) (ans+=dat[getkth(root, q[i].k)]%mo) %= mo;
}
return ans;
}

int main()
{
//freopen("doll.in","r",stdin);
//freopen("doll.out","w",stdout);
scanf("%d", &N);
genarr(P), genarr(C), genarr(L), genarr(R);
rep(i, 1, N) dat[i] = C[i];
sort(dat+1, dat+N+1);
dn = unique(dat+1, dat+N+1)-dat-1;
rep(i, 1, N) C[i] = lower_bound(dat+1, dat+dn+1, C[i])-dat;
rep(i, 1, N)
{
if (L[i]>R[i]) swap(L[i], R[i]);
q[++qn] = doll(L[i], C[i], 0, 0);
q[++qn] = doll(P[i], 0, 2, i);
q[++qn] = doll(R[i]+1, C[i], 1, 0);
}
int ans = solve();
printf("%d\n", ans);
return 0;
}


#### bzoj 2161: 布娃娃 （扫描线+线段树）

2017-04-25 09:55:36

2016-10-19 14:33:41

#### [BZOJ 2731][HNOI 2012]三角形覆盖问题(计算几何+扫描线暴力)

2015-03-10 19:22:31

#### [扫描线 线段树] BZOJ 2161 布娃娃

2016-06-26 08:40:40

#### [扫描线 线段树上二分] BZOJ 4411 [Usaco2016 Feb]Load balancing

2016-10-23 12:14:47

#### BZOJ 1845 [Cqoi2005] 三角形面积并 计算几何扫描线

2015-09-08 12:44:49

#### [线段树 扫描线 鬼畜] BZOJ 3228 [Sdoi2008]棋盘控制 && BZOJ 1905 Soldier 士兵控制的棋盘

2016-09-22 18:41:27

#### 【BZOJ2584】memory，扫描线+拓扑图+骗

2016-10-16 16:16:57

#### bzoj 2161: 布娃娃

2017-10-06 10:49:03

#### bzoj2161布娃娃 线段树

2018-01-18 16:03:32

## 不良信息举报

[BZOJ2161]布娃娃（扫描线+线段树）