【题意】

给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数。

对于30%的数据,n<=5000。

对于100%的数据,1<=n<=50000(原题写错了哈哈),保证所有的ai、bi、ci分别组成三个1~n的排列。

【解法】

标题已经说了这是偏序,读完题,这就是个四维偏序模板题(位置一维,a,b,c剩下三维)。

解法多多,我用的是CDQ树套树(树套树写的树状数组套替罪羊树,毕竟在我的印象里替罪羊树在随机数据下跑得飞快)。

第一维已经有序,不用我们预处理,这样就可以按第一维(位置)分治,再把第二维(a)排序,剩下的第三维(b)和第四维(c)直接树套树就可以了(我用的是第三维树状数组,第四维平衡树)。

当然写CDQ套CDQ+树状数组应该也可以,然而本鶸渣不会写……

也可以写树套树套树……然而怎么写……

贴个代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define siz(x) ((x)?((x)->size):(0))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int maxn=;
const double A=0.65;
struct node{//Scapegoat Tree
int data,size;
node *lc,*rc,*prt;
node(int d=):data(d),size(),lc(NULL),rc(NULL),prt(NULL){}
inline void refresh(){size=siz(lc)+siz(rc)+;}
}*root[maxn];
struct B{
int id,a,b,c;
bool operator<(const B &a)const{return this->a<a.a;}
}a[maxn],b[maxn];
void CDQ(int,int);
void add(int,int);
void del(int,int);
int query(int,int);
void insert(int);
void erase(int);
int rank(int);
node *insert(node*);
node *find(int);
node *erase(node*);
node *findmax(node*);
void rebuild(node*);
void zorder(node*);
void removetree(node*);
node *rebuild(int,int);
int n,T,cnt,data[maxn];
long long ans=0ll;
int main(){
#define MINE
#ifdef MINE
freopen("partial_order.in","r",stdin);
freopen("partial_order.out","w",stdout);
#endif
scanf("%d",&n);
for(int i=;i<=n;i++)a[i].id=i;
for(int i=;i<=n;i++)scanf("%d",&a[i].a);
for(int i=;i<=n;i++)scanf("%d",&a[i].b);
for(int i=;i<=n;i++)scanf("%d",&a[i].c);
CDQ(,n);
printf("%lld",ans);
#ifndef MINE
printf("\n-------------------------DONE-------------------------\n");
for(;;);
#endif
return ;
}
void CDQ(int l,int r){
if(l>=r)return;
int mid=(l+r)>>;
CDQ(l,mid);
CDQ(mid+,r);
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r){
if(a[i]<a[j])b[k++]=a[i++];
else b[k++]=a[j++];
}
while(i<=mid)b[k++]=a[i++];
while(j<=r)b[k++]=a[j++];
for(int i=l;i<=r;i++){
a[i]=b[i];
if(a[i].id<=mid)add(a[i].b,a[i].c);
else ans+=query(a[i].b,a[i].c);
}
for(int i=l;i<=r;i++)if(a[i].id<=mid)del(a[i].b,a[i].c);
}
void add(int x,int d){
while(x<=n){
T=x;
insert(d);
x+=lowbit(x);
}
}
void del(int x,int d){
while(x<=n){
T=x;
erase(d);
x+=lowbit(x);
}
}
int query(int x,int d){
int ans=;
while(x){
T=x;
ans+=rank(d);
x-=lowbit(x);
}
return ans;
}
void insert(int x){
node *rt=insert(new node(x));
if(rt)rebuild(rt);
}
void erase(int x){
node *rt=erase(find(x));
if(rt)rebuild(rt);
}
int rank(int x){
node *rt=root[T];
int ans=;
while(rt){
if(x<=rt->data)rt=rt->lc;
else{
ans+=siz(rt->lc)+;
rt=rt->rc;
}
}
return ans;
}
node *insert(node *x){
if(!root[T]){
root[T]=x;
return NULL;
}
node *rt=root[T];
for(;;){
if(x->data<rt->data){
if(rt->lc)rt=rt->lc;
else{
rt->lc=x;
break;
}
}
else{
if(rt->rc)rt=rt->rc;
else{
rt->rc=x;
break;
}
}
}
x->prt=rt;
x=NULL;
for(;rt;rt=rt->prt){
rt->refresh();
if(max(siz(rt->lc),siz(rt->rc))>A*rt->size)x=rt;
}
return x;
}
node *find(int x){
node *rt=root[T];
while(rt){
if(x==rt->data)return rt;
else if(x<rt->data)rt=rt->lc;
else rt=rt->rc;
}
return NULL;
}
node *erase(node *x){
if(x->lc&&x->rc){
node *y=findmax(x->lc);
x->data=y->data;
return erase(y);
}
if(!x->lc&&!x->rc){
if(x->prt){
if(x==x->prt->lc)x->prt->lc=NULL;
else x->prt->rc=NULL;
}
else root[T]=NULL;
}
else if(x->lc&&!x->rc){
x->lc->prt=x->prt;
if(x->prt){
if(x==x->prt->lc)x->prt->lc=x->lc;
else x->prt->rc=x->lc;
}
else root[T]=x->lc;
}
else if(!x->lc&&x->rc){
x->rc->prt=x->prt;
if(x->prt){
if(x==x->prt->lc)x->prt->lc=x->rc;
else x->prt->rc=x->rc;
}
else root[T]=x->rc;
}
node *rt=x->prt;
delete x;
x=NULL;
for(;rt;rt=rt->prt){
rt->refresh();
if(max(siz(rt->lc),siz(rt->rc))>A*rt->size)x=rt;
}
return x;
}
node *findmax(node *x){
while(x->rc)x=x->rc;
return x;
}
void rebuild(node *rt){
cnt=;
zorder(rt);
node *x=rebuild(,cnt);
x->prt=rt->prt;
if(rt->prt){
if(rt==rt->prt->lc)rt->prt->lc=x;
else rt->prt->rc=x;
}
else root[T]=x;
removetree(rt);
}
void removetree(node *x){
if(!x)return;
removetree(x->lc);
removetree(x->rc);
delete x;
}
void zorder(node *x){
if(!x)return;
zorder(x->lc);
data[++cnt]=x->data;
zorder(x->rc);
}
node *rebuild(int l,int r){
if(l>r)return NULL;
int mid=(l+r)>>;
node *x=new node(data[mid]);
x->lc=rebuild(l,mid-);
if(x->lc)x->lc->prt=x;
x->rc=rebuild(mid+,r);
if(x->rc)x->rc->prt=x;
x->refresh();
return x;
}

