# [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;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：[BZOJ2161]布娃娃（扫描线+线段树） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)