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. Flask中使用mysql

    Flask中使用mysql 先安装相关模块: pip  install  Flask-MySQL 先准备一下数据库 登录: mysql  -u  root  -p 创建Database和创建Table ...

  2. 【Atcoder】CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning

    [题意]给定只含小写字母的字符串,要求分割成若干段使段内字母重组顺序后能得到回文串,求最少分割段数.n<=2*10^5 [算法]DP [题解]关键在于快速判断一个字符子串是否合法,容易发现合法仅 ...

  3. bzoj 3450 DP

    首先我们设len[i]表示前i位,从第i位往前拓展,期望有多少个'o',那么比较容易的转移 len[i]=len[i-1]+1 s[i]='o' len[i]=0 s[i]='x' len[i]=(l ...

  4. AndroidStudio获得发布版安全码SHA1

    耗了一下午才搞定 在cmd中: 1.打开keytool的目录:即JDK的安装目录 2.输入口令: (E:\tenyears\tenyears\app是keystore文件的目录)

  5. Coursera在线学习---第九节(1).异常数据检测(Anomaly Detection)

    一.如何构建Anomaly Detection模型? 二.如何评估Anomaly Detection系统? 1)将样本分为6:2:2比例 2)利用交叉验证集计算出F1值,可以用F1值选取概率阈值ξ,选 ...

  6. python科学计算整理

    网站: http://bokeh.pydata.org/gallery.html

  7. monkey测试===monkeyrunner测试教程(1)

    1.安装测试环境 jdk 安装与配置 android sdk安装与配置 Python编辑器安装与配置 以上安装请自行百度教程 Monkeyrunner使用方法 http://www.android-d ...

  8. Linux kernel suspend resume学习:2.6.35与3.0.35比较【转】

    转自:http://blog.csdn.net/njuitjf/article/details/18317149 Linux kernel suspend resume学习:2.6.35与3.0.35 ...

  9. Ubuntu终端里面显示路径名称太长,怎么设置变短【转】

    转自:http://blog.csdn.net/id19870510/article/details/8276914 $: sudo vi ~/.bashrc 这个文件记录了用户终端配置 找到 if ...

  10. nodejs 使用redis 管理session

    一.在开发机安装redis并远程连接 因本人的远程开发机配置原因,使用jumbo安装redis 首先登录开发机,并使用jumbo 安装redis:jumbo install redis 查看redis ...