Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb

题目大意:给定一个长度为$2n$的序列,$1$~$n$个出现过两次,$i$第一次出现的位置记为$a_i$,第二次记为$b_i$,求满足$a_i<a_j<b_i<b_j$的个数。

注释:$1\le n\le 10^5$。


想法

这个题有一个非常不一样的地方。

我记得我之前做过的长成这样的题大概都是第一个位置+1,第二个位置-1即可。

这个题我们只能对第一个位置进行操作。

如果第一次碰见了这个数,就将当前位置+1

如果是第二次碰见了这个数,我们先更新答案,然后把这个数第一个出现的位置-1即可。

Code

#include <bits/stdc++.h>
#define N 100010
using namespace std; typedef long long ll;
int tr[N<<2],vis[N],a[N<<1],n;
inline char nc() {static char *p1,*p2,buf[100000]; return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
int rd() {int x=0; char c=nc(); while(!isdigit(c)) c=nc(); while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=nc(); return x;}
inline int lowbit(int x) {return x&(-x);}
void update(int x,int val) {for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=val;}
ll query(int x) {ll ans=0; for(int i=x;i;i-=lowbit(i)) ans+=tr[i]; return ans;}
int main()
{
ll ans=0; n=rd()*2; for(int i=1;i<=n;i++) a[i]=rd(); for(int i=1;i<=n;i++)
if(!vis[a[i]]) update(i,1),vis[a[i]]=i;
else ans+=query(i)-query(vis[a[i]]),update(vis[a[i]],-1);
cout << ans << endl ;
return 0;
}

小结:树状数组是非常有用的啊,要熟练掌握才是。

[bzoj4994][Usaco2017 Feb]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. [BZOJ4989] [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组)

    传送门 发现就是逆序对 可以树状数组求出 对于旋转操作,把一个序列最后面一个数移到开头,假设另一个序列的这个数在位置x,那么对答案的贡献 - (n - x) + (x - 1) #include &l ...

  4. 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, ...

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

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  6. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

  7. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

  8. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  9. [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对

    4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec  Memory Limit: 256 MBSubmit:  ...

随机推荐

  1. AJPFX关于抽象方法和接口

    class Demo_Animal1{ public static void main(String[] args) {                Cat a = new Cat("加菲 ...

  2. CCF|路径解析|Java

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=ne ...

  3. Fresco 源码分析(序)

    1. 为什么要写这个分析的博客 其实关于Fresco的相关内容,大家上网搜索,一般可以找到一大推,但是为什么我还要写关于这个的呢,因为在网上搜索中文和英文的关于fresco的相关知识时,大家只是潜在的 ...

  4. iOS Programming View Controllers 视图控制器

    iOS Programming View Controllers  视图控制器  1.1  A view controller is an instance of a subclass of UIVi ...

  5. 使用Jenkins进行android项目的自动构建(1)

    环境搭建 1. 下载JDK,安装,并将JDK的安装目录加入到环境变量JAVA_HOME,将JDK的bin目录加入到环境变量PATH. 2. 下载Android SDK,解压,并将SDK的安装目录加入到 ...

  6. spring3 上配置quartz 任务调度

    maven配置: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu ...

  7. C/C++ 数组、字符串、string

    1.定义数组时,数组中元素的个数不能是动态的,不能用变量表示(const变量可以),必须是已知的. 2.引用数组时只能引用数组中某个元素,不能引用整个数组. 3.定义二维数组时,若同时全部初始化,则可 ...

  8. python与shell通过微信企业号发送消息

    python与shell通过微信企业号发送信息,脚本来源于网络,做好搬运工,哈哈,相应的参考链接放在末位 shell版本: #!/bin/bash # CropID="xxxx" ...

  9. (转)淘淘商城系列——SSM框架整合之Dao层整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...

  10. nodejs的学习

    nodejs 就是使用js来编写服务端的程序.它的特性是(单线程   速度快   耗内存多  异步   事件驱动) ( 一些技术的解决方案:默认情况下是 1.不支持多核,可以使用cluster 进行解 ...