【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
题目描述
给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数
样例输入
4
3
2
4
4
1
3
2
1
样例输出
3
题解
树状数组
WH说是CDQ分治直接把我整蒙了。。。
把所有数按照第一次出现位置从小到大排序,然后扫一遍。此时一定是满足$a_j>a_i$的。
那么只需要求出$(a_j,b_j)$中以前出现过的$b_i$的数目。使用树状数组维护即可。
时间复杂度$O(n\log n)$
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 50010
using namespace std;
struct data
{
int a , b;
bool operator<(const data &x)const {return a < x.a;}
}v[N];
int n , f[N << 1];
inline void add(int x)
{
int i;
for(i = x ; i <= n << 1 ; i += i & -i) f[i] ++ ;
}
inline int query(int x)
{
int i , ans = 0;
for(i = x ; i ; i -= i & -i) ans += f[i];
return ans;
}
int main()
{
int i , x , ans = 0;
scanf("%d" , &n);
for(i = 1 ; i <= n << 1 ; i ++ )
{
scanf("%d" , &x);
if(!v[x].a) v[x].a = i;
else v[x].b = i;
}
sort(v + 1 , v + n + 1);
for(i = 1 ; i <= n ; i ++ ) ans += query(v[i].b - 1) - query(v[i].a - 1) , add(v[i].b);
printf("%d\n" , ans);
return 0;
}
【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组的更多相关文章
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...
- [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组
Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次 ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...
- [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)
传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- 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 ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
随机推荐
- SpringBoot学习11:springboot异常处理方式1(自定义异常页面)
SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制.一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求.在 sprin ...
- LeetCode804. Unique Morse Code Words
题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 ...
- Spring Cloud 入门 Consul-Server服务注册
前面见过 Eureka服务注册,需要单独启用一个springboot项目 :这里介绍一个 spring cloud consul 服务, 只需要安装相关客户端,启动它就行: 1.安装Consul(以 ...
- Linux文件服务器实战(系统用户)
ftp匿名用户设置完成之后任何人都可以访问服务器端文件,目录,甚至可以修改删除文件和目录,,那如何存放私密文件并保证文件或者目录专属于拥有者呢,就需要使用vsftp系统用户来实现了. 1.在linux ...
- (二)、Python 基础
Python入门 一.第一句Python 在 /home/dev/ 目录下创建 hello.py 文件,内容如下: print "hello,world" 执行 hello.py ...
- PHP小练习题
前几天在百度知道里面看到有位网友询问如何制作一下的小程序:用php语言设计一个小程序,计算今天到达下月的天数.全部输出这些天数,并使得每天的日期以三种颜色循环显示,设置三个表单,让用户选择字体颜色,然 ...
- 【转载】java 客户端链接不上redis解决方案 (jedis)
本文出自:http://blog.csdn.net/lulidaitian/article/details/51946169 出现问题描述: 1.Could not get a resource fr ...
- iOS常用控件-UITableViewCell
一. 封装cell: 1.加载xib文件的两种方式 <方式1> (NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] ...
- 使用postMan测试insert或者update接口
URL : http://localhost:8099/orderVoice/updateAgentLogin?access_token=7f10e803-f886-47df-b3dc-9ed307d ...
- Android 拍照或相册选择照片进行显示缩放位图 Demo
拍照后直接使用 BitmapFactory.decodeStream(...) 进行创建 Bitmap 并显示是有问题的. Bitmap 是个简单对象,它只存储实际像素数据,也就是说,即使原始照片已压 ...