数据结构(主席树,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
注意各种特判就好了。
有时间再更新题解吧……
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 const int maxn=100010; 7 const int maxm=1010; 8 int n,m; 9 long long a[10]; 10 long long b1[maxm],b2[maxm]; 11 long long b3[maxm],b4[maxm]; 12 13 struct Node{ 14 int a,b; 15 Node(int a_=0,int b_=0){ 16 a=a_;b=b_; 17 } 18 }p[maxn]; 19 20 bool cmp(Node x,Node y){ 21 if(x.a!=y.a) 22 return x.a<y.a; 23 return x.b<y.b; 24 } 25 26 void Bit_Add(long long *b,int x,int d){ 27 while(x<=m){ 28 b[x]+=d; 29 x+=x&(-x); 30 } 31 } 32 33 int Bit_Query(long long *b,int x){ 34 int ret=0; 35 while(x){ 36 ret+=b[x]; 37 x-=x&(-x); 38 } 39 return ret; 40 } 41 42 int rt[maxm],sum[maxn*20],ch[maxn*20][2],cnt; 43 void Insert(int pre,int &rt,int l,int r,int g,int d){ 44 rt=++cnt; 45 ch[rt][0]=ch[pre][0]; 46 ch[rt][1]=ch[pre][1]; 47 sum[rt]=sum[pre]+d; 48 if(l==r)return; 49 int mid=(l+r)>>1; 50 if(mid>=g)Insert(ch[pre][0],ch[rt][0],l,mid,g,d); 51 else Insert(ch[pre][1],ch[rt][1],mid+1,r,g,d); 52 } 53 54 int Query(int pre,int rt,int l,int r,int a,int b){ 55 if(a>b)return 0; 56 if(l>=a&&r<=b)return sum[rt]-sum[pre]; 57 int mid=(l+r)>>1,ret=0; 58 if(mid>=a)ret=Query(ch[pre][0],ch[rt][0],l,mid,a,b); 59 if(mid<b)ret+=Query(ch[pre][1],ch[rt][1],mid+1,r,a,b); 60 return ret; 61 } 62 63 void Init(){ 64 memset(a,0,sizeof(a));cnt=0; 65 memset(b1,0,sizeof(b1)); 66 memset(b2,0,sizeof(b2)); 67 memset(b3,0,sizeof(b3)); 68 memset(b4,0,sizeof(b4)); 69 } 70 71 int main(){ 72 #ifndef ONLINE_JUDGE 73 freopen("pair-pair.in","r",stdin); 74 freopen("pair-pair.out","w",stdout); 75 #endif 76 while(scanf("%d%d",&n,&m)!=EOF){ 77 Init(); 78 for(int i=1;i<=n;i++) 79 scanf("%d%d",&p[i].a,&p[i].b); 80 sort(p+1,p+n+1,cmp); 81 for(int i=1,last=0;i<=n;i++){ 82 long long tot=2*(i-1),tmp; 83 84 if(p[i].a<p[i].b){ 85 tmp=Bit_Query(b1,m)-Bit_Query(b1,p[i].b)+Bit_Query(b2,p[i].a-1); 86 a[4]+=tmp;tot-=tmp; 87 88 tmp=Bit_Query(b1,p[i].b)-Bit_Query(b1,p[i].a); 89 tmp+=Bit_Query(b2,p[i].b-1)-Bit_Query(b2,p[i].a-1); 90 91 tmp+=Bit_Query(b3,m)-Bit_Query(b3,p[i].b)+Bit_Query(b4,p[i].a-1); 92 tmp+=Bit_Query(b2,m)-Bit_Query(b2,p[i].b); 93 for(int j=last+1;j<p[i].a;j++)rt[j]=rt[last]; 94 95 tmp+=Query(rt[0],rt[p[i].a-1],1,m,p[i].b,m); 96 97 a[3]+=tmp;tot-=tmp; 98 99 Insert(rt[last],rt[p[i].a],1,m,p[i].b,1); 100 101 last=p[i].a;a[2]+=tot; 102 103 Bit_Add(b1,p[i].a,1);Bit_Add(b2,p[i].b,1); 104 } 105 else{ 106 tmp=Bit_Query(b3,p[i].b)+Bit_Query(b4,m)-Bit_Query(b4,p[i].a-1); 107 a[1]+=tmp;tot-=tmp; 108 109 tmp=Bit_Query(b1,m)-Bit_Query(b1,p[i].b)+Bit_Query(b2,p[i].a-1); 110 a[3]+=tmp;tot-=tmp; 111 112 a[2]+=tot; 113 Bit_Add(b3,p[i].a,1);Bit_Add(b4,p[i].b,1); 114 } 115 } 116 117 for(int i=1;i<=n;i++){ 118 if(p[i].a!=p[i].b)a[2]+=1; 119 else a[1]+=1; 120 } 121 printf("%lld %lld %lld %lld\n",a[1],a[2],a[3],a[4]); 122 } 123 return 0; 124 }
转载于:https://www.cnblogs.com/TenderRun/p/5588573.html
数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair相关推荐
- 数据结构(主席树):COGS 2211. 谈笑风生
2211. 谈笑风生 ★★★★ 输入文件:laugh.in 输出文件:laugh.out 简单对比 时间限制:3 s 内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如 ...
- 数据结构----主席树
这两天一直在看各种树~划分树,左偏树,主席树,伸展树~~~好乱~~ 一听到主席树这个名字的时候感觉好奇怪,为什么会叫主席树,感觉好难好高大上,所以一直敬而远之,,,,,主席树是一个大牛的拼音缩写HJT ...
- 数据结构 - 主席树
文章目录 好文推荐 求区间第K大 [模板]可持久化线段树 1(主席树) 题目 代码 可持久化数组 [模板]可持久化数组(可持久化线段树/平衡树) 好文推荐 权值线段树.主席树学习 树状结构之主席树 求 ...
- 【主席树】可持久化数组(金牌导航 可持久化数据结构-3)
可持久化数组 金牌导航 可持久化数据结构-3 题目大意 给出一个序列a,让你执行若干操作,操作分为两种: 1.继承第v次操作后把第x个数改成y 2.查询第v次操作的第x个数的值 输入样例 5 10 5 ...
- 浅谈数据结构之主席树(线段树进阶版)
今天看了点主席树的概念,加上飞哥上次讲的,目前对主席树有了大致的了解,简单谈谈吧,不讲代码,只讲思路,日后贴题! Orz高级数据结构发明者主席!!最早在CLJ的课件里第一次看到了这个词,最近做区间第K ...
- COGS 930. [河南省队2012] 找第k小的数 主席树
主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 ...
- NOI数据结构:主席树
主席树详解 主席树详解_西红柿爱炒番茄-CSDN博客 最详细的主席树(不修改,待修改) BZOJ 1901 最详细的主席树(不修改,待修改) BZOJ 1901_Bartholomew_的博客-CSD ...
- SPOJ - COT Count on a tree [LCA+主席树]【数据结构】
题目链接:http://www.spoj.com/problems/COT/en/ -------------------------------------- COT - Count on a tr ...
- hdu 2665(主席树查询区间k大值)
先贴我自己写的代码做模板虽然跟原博主没什么两样.(一开始空间开的4*maxn,交到hdu上一直TLE很奇怪) #include<bits/stdc++.h> using namespace ...
- [BZOJ4556][TJOI2016HEOI2016]字符串(二分答案+后缀数组+RMQ+主席树)
4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1360 Solved: 545 ...
最新文章
- 如何安装rabbitmq
- 5、Scala模式匹配
- JavaScript有限状态机实现方式
- SQL Server 2005 常用数据类型详解
- OpenCASCADE绘制测试线束:图形命令之AIS 查看器——对象命令
- 安装windows时loading files结束就重启_Boot Camp安装windows 10
- .Net Core应用搭建的分布式邮件系统设计
- 揭秘2019双11背后的云网络 – 双11网络架构和洛神系统
- errcode: 41001, errmsg: access_token missing hint: [w.ILza05728877!]
- Redis高并发5-redis数据持久化之企业应用
- python 培训 邹博
- 将World中的向下箭头替换为回车符
- md 生成目录 码云_码云搭建博客
- 【华为云·云筑2020】云学院考卷答案
- AI——六(图层、蒙版)
- SOA、网格计算、云计算与P2P技术
- C语言程序设计笔记(浙大翁恺版) 第七章:函数
- 表白,游戏,跨年,各种节日祝福的link
- ThinkPHP 缓存技术详解 使用大S方法
- python calu()函数_python面向对象
热门文章
- js click与onclick事件绑定,触发与解绑
- 阿里云安装Jdk1.8,So easy!
- Spring Boot 支持 HTTPS 如此简单,So easy!
- 九爷带你部署Mfs分布式文件系统
- 台式机使用笔记本电脑上网解决办法。
- MySQL 5.7都即将停只维护了,是时候学习一波MySQL 8了
- 怎么把多个pdf文件合并成一个pdf?
- Android手机怎么找回微信好友,安卓手机微信好友删除怎么找回?这三种方法真香...
- 如何用python画帆船_简单几步,100行代码用Python画一个蝙蝠侠的logo
- 移动热点服务的属性此计算机,Win10打开移动热点的四种方法