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+50;
const int mod = 1e9+7;
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<=2*n;i+=i&(-i)){sum[i]+=add;}
}
int qry(int x){int ret=0;for(int i=x;i>=1;i-=i&(-i)){ret+=sum[i];}return ret;
}
int main(){scanf("%d",&n);for(int i=1;i<=2*n;i++){scanf("%d",&a[i]);}for(int i=1;i<=2*n;i++){if(!vis[a[i]]){upd(i,1);vis[a[i]]=i;}else {int s=qry(i-1)-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]],-1);}}printf("%d\n",ans);return 0;
}

转载于:https://www.cnblogs.com/jianrenfang/p/7253872.html

Why Did the Cow Cross the Road III(树状数组)相关推荐

  1. 【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 样例输 ...

  2. [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$各处现过$2$次,$i$第一次出现 ...

  3. bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III(Pu1 2018.10.1)

    算法:dfs,连通块 难度:NOIP 题解:搜索出每个连通块,找到每个连通块里有几头牛.然后每个连通块内牛数量相乘,求和即为正解 不清楚luogu题解中的代码明明MLE了,却还能AC??? 代码如下: ...

  4. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

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

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

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

    本来想做一道搜索 最后发现是一道并查集的水题 如果两个邻格之间没有路 就把他们合并就行了 #include <cstdio> #include <cstring> #inclu ...

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

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

  8. bzoj4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III(bfs)

    枚举每一个起点,bfs能访问到几头牛即可.复杂度O(n3)O(n^3) #include <cstdio> #include <cstring> #include <al ...

  9. bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III

    题意: 给你一个n*n的地图 某对相邻的点中间有墙. 然后给你n个坐标,问你有多少对坐标不可以互相到达. 题解: 至今为止,提交记录就只有1个WA的-- 随便暴力做一下就好了.

最新文章

  1. Apache 配置的性能调优
  2. Android recovery支持adb shell
  3. Jenkisn之JDK-MVN-ANT-GRADLE
  4. 树莓派2+无线网卡=钓鱼热点
  5. 2 自动递增_有石CAD自动下单,1天工作量1小时完成
  6. 传世经典书丛-UNIX编程艺术
  7. 终端terminal个性化配置
  8. Centos下安装Mongodb
  9. ubuntu下编译ffmpeg并用eclipse调试
  10. 干货满满!!!盘点交互式甘特图控件VARCHART XGantt用户手册、视频教程!
  11. LiveVideoStackCon2021 北京站专访:从上云到创新,视频云的新技术、新场景
  12. 小程序获取oppenid
  13. b区计算机211学校排名,考研B区院校排名
  14. 李宏毅作业十 Generative Adversarial Network生成对抗网络(代码)
  15. 《战狼Ⅱ》豆瓣十二万影评浅析
  16. UE4 HUD相关的笔记
  17. 医院公厕智能化管理需要实现哪些功能
  18. 【中亦安图】运维无小事之一次导致数据丢失的小变更(10)
  19. 项目计划表格甘特图_项目管理:什么是甘特图?
  20. python学习 字典

热门文章

  1. Spring_day01
  2. JButton的setRollover出现的奇怪问题
  3. 读----------空乏的估算
  4. Windows核心编程 第25章 未处理异常和C ++异常(下)
  5. UVA11384正整数序列(把123..变成0的最小步数)
  6. hdu1358 最小循环节,最大循环次数 KMP
  7. 【Qt】打开现有 Qt 项目 ( 打开已存在的项目 | 运行打开的项目 )
  8. 【BLE MIDI】推荐一个 Android 平台开源 MIDI 软件 MidiSheetMusic ( 相关资料 | Android Studio 中导入 Eclipse 源码 )
  9. python+appium+PyCharm==自动化测试APP环境
  10. 2016 Multi-University Training Contest 10 [HDU 5861] Road (线段树:区间覆盖+单点最大小)...