zoukankan      html  css  js  c++  java
  • bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)

    题目描述

    Farmer John is continuing to ponder the issue of cows crossing the road through his farm, introduced in the preceding two problems. He realizes now that the threshold for friendliness is a bit more subtle than he previously considered -- breeds aa and bb are now friendly if |a - b| leq KabK, and unfriendly otherwise. Given the orderings of fields on either side of the road through FJ's farm, please count the number of unfriendly crossing pairs of breeds, where a crossing pair of breeds is defined as in the preceding problems.

    思考过前两个问题后,农民约翰正在继续思考如何对付穿过农场的牛的问题。 他现在意识到,友好的品种的标准比他以前想的稍微微妙一些 -对于品种a,ba,b 如果|a - b| leq KabK,现在是友好的。 否则是不友好的。给定这条路两边的田地的顺序,请计算有多少交叉的不良品种对,其中一对品种在前问题被定义。

    输入输出格式

    输入格式:

    The first line of input contains NN (1 leq N leq 100,0001N100,000) and KK (0 leq K < N0K<N). The next NN lines describe the order, by breed ID, of fields on one side of the road; each breed ID is an integer in the range 1 ldots N1N. The last NN lines describe the order, by breed ID, of the fields on the other side of the road. Each breed ID appears exactly once in each ordering.

    一行包含NN (1 leq N leq 100,0001N100,000)与KK (0 leq K < N0K<N),接下来NN行按顺序描述了小路一旁田地的品种的ID号,每一个ID号是一个在1...N1...N之间的整数。倒数NN行描述了小路另一旁田地的品种的ID号。每个ID只在一个顺序中出现一次

    输出格式:

    Please output the number of unfriendly crossing pairs of breeds.

    请输出不友好的品种对的数量。

    输入输出样例

    输入样例#1: 复制
    4 1
    4
    3
    2
    1
    1
    4
    2
    3
    输出样例#1: 复制
    2

    说明

    In this example, breeds 1 and 4 are unfriendly and crossing, as are breeds 1 and 3.

    题意就是求|a[i].a-a[j].a|>k,a[i].b>a[j].b&&a[i].c<a[j].c || a[i].b<a[j].b&&a[i].c>a[j].c

    满足这个即可,这就是一个三维偏序问题,和陌上花开一样,一维排序,一维CDQ,一维树状数组。

    这样在第三维中,因为是绝对值,所以ans为query(num-k-1)+query(n)-query(num+k),转化一下形式

    一点点小技巧即可。

     1 #include<cstring>
     2 #include<iostream>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<cstdio>
     6 
     7 #define ll long long
     8 #define N 100007
     9 using namespace std;
    10 inline int read()
    11 {
    12     int x=0,f=1;char ch=getchar();
    13     while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
    14     while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    15     return x*f;
    16 }
    17 
    18 int n,k;ll ans;
    19 int tr[N];
    20 struct Node
    21 {
    22     int a,b,c;
    23 }a[N];
    24 
    25 bool cmp1(Node x,Node y)
    26 {
    27     if (x.a==y.a&&x.b==y.b)return x.c<y.c;
    28     if (x.a==y.a) return x.b<y.b;
    29     return x.a<y.a;
    30 }
    31 bool cmp2(Node x,Node y)
    32 {
    33     if (x.b==y.b) return x.c<y.c;
    34     return x.b>y.b;
    35 }
    36 inline int lowbit(int x){return x&(-x);}
    37 inline void update(int x,int num)
    38 {
    39     for (int i=x;i<=n;i+=lowbit(i))
    40         tr[i]+=num;
    41 }
    42 inline int query(int x)
    43 {
    44     int res=0;
    45     for (int i=x;i>=1;i-=lowbit(i))
    46         res+=tr[i];
    47     return res;    
    48 }
    49 void cdq(int l,int r)
    50 {
    51     if (l==r) return;
    52     int mid=(l+r)>>1;
    53     cdq(l,mid),cdq(mid+1,r);
    54     sort(a+l,a+mid+1,cmp2),sort(a+mid+1,a+r+1,cmp2);
    55     int i=l,j=mid+1;
    56     while(j<=r)
    57     {
    58         while(i<=mid&&a[i].b>a[j].b)update(a[i].c,1),i++;
    59         ans+=(ll)(query(a[j].c-k-1)+query(n)-query(min(a[j].c+k,n))),j++;
    60     }
    61     for (int j=l;j<i;j++)update(a[j].c,-1);
    62 }
    63 int main()
    64 {
    65     freopen("1.in","r",stdin);
    66     freopen("1.out","w",stdout);
    67     
    68     n=read(),k=read();
    69     for (int i=1;i<=n;i++)
    70     {
    71         int x=read();
    72         a[x].a=i;
    73     }
    74     for (int i=1;i<=n;i++)
    75     {
    76         int x=read();
    77         a[x].b=i;
    78     }
    79     for (int i=1;i<=n;i++)
    80         a[i].c=i;
    81     sort(a+1,a+n+1,cmp1);
    82     cdq(1,n);
    83     printf("%lld
    ",ans);    
    84 }
  • 相关阅读:
    IOS 给一个文本框,按钮,view加虚线边框
    AFNetworking 使用  基础篇
    IOS——中级篇 --TableView以及Cell
    IOS中级篇 —— picKerView and DatePicKer
    IOS中级篇 ——自动布局 Autolayout  and  VFL
    IOS中级篇—— 多线程--NSOperation
    IOS中级篇 —— Autoresizing
    IOS中级篇 —— 字典转模型
    IOS中级篇 —— 关于深复制和浅复制
    IOS中级篇 —— 日期时间对象
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/7976555.html
Copyright © 2011-2022 走看看