数据结构(主席树,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 }
时间: 2024-08-24 19:10:44

数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair的相关文章

数据结构(主席树):COGS 2213. K个串

2213. K个串 ★★★★   输入文件:bzoj_4504.in   输出文件:bzoj_4504.out   简单对比时间限制:20 s   内存限制:512 MB [题目描述] 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一 个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计一次). 兔子们想知道,在这个数字序列所有连续的子串中,按照以上方式统计其所有数字之和,第 k大的和是多少. [输入格式] 第一行,两个整数n和k,分别表示长度为n

数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies from his leader when he did his internship at Tencent. Each watermelon candy has it's sweetness which denoted by an integer number. xiaoxin is very smart

数据结构 - 主席树

查询 $[l,r]$ 区间第 $k$ 小的值. #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1e5; //数据范围 int tot, n, m; int sum[(maxn << 5) + 10], rt[maxn + 10], ls[(maxn << 5) + 10], rs[(maxn <&

关于树状数组套主席树的一些博客

哇仿佛磕了几百年啊; 废话不多说,以下是帮助很大的一些blog: ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树) 主席树全纪录(这个很好) 主席树乱讲(没啥关系,不过有些题目可以刷??) 随笔分类 - 数据结构---主席树(同上) 原文地址:https://www.cnblogs.com/wwtt/p/10099695.html

可持久化数据结构之主席树

转自:http://finaltheory.info/?p=249 HomeACM可持久化数据结构之主席树 06十2013 可持久化数据结构之主席树 Written by FinalTheory on. Posted in ACM 引言 首先引入CLJ论文中的定义: 所谓的“持久化数据结构”,就是保存这个数据结构的所有历史版本,同时利用它们之间的共用数据减少时间和空间的消耗. 本文主要讨论两种可持久化线段树的算法思想.具体实现以及编码技巧. 核心思想 可持久化线段树是利用函数式编程的思想,对记录

【模板】【数据结构】【树】主席树

技巧一:离散去重 for(int i=1;i<=n;i++) scanf("%d",&a[i] ),b[i]=a[i]; sort(b+1,b+n+1); int nn=unique(b+1,b+n+1)-b-1;//假设有x个数,那么nn指针会停在第x+1个数的位置 ,nn及以后的都是重复的元素for(int i=1;i<=n;i++) id[i]=lower_bound(b+1,b+nn+1,a[i])-b;//离散过后的新value 技巧二:可持久化数据结构

数据结构(主席树):HZOI 2016 采花

[题目描述] 给定一个长度为n,包含c种颜色的序列,有m个询问,每次给出两个数l,r,表示询问区间[l,r]中有多少种颜色的出现次数不少于2次. 本题强制在线,对输入的l,r进行了加密,解密方法为: l = l' xor lastans r = r' xor lastans 其中l', r'为输入的l和r,xor表示异或,lastans为上一次询问的答案且初始值为0. [输入格式] 第一行三个正整数n,c,m,意义与题目描述中的相同. 第二行n个位于[1,c]内的正整数,表示序列上每个位置的颜色

COGS 930. [河南省队2012] 找第k小的数 主席树

主席树裸板子 #include<cstdio> #include<iostream> #include<algorithm> #define MAXN 100005 #define MAX 2000005 using namespace std; int sum[MAX],l[MAX],mid[MAX],r[MAX],a[MAXN],b[MAXN],n,m,sz,size,root[MAXN],cnt; void build(int &x,int z,int y

[COGS 257]动态排名系统 树状数组套主席树

主席树就是所谓可持久化线段树.目前只会打区间k值操作. 那么带修改的呢?因为主席树目的上也是搞前缀和,所以类比数组操作,套一个树状数组就可以了. 谨以此纪念此类型树套树入门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define pos(i,a,b) for(int i=(a);i<=(b);i++) #define N 51000 #include<