洛谷 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 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 NN cows, conveniently identified with the integer IDs 1 \ldots N1…N, so there are precisely 2N2N 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 2N2N 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,b) a "crossing" pair if cow aa‘s path from entry to exit must cross cow bb‘s path from entry to exit. Please help Farmer John count the total number of crossing pairs.

输入输出格式

输入格式:

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

输出格式:

Please print the total number of crossing pairs.

输入输出样例

输入样例#1:

4
3
2
4
4
1
3
2
1

输出样例#1:

3

 解:这题用树状数组就可以做出来了,难度一般。先预处理好所有的数字的两个位置。

  我们可以对每段区间左边和右边分别进行求和,并取最小值。

  进行累加就可以得到答案了。   嗯。。。类似求逆序对的方法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define man 50010
#define lo(x) (x&(-x))
#define read(x) scanf("%d",&x)
/*TEST*/
int n;
struct node
{	int x,y;}e[man<<2];
/*B_TREE*/
int c[man<<2];
inline void update(int x,int val)
{	while(x<=2*n)
	{	c[x]+=val;
		x+=lo(x);
		}
	return ;
	}
inline int query(int x)
{	int ans=0;
	while(x>0)
	{	ans+=c[x];
		x-=lo(x);
		}
	return ans;
	}
inline int calc(int x,int y)
{	int l=query(x);
	int r=query(y);
	r=r-l;
	return min(l,r);//从1位置到左端点,从左端点位置+1到右端点
	}
/*SORT*/
inline int cmp(node a,node b)
{	return a.x<b.x;}
int main()
{	read(n);
	for(int i=1;i<=2*n;i++)
	{	int tmp;read(tmp);
		if(e[tmp].x>0)
	 	    e[tmp].y=i;
	    else e[tmp].x=i;
		}
	sort(e+1,e+1+n,cmp);
	int ans=0;
	for(int i=1;i<=n;i++)
	{	ans+=calc(e[i].x,e[i].y);
		update(e[i].x,1);
		update(e[i].y,1);
		}
	printf("%d\n",ans);
	return 0;
	}

  

时间: 2024-10-04 20:22:01

洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)的相关文章

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

[USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)

题目大意:给你两个序列,可以序列进行若干次旋转操作(两个都可以转),对两个序列相同权值的地方连边,求最少的交点数 记录某个值在第一个序列的位置,再记录第二个序列中某个值 在第一个序列出现的位置 ,求逆序对数量即可 本以为是一道逆序对水题,结果被卡了20分.看了题解才恍然大悟,实际上,序列可以旋转 ≠ 序列成环,由于逆序对的特殊性(并不适用于环),故不能把一个序列单独旋转看成它们的相对移动,正着旋转一个序列≠反着旋转另一个序列(更详细证明可以看洛谷) 所以我们要对两个序列再反着进行一次同样的操作

bzoj 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边数,容易得到方程: 不连边: 连边: 实际是这样的,每个位置如果想连边,就要从能连边的位置之前找最大值,即 直接转移不可取,由于最多只从9个位置转移,我们可以缩减一维,用记录b序列进行到位置 j 的最大连边数,再用树状数组维护的最大前缀和方便转移 1 #include <bits/stdc++.h>

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

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

[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

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

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  比右