欢迎访问~原文出处——博客园-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 树状数组的更多相关文章

  1. [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$第一次 ...

  2. 【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. 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, ...

  4. [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)

    传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...

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

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

  6. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  7. [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 ...

  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. 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 ...

随机推荐

  1. tf.matmul()和tf.multipy()的区别

    首先我们分析一下下面的代码: import tensorflow as tf import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]]) ...

  2. POJ1258 Agri-Net【最小生成树】

    题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离. 思路: 又是一个最小生成树,因为 ...

  3. npm快捷键

    一.npm基本快捷键 node -v查看安装的nodejs版本,出现版本号,说明刚刚已正确安装nodejs.PS:未能出现版本号,请尝试注销电脑重试: npm -v查看npm的版本号,npm是在安装n ...

  4. python3爬虫一

    1.环境安装: 在py3env(虚拟环境)下安装scrapy,新建项目: pip install scrapy scrapy startproject ArticleSpider cd Article ...

  5. API 开发平台 dreamfactory,参考SAWAGGER,国外厂家,开源,本地与云部署

    API 开发平台,参考SAWAGGER,国外厂家,本地与云部署:参考  http://swagger.io/commercial-tools/ 1.dreamfactory 梦工厂公司  https: ...

  6. mysql启动时报错:Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.pid)

    mysql启动报错Starting MySQL... ERROR! The server quit without updating PID file (/opt/mysql/data/mysql.p ...

  7. MySQL占用IO过高解决方案【转】

    1.日志产生的性能影响: 由于日志的记录带来的直接性能损耗就是数据库系统中最为昂贵的IO资源.MySQL的日志包括错误日志(ErrorLog),更新日志(UpdateLog),二进制日志(Binlog ...

  8. 【转】#ifdef __cplusplus+extern "C"的用法

    时常看到别人的头文件中,有这样的代码: #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #en ...

  9. 出现“error LNK1169: 找到一个或多个多重定义的符号”的原因

    或许,有人真的会这样写程序吧...所以才会碰到如下哥们提出的问题. https://zhidao.baidu.com/question/131426210.html 出现这种问题的原因链接中的最佳答案 ...

  10. sublime汉化步骤记录

    1.下载sublime编辑器,下载地址:http://www.sublimetext.com/ 2.安装sublime 3.首先安装“Package Control”(如果已经安装过可以跳过此步骤) ...