[ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\)
\(Description\)
给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足\(a_i<a_j<b_i<b_j\)的\((i,j)\)对数。
- \(N\in [1,10^5]\)
\(\\\)
\(Solution\)
考虑以一个数作为\(i\)出现在答案里,对应的\(j\)应满足\(a_j\in (a_i,b_i),b_j>b_i\)。也就是说,我们需要对每一个数统计它两次出现的位置构成的区间里,有多少个数字是第一次出现。
考虑树状数组的做法,第一次遇到一个数时,在出现位置打标记,记录下这个数第一次出现的位置,便于询问。
当第二次遇到这个数时,直接查询区间\((a_i,b_i)\)的区间和即可。因为这个数已经出现了两次,不能对后续的询问做出贡献了,所以要将之前的打标记处撤销标记。
\(\\\)
\(Code\)
#include<map>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 50010
#define R register
#define gc getchar
using namespace std;
inline int rd(){
int x=0; bool f=0; char c=gc();
while(!isdigit(c)){if(c=='-')f=1;c=gc();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=gc();}
return f?-x:x;
}
int n,ans,p[N];
struct BIT{
int c[N];
inline int lowbit(int x){return x&-x;}
inline void add(int p,int x){for(;p<=n;p+=lowbit(p))c[p]+=x;}
inline int sum(int p){
int res=0;
for(;p;p-=lowbit(p)) res+=c[p];
return res;
}
}bit;
int main(){
n=rd()<<1;
for(R int i=1,x;i<=n;++i){
x=rd();
if(!p[x]) bit.add((p[x]=i),1);
else ans+=bit.sum(i)-bit.sum(p[x]),bit.add(p[x],-1);
}
printf("%d\n",ans);
return 0;
}
[ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)的更多相关文章
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为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 样例输 ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)
Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)
Description 有一幅n*n的方格图,n <=100,每个点上有一个值. 从(1,1)出发,走到(n,n),只能走上下左右. 每走一步花费t,每走三步需要花费走完三步后到达格子的值. 求 ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- 【USACO 2017FEB】 Why Did the Cow Cross the Road III
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...
随机推荐
- dubbo服务的group和version
group 当一个接口有多种实现时,可以用group区分 <!-- dubbo group 使用示例 --> <bean id="demoA" class=&qu ...
- how to get geometry type of layer using IMapServer3 and IMapLayerInfo? (C#)
http://forums.arcgis.com/threads/11481-how-to-get-geometry-type-of-layer-using-IMapServer3-and-IMapL ...
- 自己构建的Lumbda表达式
自己构建的Lumbda表达式 定义接口: package com.stono.lambda; public interface Add { public void add(int x, int y); ...
- CentOS 5.5 介绍
转载:http://www.osyunwei.com/archives/3625.html 操作系统:CentOS 5.5主机名称:wwwIP地址:192.168.21.128网关:192.168.2 ...
- poj3511--A Simple Problem with Integers(线段树求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60441 ...
- vue全局注册与局部注册的写法
vue全局注册是每个实例化的vue都可以使用,而局部则是实例化注册的那个可以用.举个例子,看看写法: <div id="app"> <p>页面载入时,inp ...
- 9种样式CSS3 渐变button集
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
- JDK和TOMCAT的安装与配置环境变量
一.JDK该怎么安装与配置环境变量 步骤1.安装JDK选择安装目录,安装jdk1.8.0_77过程中会出现安装提示. 步骤2.(1)安装jdk随意选择目录 只需把默认安装目录\java之前的目录修改即 ...
- NSArray和NSMutableArray的常用方法 (转)
NSArray和NSMutableArray的常用方法 (转) (2013-09-06 15:13:46) 标签: it 分类: ios编程 初始化方法: 1.init返回一个空数组 2.i ...
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
D. Once Again... You a ...