数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair
pair-pair
输入文件:pair-pair.in 输出文件:pair-pair.out 简单对比
时间限制:7 s
内存限制:64 MB
Time Limit : 7000 MS
Memory Limit : 65536 KB
Pair-Pair
Bobo is tired of all kinds of hard LIS (Longest Increasing Subsequence) problems, so he decides to make himself some easier one.
Bobo has n pairs (a1,b1),(a2,b2),…,(an,bn) where 1≤ai,bi≤m holds for all
i. He defines f(i,j) be the length of longest increasing subsequence of
sequence {ai,bi,aj,bj}.
It's clear that 1≤f(i,j)≤4. Bobo would like to know g(k) which is the number of pairs (i,j) where f(i,j)=k.
Note that a sequence labeled with {i1,i2,…,ik} is an increasing subsequence of {a1,a2,…,an} only if:
1≤i1<i2<⋯<ik≤nai1<ai2<⋯<aik
Input
The first line contains 2 integers n,m (1≤n≤105,1≤m≤103).
The i-th of the following n lines contains 2 integers ai,bi (1≤ai,bi≤m).
Output
For each set, 4 integers g(1),g(2),g(3),g(4).
Sample Input
2 4
1 2
3 4
2 1
1 1
1 1
Sample Output
0 3 0 1
4 0 0 0
注意各种特判就好了。
有时间再更新题解吧……
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
const int maxm=;
int n,m;
long long a[];
long long b1[maxm],b2[maxm];
long long b3[maxm],b4[maxm]; struct Node{
int a,b;
Node(int a_=,int b_=){
a=a_;b=b_;
}
}p[maxn]; bool cmp(Node x,Node y){
if(x.a!=y.a)
return x.a<y.a;
return x.b<y.b;
} void Bit_Add(long long *b,int x,int d){
while(x<=m){
b[x]+=d;
x+=x&(-x);
}
} int Bit_Query(long long *b,int x){
int ret=;
while(x){
ret+=b[x];
x-=x&(-x);
}
return ret;
} int rt[maxm],sum[maxn*],ch[maxn*][],cnt;
void Insert(int pre,int &rt,int l,int r,int g,int d){
rt=++cnt;
ch[rt][]=ch[pre][];
ch[rt][]=ch[pre][];
sum[rt]=sum[pre]+d;
if(l==r)return;
int mid=(l+r)>>;
if(mid>=g)Insert(ch[pre][],ch[rt][],l,mid,g,d);
else Insert(ch[pre][],ch[rt][],mid+,r,g,d);
} int Query(int pre,int rt,int l,int r,int a,int b){
if(a>b)return ;
if(l>=a&&r<=b)return sum[rt]-sum[pre];
int mid=(l+r)>>,ret=;
if(mid>=a)ret=Query(ch[pre][],ch[rt][],l,mid,a,b);
if(mid<b)ret+=Query(ch[pre][],ch[rt][],mid+,r,a,b);
return ret;
} void Init(){
memset(a,,sizeof(a));cnt=;
memset(b1,,sizeof(b1));
memset(b2,,sizeof(b2));
memset(b3,,sizeof(b3));
memset(b4,,sizeof(b4));
} int main(){
#ifndef ONLINE_JUDGE
freopen("pair-pair.in","r",stdin);
freopen("pair-pair.out","w",stdout);
#endif
while(scanf("%d%d",&n,&m)!=EOF){
Init();
for(int i=;i<=n;i++)
scanf("%d%d",&p[i].a,&p[i].b);
sort(p+,p+n+,cmp);
for(int i=,last=;i<=n;i++){
long long tot=*(i-),tmp; if(p[i].a<p[i].b){
tmp=Bit_Query(b1,m)-Bit_Query(b1,p[i].b)+Bit_Query(b2,p[i].a-);
a[]+=tmp;tot-=tmp; tmp=Bit_Query(b1,p[i].b)-Bit_Query(b1,p[i].a);
tmp+=Bit_Query(b2,p[i].b-)-Bit_Query(b2,p[i].a-); tmp+=Bit_Query(b3,m)-Bit_Query(b3,p[i].b)+Bit_Query(b4,p[i].a-);
tmp+=Bit_Query(b2,m)-Bit_Query(b2,p[i].b);
for(int j=last+;j<p[i].a;j++)rt[j]=rt[last]; tmp+=Query(rt[],rt[p[i].a-],,m,p[i].b,m); a[]+=tmp;tot-=tmp; Insert(rt[last],rt[p[i].a],,m,p[i].b,); last=p[i].a;a[]+=tot; Bit_Add(b1,p[i].a,);Bit_Add(b2,p[i].b,);
}
else{
tmp=Bit_Query(b3,p[i].b)+Bit_Query(b4,m)-Bit_Query(b4,p[i].a-);
a[]+=tmp;tot-=tmp; tmp=Bit_Query(b1,m)-Bit_Query(b1,p[i].b)+Bit_Query(b2,p[i].a-);
a[]+=tmp;tot-=tmp; a[]+=tot;
Bit_Add(b3,p[i].a,);Bit_Add(b4,p[i].b,);
}
} for(int i=;i<=n;i++){
if(p[i].a!=p[i].b)a[]+=;
else a[]+=;
}
printf("%lld %lld %lld %lld\n",a[],a[],a[],a[]);
}
return ;
}
数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair的更多相关文章
- 数据结构(主席树):COGS 2211. 谈笑风生
2211. 谈笑风生 ★★★★ 输入文件:laugh.in 输出文件:laugh.out 简单对比时间限制:3 s 内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...
- 数据结构(主席树):COGS 2213. K个串
2213. K个串 ★★★★ 输入文件:bzoj_4504.in 输出文件:bzoj_4504.out 简单对比时间限制:20 s 内存限制:512 MB [题目描述] 兔子们在玩k个 ...
- 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...
- luogu4770 [NOI2018]你的名字 (SAM+主席树)
对S建SAM,拿着T在上面跑 跑的时候不仅无法转移要跳parent,转移过去不在范围内也要跳parent(注意因为范围和长度有关,跳的时候应该把长度一点一点地缩) 这样就能得到对于T的每个前缀,它最长 ...
- 【数据结构模版】可持久化线段树 && 主席树
浙江集训Day4,从早8:00懵B到晚21:00,只搞懂了可持久化线段树以及主席树的板子.今天只能记个大概,以后详细完善讲解. 可持久化线段树指的是一种基于线段树的可回溯历史状态的数据结构.我们想要保 ...
- 数据结构(主席树):HZOI 2016 采花
[题目描述] 给定一个长度为n,包含c种颜色的序列,有m个询问,每次给出两个数l,r,表示询问区间[l,r]中有多少种颜色的出现次数不少于2次. 本题强制在线,对输入的l,r进行了加密,解密方法为: ...
- 数据结构(主席树):HDU 4729 An Easy Problem for Elfness
An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65535/65535 K (J ...
- COGS 930. [河南省队2012] 找第k小的数 主席树
主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
随机推荐
- +load,+initialize原理
+load,+initialize原理 1.load 父类的load方法在子类load方法之前调用,分类的load方法在原来类load方法之后调用,依赖类的load方法会在自己之前调用,总之所有的类的 ...
- PowerDesigner15的安装和破解
一.PowerDesigner15的安装 运行安装包,出现如下安装界面
- HTML5 FileReader读取Blob对象API详解
使用FileReader对象,web应用程序可以异步的读取存储在用户计算机上的文件(或者原始数据缓冲)内容,可以使用File对象或者Blob对象来指定所要读取的文件或数据.其中File对象可以是来自用 ...
- jQuery Callback 方法
Callback 函数在当前动画 100% 完成之后执行. jQuery 动画的问题 许多 jQuery 函数涉及动画.这些函数也许会将 speed 或 duration 作为可选参数. 例子:$(& ...
- C++ Reference 的“三位一体”诠释
C++ 是介于汇编语言与高级语言之间的一种“全能”语言.它的能力是其他任何基于VMA(冯-诺曼架构)计算机的高级程序设计语言无法望其项背的,而性能也只有C语言可与之伯仲. 然而长期以来,喜欢C++和憎 ...
- IE6双倍margin间距解决方案
问题:在IE6下如果某个标签使用了float属性,同时设置了其外补丁“margin:10px 0 0 10px”可以看出,上边距和左边距同样为10px,但第一个对象距左边有20px. 解决 ...
- SVN版本控制图标不显示的解决方法~
新系统每次装了svn之后,过了一段时间,安装的软件一多就会出现这个问题,哎,收录一下解决方案! 输入:win+R,输入regedit,调出注册表信息,按下Ctrl+F,在注册表里搜索"She ...
- linux负载均衡
1.linux lvs nat实现负载均衡 添加两块网卡并开启路由管道 > /proc/sys/net/ipv4/ip_forward //开始路由管道 安装ipvsadm yum instal ...
- Meta元素可视区
一.网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" cont ...
- php报警:Strict Standards: Only variables should be passed by reference in
错误原因 因为end函数的原因. end函数: mixed end ( array &$array ) 你可以看到end的参数是一个引用(reference),而你只能把一个变量的引 ...