【后记】

这个题是用CDQ+树状数组水掉三维偏序之后的脑洞的产物……貌似在报复社会……

自己真是玩疯了……

COGS 2479 偏序 题解的更多相关文章

  1. COGS 2479. [HZOI 2016]偏序 [CDQ分治套CDQ分治 四维偏序]

    传送门 给定一个有n个元素的序列,元素编号为1~n,每个元素有三个属性a,b,c,求序列中满足i<j且ai<aj且bi<bj且ci<cj的数对(i,j)的个数. 对于100%的 ...

  2. COGS 2479. [HZOI 2016] 偏序 (CDQ套CDQ)

    传送门 解题思路 四维偏序问题,模仿三维偏序,第一维排序,第二维CDQ,最后剩下二元组,发现没办法处理,就继续嵌套CDQ分治.首先把二元组的左右两边分别打上不同的标记,因为统计答案时只统计左边对右边的 ...

  3. BZOJ3262:陌上花开 & 洛谷3810:三维偏序——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3262 https://www.luogu.org/problemnew/show/3810 Desc ...

  4. CDQ分治学习笔记(三维偏序题解)

    首先肯定是要膜拜CDQ大佬的. 题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai​.b_ibi​.c_ ...

  5. COGS 2479 奇怪的姿势卡♂过去 (bitset+折半)

    思路: 此题显然是CDQ套CDQ套树套树 (然而我懒) 想用一种奇怪的姿势卡过去 就出现了以下解法 5w*5w/8的bitset hiahiahia 但是空间会爆怎么办啊- 折半~ 变成5w*2.5w ...

  6. CDQ分治嵌套模板:多维偏序问题

    CDQ分治2 CDQ套CDQ:四维偏序问题 题目来源:COGS 2479 偏序 #define LEFT 0 #define RIGHT 1 struct Node{int a,b,c,d,bg;}; ...

  7. 四维偏序 CDQ套CDQ

    对CDQ深一步的理解 昨天做了一道CDQ,看了一堆CDQ可做的题,今天又做了一道四维偏序 感觉对CDQ的理解又深了一点,故来写一写现在自己对于CDQ的理解 CDQ其实就是实现了这样的一个问题的转化: ...

  8. 【教程】CDQ套CDQ——四维偏序问题

    前言 上一篇文章已经介绍了简单的CDQ分治,包括经典的二维偏序和三维偏序问题,还有带修改和查询的二维/三维偏序问题.本文讲介绍多重CDQ分治的嵌套,即多维偏序问题. 四维偏序问题       给定N( ...

  9. 几道很Interesting的偏序问题

    若干道偏序问题(STL,分块) 找了4道题目 BZOJ陌上花开(权限题,提供洛谷链接) Cogs2479偏序 Cogs2580偏序II Cogs2639偏序++ 作为一个正常人,肯定先看三维偏序 做法 ...

随机推荐

  1. xutils 3.0 post 使用

    github:https://github.com/wyouflf/xUtils3 xUtils3简介 xUtils 包含了很多实用的android工具. xUtils 支持超大文件(超过2G)上传, ...

  2. HTML 个人资料框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Unity3D Animation Curve

    http://docs.unity3d.com/430/Documentation/Manual/UsingAnimCurves.html

  4. Joomla![1.5-3.4.5]反序列化远程代码执行EXP(直接写shell)

    Usage:x.py http://xxx.com # coding=utf-8# author:KuuKi# Help: joomla 1.5-3.4.5 unserialize remote co ...

  5. safari 调用隐藏fileInput

    在safari上,用自定义按钮调用隐藏fileInput,注意点 1. event listener中,不要 return false2. 不要使用display:none,可使用 opacity:0 ...

  6. dubbo 教程

    阿里巴巴dubbo主页:http://code.alibabatech.com/wiki/display/dubbo/Home-zh 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提 ...

  7. 【转】asp.net mvc 页面跳转

    1.使用传统的Response.Redirect例如string url = "/account/create";Response.Redirect(url); 1.Server. ...

  8. android自定义控件(5)-实现ViewPager效果

    对于系统的ViewGroup我们已经是十分熟悉了,最常用的LinearLayout和RelativeLayout几乎是天天要打交道,下面我们就来看看,如何一步一步将其实现: 一.首先当然也是最通常的新 ...

  9. PHP从零开始-笔记-面向对象编程的概念

    面向对象变成的概念 需要一一种不同的方式来考虑如何构造应用程序.通过对象可以在对应用程序所处理的显示任务.过程和思想进行编码是,实施更贴切的建模.OOP方法并不是将应用程序考虑成一个将大量数据从一个函 ...

  10. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...