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 树状数组_排序的更多相关文章

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

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...

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

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

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

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

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

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

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

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

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

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

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  2. RDS MySQL 表上 Metadata lock 的产生和处理

    https://help.aliyun.com/knowledge_detail/41723.html?spm=5176.7841698.2.18.vNfPM3

  3. 使用javacv注意点

    由于Android开发需要,使用JavaCV,一开始我配置windows上的Javacv,发现总是出问题,说找不到dll文件.最终发现Opencv库必须解压在C盘根目录下才行. 这个有点坑爹,另外要注 ...

  4. [React] Refactor a connected Redux component to use Unstated

    In this lesson, I refactor a simple Counter component connected to Redux to use Unstated instead. I ...

  5. Linux模块化机制和module_init

    致谢: 微信公众号:嵌入式企鹅圈 每天都新增爱好者关注,感谢大家的支持和大牛们的建议. 本人将竭力出品很多其它优质的原创文章回馈大家的厚爱. 引子:模块化机制长处 模块化机制(module)是Linu ...

  6. mysql之left join、right join、inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...

  7. Mybatis 框架文档 超具体笔记

    1      Mybatis入门 1.1    单独使用jdbc编程问题总结 1.1.1  jdbc程序 Public static void main(String[] args) { Connec ...

  8. ListView实现简单列表

    ListView实现简单列表 效果图: 啥也没干的ListView张这样: fry.Activity01 package fry; import com.example.ListView.R; imp ...

  9. [Apple开发者帐户帮助]五、管理标识符(4)注册一个应用程序组

    您需要注册一个或多个组才能启用应用组. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择操作系统. 在“标识符”下,选择“应用程序组”,然后单击右上角的“添 ...

  10. flash 遮住 div 解决办法

    被遮盖的div 下面的代码   <!--列表菜单-->             <div id="opreationmenu" style="posit ...