4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III

4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 25  Solved: 20
[Submit][Status][Discuss]

Description

给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数

Input

Output

Sample Input

4
3
2
4
4
1
3
2
1

Sample Output

3

刚开始写了个滑动窗口 写到一半发现GG

重新想一想 其实就是从前向后扫 每次询问区间内权值和


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(ll x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int n,w[N],s[N],bit[N];

inline void modify(int x)
{for(;x<=n;x+=(x&-x))bit[x]++;}

inline int query(int x)
{int res=0;for(;x;x-=(x&-x))res+=bit[x];return res;}

bool book[N];

int main()
{
	n=read()<<1;
	register int i,x;
	for(i=1;i<=n;++i)
	{
		x=read();
		if(!book[x])book[x]=1,w[x]=i;
		else s[w[x]]=i-w[x];
	}
	ll ans=0;
	modify(1+s[1]);
	for(i=2;i<=n;++i)if(s[i])
	{
		ans+=query(i+s[i])-query(i);
		modify(i+s[i]);
	}
	print(ans);puts("");
	return 0;
}
/*
4
3 2 4 4 1 3 2 1

3
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值