• 数据结构(主席树,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
    ",a[1],a[2],a[3],a[4]);
    122     }
    123     return 0;
    124 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    基于IPV6的数据包分析
    OSPFv3与OSPF的配置
    防火墙技术综合实验
    基于服务器的AAA配置实验
    基于服务器的AAA实验
    ipv6到ipv4隧道6to4(GNS3)
    Intellij IDEA(idea) 设置创建文件自动添加创建人和创建时间
    虎扑盖饭系列的数据爬虫和分析
    优酷评论爬虫
    MySQL聚族索引、辅助索引、回表和索引覆盖
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5588573.html
Copyright © 2020-2023  润新知