题目背景

给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数

题目描述

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn.

As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns NN cows, conveniently identified with the integer IDs 1 \ldots N1…N, so there are precisely 2N2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b)(a,b) a "crossing" pair if cow aa's path from entry to exit must cross cow bb's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

输入输出格式

输入格式:

The first line of input contains NN (1 \leq N \leq 50,0001≤N≤50,000), and the next 2N2N lines describe the cow IDs for the sequence of entry and exit points around the field.

输出格式:

Please print the total number of crossing pairs.

输入输出样例

输入样例#1:

4
3
2
4
4
1
3
2
1
输出样例#1:

3

 解:这题用树状数组就可以做出来了,难度一般。先预处理好所有的数字的两个位置。

  我们可以对每段区间左边和右边分别进行求和,并取最小值。

  进行累加就可以得到答案了。   嗯。。。类似求逆序对的方法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define man 50010
#define lo(x) (x&(-x))
#define read(x) scanf("%d",&x)
/*TEST*/
int n;
struct node
{ int x,y;}e[man<<2];
/*B_TREE*/
int c[man<<2];
inline void update(int x,int val)
{ while(x<=2*n)
{ c[x]+=val;
x+=lo(x);
}
return ;
}
inline int query(int x)
{ int ans=0;
while(x>0)
{ ans+=c[x];
x-=lo(x);
}
return ans;
}
inline int calc(int x,int y)
{ int l=query(x);
int r=query(y);
r=r-l;
return min(l,r);//从1位置到左端点,从左端点位置+1到右端点
}
/*SORT*/
inline int cmp(node a,node b)
{ return a.x<b.x;}
int main()
{ read(n);
for(int i=1;i<=2*n;i++)
{ int tmp;read(tmp);
if(e[tmp].x>0)
e[tmp].y=i;
else e[tmp].x=i;
}
sort(e+1,e+1+n,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{ ans+=calc(e[i].x,e[i].y);
update(e[i].x,1);
update(e[i].y,1);
}
printf("%d\n",ans);
return 0;
}

  

洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)的更多相关文章

  1. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

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

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

  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. Why Did the Cow Cross the Road III(树状数组)

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

  5. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

  6. [USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)

    题目大意:给你两个序列,可以序列进行若干次旋转操作(两个都可以转),对两个序列相同权值的地方连边,求最少的交点数 记录某个值在第一个序列的位置,再记录第二个序列中某个值 在第一个序列出现的位置 ,求逆 ...

  7. P3660 [USACO17FEB]Why Did the Cow Cross the Road III G

    Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...

  8. BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

    题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...

  9. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

随机推荐

  1. 重新理理C++:从《c++ primer》开始

    以前学过C++,但是感觉很多东西还是不清不楚,很多问题解决起来啃吧啃吧的.... 即使c++的东西看过,但是这本书看起来速度还是提不上去,确实需要扎实扎实.很多以前只会用的东西,这本书上都讲的很清楚, ...

  2. mongodb sharding集群搭建

    创建虚拟机,如果是使用copy的方式安装系统,记得修改机器名,否则所有的机器名称都一样,会造成安装失败 同时关闭掉防火墙,将所有的机器的时间调成一致,master和slave的heartbeat间隔不 ...

  3. {转载}需要同时设置 noatime 和 nodiratime 吗?

    相信对性能.优化这些关键字有兴趣的朋友都知道在 Linux 下面挂载文件系统的时候设置 noatime 可以显著提高文件系统的性能.默认情况下,Linux ext2/ext3 文件系统在文件被访问.创 ...

  4. PhpStorm 10.0.3 下载安装与汉化

    https://www.7down.com/soft/229568.html 2JA97R55MG-eyJsaWNlbnNlSWQiOiIySkE5N1I1NU1HIiwibGljZW5zZWVOYW ...

  5. Oracle使用startup与startup force启动的区别

    1. startup 就是正常启动数据库,没什么好说的. 2. startup force 是shutdown abort + startup的组合,即强制关闭数据库+ 正常启动数据库,想快速重启数据 ...

  6. bzoj3802: Vocabulary

    Description 给你三个字符串,这些字符串有些单词模糊不可认了,用"?"来代表. 现在你可以用任意英文小写字母来代表它们.要求是使得给定的三个字符串中 所有的"? ...

  7. mysql 8.0 初识

    1 下载并安装mysql 8.0官网下载比较慢,这里选择163的镜像http://mirrors.163.com/mysql/Downloads/MySQL-8.0/下载版本mysql-8.0.14- ...

  8. Clearsigned file isn't valid, got 'NOSPLIT' (does the network require authentication?)

    ubuntu16在运行sudo apt-get update 命令后,报出错误: Clearsigned file isn't valid, got 'NOSPLIT' (does the netwo ...

  9. Oracle 性能调优之:使用 V$SQL_PLAN 视图查询内存中的执行计划

    V$SQL_PLAN视图提供了一种方法,可用于检查仍位于库高速缓存的游标的执行计划.此视图中的信息与 PLAN_TABLE 视图中的信息非常类似.但是,EXPLAIN PLAN 显示的是执行相应语句时 ...

  10. curl获取响应时间

    1.开启gzip请求curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间curl -o /dev/nu ...