Why Did the Cow Cross the Road III(树状数组)

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;
}
时间: 2024-10-07 05:04:33

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,求满足ai<aj<bi<bj的对数. n<=100000(这个数据范围是我凑出来的,但是我没试过更小的范围,BZOJ上没写数据范围(截止2017-08-24)) 题解 水题,开一个树状数组在线解决. 比如我们顺着扫过去,当到达一个 bj 时,我们求满足条件的 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 样例输出 3 题解 树状数组 WH说是CDQ分治直接把我整蒙了... 把所有数按照第一次出现位置从小到大排序,然后扫一遍.此时一定是满足$a_j>a_i$的. 那么只需要求出$(a_j,b_j)$中以前出现过的$b_i$的数目.使用树状数组维护即可. 时间复杂度$O(n\log n)$ #include &l

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

传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include <cstdio> #include <cstring> #include <iostream> #define N 200011 #define LL long long using namespace std; int n; int a[N], b[N], pos1[N],

洛谷 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 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 morn

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

传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, 也就是说在两个i之间只有一个j那么对答案的贡献为1,所以可以用树状数组,当第一次出现一个数的时候,那个位置+1,当第二次出现的时候,第一次出现的位置再-1,也就是把它对答案的贡献去掉,同样是树状数组统计答案 #include <cstdio> #include <iostream>

[USACO17FEB]Why Did the Cow Cross the Road III P(CDQ分治)

题意 两列$n$的排列,相同的数连边,如果一对数有交叉且差的绝对值$>k$,则$++ans$,求$ans$ 题解 可以把每一个数字看成一个三元组$(x,y,z)$,其中$x$表示在第一列的位置,$y$表示在第二列的位置,$z$表示权值 两条线交叉,就是$x<x'$且$y>y'$,又要满足差值的绝对值小于等于$k$,就是$|z-z'|<=k$ 于是就转化为了一个三维偏序问题,直接上CDQ 具体细节请看代码 1 // luogu-judger-enable-o2 2 //minamot

Why Did the Cow Cross the Road III HYSBZ - 4991 -CDQ-逆序数

HYSBZ - 4991 题意: 第一列 1-n的排列 ,第二列 1-n的排列.  相同数字连边  ,问  有多少组 数字 是有交点的并且 绝对值之差>K思路:处理一下 1-n 在第一列的位置,1-n在第二列的位置.按照第一列的位置从小到大排序,然后 进行cdq分治,因为现在第一列已经是递增序列了,如果在第二列中出现了递减那么这两个数就有交点,分治解决,递归左区间 的必然第一列必然小于递归右区间.所以只处理左区间对右区间的影响,两段小区间分别按照 b 从大到小排序,然后 统计 左区间的b  比右

洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm has NN crosswalks across it, conveniently numbered 1 \ldots N1…N (1 \leq N \leq 100,0001≤N≤100,000). To allow cows to cross at these crosswalks, FJ in

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cow