BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ4994
题意概括
给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数。
n<=100000(这个数据范围是我凑出来的,但是我没试过更小的范围,BZOJ上没写数据范围(截止2017-08-24))
题解
水题,开一个树状数组在线解决。
比如我们顺着扫过去,当到达一个 bj 时,我们求满足条件的 ai,bi 个数,其实就是求 bi~bj 之间有几个数出现一次而且是第一次出现。
所以我们开树状数组维护。
我顺着做过去,对于每一个数字i,在ai的地方+1,到了bi就在ai的地方-1,并统计区间ans,累加即可。
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long LL;
const int N=100000+5;
int n,c[N*2],f[N];
LL ans=0;
int lowbit(int x){
return x&-x;
}
void add(int x,int d){
for (;x<=n*2;x+=lowbit(x))
c[x]+=d;
}
int sum(int x){
int ans=0;
for (;x>0;x-=lowbit(x))
ans+=c[x];
return ans;
}
int main(){
scanf("%d",&n);
memset(c,0,sizeof c);
memset(f,0,sizeof f);
for (int i=1,x;i<=n*2;i++){
scanf("%d",&x);
if (!f[x]){
f[x]=i;
add(i,1);
}
else {
ans+=sum(i-1)-sum(f[x]);
add(f[x],-1);
}
}
printf("%lld",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_树状数组
Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次 ...
- 【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 样例输 ...
- 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 ...
随机推荐
- 使用eclipse创建web项目的项目图文步骤
在eclipse中使用maven ① maven插件:eclipse内置 ②maven插件的设置: [1]installations:指定maven核心程序的位置,不建议使用自带的maven程序而应该 ...
- Linux - rar 压缩
Linux - rar yum -y install libstdc++.so. wget http://rarsoft.com/rar/rarlinux-4.0.1.tar.gz cd rar ma ...
- JavaScript之this,call,apply
this:被调用的上下文对象: apply与call:切换被调用的上下文对象,即 调用时,this被临时性地切换 //demo 1 [call] function forEach(list,callb ...
- 【BUG】websphere找不到类或jar包冲突
来自:http://liuwei1578.blog.163.com/blog/static/49580364200991572642653/ Jar包冲突问题是在大型Java软件开发中经常遇到的问题, ...
- 【Elasticsearch】Elasticsearch在windows下的安装方法
版本 elasticsearch-2.4.4 2.4.4版本下载地址 下载地址1 下载地址2 启动 进入bin目录,双击elasticsearch.bat: 在浏览器中输入http://localho ...
- TCP3次握手和4次挥手及其为什么
TCP 3次握手 客户端向服务器发送一个SYN(包含了SYN,SEQ). 当服务器接收到客户端发过来的SYN时,会向客户端发送一个SYN+ACK的数据包,其实ACK的ack等于上一次发送SYN数据包的 ...
- python set()、len()、type()、保留小数、EOFError
1.集合set() set()存的数据为不重复的,可以用此来过滤重复的元素.可以用&求两个set的交集 c = a&b,用|求两个set的并集 c = a | b 用for i in ...
- Android Retrofit 2.0使用
实例带你了解Retrofit 2.0的使用,分享目前开发Retrofit遇到的坑和心得. 添加依赖 app/build.gradle 1 compile 'com.squareup.retrofit2 ...
- Java 二进制I/O处理
在Java中如何处理文本I/O 使用Scanner类读取文本数据,使用PrintWriter类写文本数据 例子: public class IO { public static void main(S ...
- sql 查询名字中有_的员工
select * from emp where ename like '%\_%' escape '\' ;\可以换作任意的字符 select * from emp where ename like ...