Why Did the Cow Cross the Road III

时间限制: 1 Sec  内存限制: 128 MB
提交: 65  解决: 28
[提交][状态][讨论版]

题目描述

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn. 
As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns N cows, conveniently identified with the integer IDs 1…N, so there are precisely 2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a "crossing" pair if cow a's path from entry to exit must cross cow b's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

输入

The first line of input contains N (1≤N≤50,000), and the next 2N lines describe the cow IDs for the sequence of entry and exit points around the field.

输出

Please print the total number of crossing pairs.

样例输入

4
3
2
4
4
1
3
2
1

样例输出

3
【题意】在一个圆上,顺时针 给出一些数字,每个数字出现两遍,然后数字相同的连边,问多少对边相交。
【分析】我们可以发现按照题目给出的数据顺序,如果两个数的连线相交,那么他俩在一维中的连线一相交,也就是如果某个数两次出现
的位置中间有多少个数 出现了一次,那么就有多少条线与他相交,那我们直接对于每个数统计两个位置之间的出现一次的数就行了。
对于这种数据量较大无法N方解决的统计问题,树状数组一般都可以。对于这个题,从左到右扫,当第一次扫到这个数时,从这个位置
向上lowbit依次+1,当第二次扫到这个数的时候,统计第一次出现的位置到当前位置的sum值,然后从第一次出现的位置向上lowbit
依次-1,表示删除此边。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;
const int mod = 1e9+;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,ans;
int a[N],sum[N],vis[N];
void upd(int x,int add){
for(int i=x;i<=*n;i+=i&(-i)){
sum[i]+=add;
}
}
int qry(int x){
int ret=;
for(int i=x;i>=;i-=i&(-i)){
ret+=sum[i];
}
return ret;
}
int main(){
scanf("%d",&n);
for(int i=;i<=*n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=*n;i++){
if(!vis[a[i]]){
upd(i,);
vis[a[i]]=i;
}
else {
int s=qry(i-)-qry(vis[a[i]]);
//printf("i:%d ai:%d l:%d r:%d\n",i,a[i],qry(vis[a[i]]),qry(i-1));
ans+=s;
upd(vis[a[i]],-);
}
}
printf("%d\n",ans);
return ;
}

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

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

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

  6. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  7. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  8. 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

    题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...

  9. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

随机推荐

  1. 解决SpringSecurity限制iframe引用页面的问题

    使用Spring Security的过程中,需要使用iframe来引入其他域的页面,页面会报X-Frame-Options的错误,试了好几种方法一直未能很好的解决这个问题. 这里涉及到Spring S ...

  2. 【poj1222-又一道开关问题】高斯消元求解异或方程组

    题意:给出一个5*6的图,每个灯泡有一个初始状态,1表示亮,0表示灭.每对一个灯泡操作时,会影响周围的灯泡改变亮灭,问如何操作可以使得所有灯泡都关掉. 题解: 这题和上一题几乎完全一样..就是要输出解 ...

  3. java提取SVN提交log

    http://wiki.svnkit.com/Printing_Out_Repository_History 这个介绍的相当详细. 总之就是要使用SVNKit包,下载地址.http://svnkit. ...

  4. hdu 1395 2^x mod n = 1(暴力题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Oth ...

  5. 【EverydaySport】健身笔记——背部训练

    背部训练大致可以分为两种. 1 下拉式动作 躯干纵向上下位移的动作 典型代表 这样的下拉类动作 针对的是背阔肌 也就是两边像翅膀一样的部分 2 垂直于躯干的方向作用 向内拉 主要针对的是,背部的中部 ...

  6. quazip 在windows msvc 2005 下的编译

    quazip 在windows  msvc 2005 下的编译 http://blog.csdn.net/v6543210/article/details/11661427

  7. SqlServer存储过程中使用事务,示例

    create proc pro_GetProTrans @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId in ...

  8. APP运营

    产品相关术语 APP:application的简写,即应用. 开发商:也叫CP,即ContentProvider内容提供商. 发行商(运营商):代理CP开发出来的产品. 联运:CP和渠道联合运营产品. ...

  9. 《Java编程思想》阅读笔记二

    Java编程思想 这是一个通过对<Java编程思想>(Think in java)进行阅读同时对java内容查漏补缺的系列.一些基础的知识不会被罗列出来,这里只会列出一些程序员经常会忽略或 ...

  10. LinkedList 源码分析

    LinkedList :双向链表结构, 内部存在frist节点 和 last节点.通过改变 首节点和 尾节点的引用来实现新增和修改 有一个内部类: //节点类,内部包括前节点和后节点,和数据项 // ...