• HYSBZ - 4991
  • 题意: 第一列 1-n的排列 ,第二列 1-n的排列。  相同数字连边  ,问  有多少组 数字 是有交点的并且 绝对值之差>K
    思路:处理一下 1-n 在第一列的位置,1-n在第二列的位置。按照第一列的位置从小到大排序,然后 进行cdq分治,
    因为现在第一列已经是递增序列了,如果在第二列中出现了递减那么这两个数就有交点,分治解决,递归左区间 
    的必然第一列必然小于递归右区间。所以只处理左区间对右区间的影响,两段小区间分别按照 b 从大到小排序,
    然后 统计 左区间的b  比右区间大的树状数组更新那个数字。然后更新完成之后查询, 右区间当前的数
    (绝对值之差>K无非就是, < x-k  有多少 ,  > x+k有多少)分别 树状数组进行查询,进行完成之后 ,
    数组数组恢复最初状态,回溯 继续处理大区间即可

  • #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define maxn 100010
    struct node
    {
    int num,a,b;
    } data[maxn];
    bool cp(node x,node y)
    {
    return x.a<y.a;
    };
    bool cp2(node x,node y)
    {
    return x.b>y.b;
    };
    int tree[maxn],n,k,x;
    ll ans;
    int lowbit(int x)
    {
    return x&(-x);
    }
    void add(int x,int ad)
    {
    while(x<=n)
    {
    tree[x]+=ad;
    x+=lowbit(x);
    }
    }
    int query(int x)
    {
    int re=0;
    while(x>0)
    {
    re+=tree[x];
    x-=lowbit(x);
    }
    return re;
    }
    void cdq(int l,int r)
    {
    if(l==r)return ;
    int mid=(l+r)>>1;
    cdq(l,mid);
    cdq(mid+1,r);
    sort(data+l,data+1+mid,cp2);
    sort(data+mid+1,data+1+r,cp2);
    int i=l,j=mid+1;
    for(; j<=r; j++)
    {
    while(data[i].b>data[j].b&&i<=mid)
    {
    add(data[i].num,1);
    i++;
    }
    ans+=query(data[j].num-k-1);
    if(data[j].num+k<n)ans+=query(n)-query(data[j].num+k);
    }
    for(j=l; j<i; j++)
    add(data[j].num,-1);
    }
    int main()
    {
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++)
    {
    scanf("%d",&x);
    data[i].num=i;
    data[x].a=i;
    }
    for(int j=1; j<=n; j++)
    {
    scanf("%d",&x);
    data[x].b=j;
    }
    sort(data+1,data+1+n,cp);
    cdq(1,n);
    printf("%lld\n",ans);
    return 0;
    }

      

Why Did the Cow Cross the Road III HYSBZ - 4991 -CDQ-逆序数的更多相关文章

  1. [USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

    题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置, ...

  2. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  3. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  4. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  5. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  6. [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对

    4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec  Memory Limit: 256 MBSubmit:  ...

  7. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  8. 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, intro ...

  9. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

随机推荐

  1. 纯css使用线性渐变实现滚动进度条(来自于微信前端早读课)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 浅谈java中bigInteger用法

    1.赋值: BigInteger a=new BigInteger("1"); BigInteger b=BigInteger.valueOf(1); 2.运算: ① add(); ...

  3. Java的动手动脑(六)

    日期:2018.11.8 星期四 博客期:022 --------------------------------------------------------------------------- ...

  4. gnuradio 创建cos_source

    C++教程 ys_linux@computer:~$ gr_modtool nm kcd Creating out-of-tree module in ./gr-kcd... Done. Use 'g ...

  5. 图书管理系统(无中间件,用装饰器的)-----未基于FORM组件

    目的:实现图书的增删改查 models.py from django.db import models # Create your models here. class Book(models.Mod ...

  6. python(5):scipy之numpy介绍

    python 的scipy 下面的三大库: numpy, matplotlib, pandas scipy 下面还有linalg 等 scipy 中的数据结构主要有三种: ndarray(n维数组), ...

  7. django----常用功能

    request.path_info 获取url地址

  8. hdu3586 树形dp+二分答案

    /* dp[i]表示孤立i结点的费用,二分功率上限w,即dp[i]在选择时不可以选择功率大于w的边 */ #include<bits/stdc++.h> using namespace s ...

  9. linux安装MongoDB

    安装 32bit的mongodb最大只能存放2G的数据,64bit就没有限制 到官网,选择合适的版本下载,本次下载3.4.0版本 解压 tar -zxvf mongodb-linux-x86_64-u ...

  10. shell脚本批量创建用户

    #!/bin/bash DATE=$(date +%F_%T) USER_FILE=user.txt echo_color() { == "green" ];then echo - ...