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 ...
随机推荐
- mysql5.7 简易修改mysql密码
MySQL 5.7 mysql库的user表中已经不再有password字段,取而代之的为authentication_string修改语法相同,步骤也相同.注意:/etc/my.cnf这个配置文件中 ...
- hdu_1038_Biker's Trip Odometer_201311021643
Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu_1029-Ignatius and the Princess IV_201310180916
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDU 5288
//枚举因子,查找和i最近的左右是i因子的点即可. #include <iostream> #include <cstdio> #include <algorithm&g ...
- HDU 3681
也算难题,难在如何处理有些点可以无限次经过 问题. 这道题,其实很容易想到二分+TSP的状态压缩,但在处理上述问题时,确实没想到.题解是处理每一个Y或G或F点到其他YGF点的距离,BFS,这样就出现一 ...
- UML类图符号解释
在UML类图中,常见的有以下几种关系: 泛化(Generalization)和 实现(Realization) - 父子关系 依赖(Dependency) - 局部变量.方法參数 聚合(Aggre ...
- JCE, Java Cryptography Extension
JCE, Java Cryptography Extension Java 8 JCE下载地址: http://www.oracle.com/technetwork/java/javase/downl ...
- [Tool] Open Multiple Terminal Tabs on npm Start with ttab and npm-run-all
Often times when developing web applications, you need to open multiple tabs to run different script ...
- .NET几大热点问题(.NET人员必读)
下面收集了关于.NET几大热点问题及简要答案,防止我们回答问题不专业的尴尬.同一时候还将一些.NET资源的相关网址罗列一二. 一.什么是.Net?它主要包含什么? .Net是为简化在第三代因特网的高 ...
- luogu2161 [SHOI2009]会场预约
题目大意 随着时间的推移这里有几个任务对应着一段区间.每次要将任务安到时间线上时,要把时间线上已有的与该任务对应区间有交集的区间对应的任务删去.求每次删去的区间个数,以及整个时间线上有几个任务.时间线 ...