数据结构(主席树,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 ...
随机推荐
- Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo
Google自Android4.0出了TextureView,为什么推出呢?就是为了弥补Surfaceview的不足,另外一方面也是为了平衡GlSurfaceView,当然这是本人揣度的.关于Text ...
- (转)system()函数
[C/C++]Linux下system()函数引发的错误 今天,一个运行了近一年的程序突然挂掉了,问题定位到是system()函数出的问题,关于该函数的简单使用在我上篇文章做过介绍: http:/ ...
- Html网页生成Pdf
在http://code.google.com/p/wkhtmltopdf/downloads/list下载安装程序. 1.添加引用 using System.Diagnostics; 添加引用 2. ...
- 2017JAVA必读书籍
1.深入理解Java虚拟机:JVM高级特性与最佳实践 2.Oracle查询优化改写技巧与案例 3.Effective Java 4.Spring3.x企业应用开发实战 5.Spring技术内幕:深入解 ...
- 利用linq快速判断给定数字是否包含在某个段范围内
一.需求: 知道某段范围0x0020~0x007F0x00A0~0x017F0x01A0~0x01CF0x01F0~0x01FF0x0210~0x021F0x1EA0~0x1EFF给定一个值,快速判断 ...
- 使用AutoMapper实现Dto和Model之间自由转换
应用场景:一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.另一方面,当用户请求数据时,我们又需要做相反的工作:将从数据库中查询出来的领域模型以相反的方式转 ...
- 在window系统下配置login.sql
在window系统下配置login.sql 他的位置是登录用户的文件夹,我的win7系统位置是: C:\Users\Administrator 我的login.sql下载地址: http://file ...
- 【vc】14_网络编程_socket编程
1.计算机网络基本知识 最简单的网络程序如图: 提示:IP地址就相当于一个公司的总机号码,端口号就相当于分机号码.在打电话时,拨通总机后,还需要转到分机上. (1)协议 ·为进行网络中的数据交换(通信 ...
- MongoDB 复制集模式Replica Sets
1.概述 复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复. 复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举 产生一个主结点.该主 ...
- linux 下面 opcache 拓展
PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展,只需要在编译安装的时候, 如果你使用--disable-all参数 禁用了默认扩展的构建, 那么必须使用--enable-opcach ...