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,数一下点即可.
方法二:
对于每个数存一下左端点 $l[a]$ 与右端点 $r[a]$, 按照左端点从小到大进行排序.
枚举到每个数字,就将左端点对应的桶 ++, 右端点对应的桶 --,每次对答案的更新为 $sum[l]$ - $sum[r]$.
为什么呢 ?
假设左端点的 $sum$ 为 $a$, 右端点的 $sum$ 为 $b$.
那么, 说明在左端点这个位置上有 $a$ 个数字区间已经启动,却没有终止(感性理解一下).
而 $a-b$ 则代表在 $[l[a],r[a]]$ 区间中终止的个数. 这不就是会对该区间有贡献的个数吗 ?
前缀和用树状数组维护即可.
code:
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 200001
using namespace std;
int arr[maxn],l[maxn],r[maxn],A[maxn],C[maxn];
int cmp(int a,int b)
{
return l[a]<l[b];
}
int lowbit(int t)
{
return t&(-t);
}
void update(int x,int delta)
{
while(x<maxn) C[x]+=delta,x+=lowbit(x);
}
int query(int x)
{
int tmp=0;
while(x>0) tmp+=C[x],x-=lowbit(x);
return tmp;
}
int main(){
// setIO("input");
int n,m;
scanf("%d",&n),m=(n<<1) ;
for(int i=1,a;i<=m;++i)
{
scanf("%d",&a);
if(l[a])
r[a]=i;
else
l[a]=i;
}
for(int i=1;i<=n;++i) A[i]=i;
sort(A+1,A+1+n,cmp);
int ans=0;
for(int i=1;i<=n;++i)
{
int cur=A[i],a;
a=query(l[cur])-query(r[cur]);
ans+=a;
update(l[cur],1),update(r[cur],-1);
}
printf("%d\n",ans);
return 0;
}
bzoj 4994: [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 树状数组
题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 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$第一次 ...
- [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)
传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...
- 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 ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- [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 ...
- 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 ...
随机推荐
- hdu_1040_As Easy As A+B_201308191751
As Easy As A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 洛谷——P2871 [USACO07DEC]手链Charm Bracelet
https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...
- Spring MVC-控制器(Controller)-多动作控制器(Multi Action Controller)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_multiactioncontroller.htm 说明:示例基于Spring M ...
- 改进MySQL Order By Rand()的低效率
Author:flymorn Source:飘易Categories:PHP编程 PostTime:2011-1-14 15:35:07 正 文: 最近由于需要研究了一下MYSQL的随机抽取实现方法. ...
- 创建hive整合hbase的表总结
[Author]: kwu 创建hive整合hbase的表总结.例如以下两种方式: 1.创建hive表的同步创建hbase的表 CREATE TABLE stage.hbase_news_compan ...
- iOS 图像处理 - 图像拼接
解决这个问题:将两个图像拼接在一起 前提:须要加入Framework:CoreGraphics.framework 源代码: - (UIImage *) combine:(UIImage*)leftI ...
- win7下code::blocks开发环境
一.环境配置步骤: 下载安装code::blocks; 下载安装MinGW; 在complier settings中, Toolchain executables选择MinGW的安装路径. 完成安装. ...
- 浅析 Linux 中的时间编程和实现原理一—— Linux 应用层的时间编程【转】
本文转载自:http://www.cnblogs.com/qingchen1984/p/7007631.html 本篇文章主要介绍了"浅析 Linux 中的时间编程和实现原理一—— Linu ...
- 【专题】概率期望DP
11.22:保持更新状态:主要发一些相关的题目和个人理解 (P.S.如果觉得简单,可以直接看后面的题目) upd 11.30 更完了 [NO.1] UVA12230 Crossing Rivers ...
- C - Anton and Danik
Problem description Anton likes to play chess, and so does his friend Danik. Once they have played n ...