嘟嘟嘟

首先看到这种序列的问题,我就想到了逆序对,然后就想如何把这道题转化。

首先要满足这个条件:ai <bi。那么我们把所有数按第一次出现的顺序重新赋值,那么对于新的数列,一定满足了ai < bi。

因为要转换成逆序对,所以先出现的数赋成更大的值,得到了ai > bi。

接下来的操作都是在新的序列上进行的:像原来求逆序对一样从后往前扫,则扫到的第一个数实际上是该数第二次出现,这时候统计已经出现的比他小的数的个数,那么这些数和它构成的数对满足要求,累加到答案中。然后为了以后的查询,在树状数组上给这个数所在的位置加1。

如果遇到了第二次出现的数,我们就在树状数组上给这个数所在的位置减1,相当于清除这个数,不用来更新答案。

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 5e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, a[maxn << ];
int id[maxn], cnt = ;
bool vis[maxn];
ll ans = ; int c[maxn];
int lowbit(int x)
{
return x & -x;
}
void add(int pos, int d)
{
for(; pos <= n; pos += lowbit(pos)) c[pos] += d;
}
int query(int pos)
{
int ret = ;
for(; pos; pos -= lowbit(pos)) ret += c[pos];
return ret;
} int main()
{
n = read(); cnt = n;
for(int i = ; i <= (n << ); ++i) a[i] = read();
for(int i = ; i <= (n << ); ++i) if(!id[a[i]]) id[a[i]] = cnt--; //倒序赋值
for(int i = (n << ); i; --i)
{
if(!vis[id[a[i]]])
{
vis[id[a[i]]] = ;
ans += query(id[a[i]]);
add(id[a[i]], );
}
else add(id[a[i]], -);
}
write(ans), enter;
return ;
}

[USACO17FEB]Why Did the Cow Cross the Road III G的更多相关文章

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

  2. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】

    题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...

  3. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

  4. P3660 [USACO17FEB]Why Did the Cow Cross the Road III G

    Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...

  5. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  6. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  7. 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G

    //神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...

  8. [Luogu3659][USACO17FEB]Why Did the Cow Cross the Road I G

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

  9. [USACO17FEB]Why Did the Cow Cross the Road III S

    题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...

随机推荐

  1. APP在用户设备发生crash,应该怎么修复

    Crash原因 Crash原因有共性,归纳起来有: 内存管理错误 程序逻辑错误  SDK错误 (部署版本< 编译版本) 主线程阻塞 内存管理错误 内存管理是iPhone开发所要掌握的最基本问题, ...

  2. BaaS_后端即服务 RESTful

    码云coding API https://open.coding.net/ Swagger 官网用VPN能流畅打开,但它自己的基于web的编辑器不行 用来设计RESTful API LeanCloud ...

  3. unity项目架构

    Unity 游戏框架搭建 (一) 概述Unity 游戏框架搭建 (二) 单例的模板Unity 游戏框架搭建 (三) MonoBehaviour单例的模板Unity 游戏框架搭建 (四) 简易有限状态机 ...

  4. 强哥的分享--如何使用Spring Boot做一个邮件系统

    http://springboot.fun/ actuator是单机.集群环境下要使用Spring Boot Admin将各个单机的actuator集成越来 mvn clean package -Dm ...

  5. java连接sql server数据库

    1.新建项目,导入包  sqljdbc4.jar或sqljdbc.jar(jdk1.7版本) 2.新建类文件ConnectionDB.java package hello; import java.s ...

  6. git获取别人远程dev分支上的代码

    我们在使用 git clone  xxx.git 下载代码的时候,获取到的只是 master上的代码 假入有个 dev 分支我们想获取上面的代码怎么办! #下载dev分支上的代码并切换到dev分支 g ...

  7. 错误 6 未能找到类型或命名空间名称“BLL”(是否缺少 using 指令或程序集引用?)

    出现这个错误,要确保先引用BLL.dll, 如果引用之后还没有解决这个问题的话,确认一下程序集FrameWork版本与项目FrameWork版本一致.右击程序集属性可以更改.

  8. C#学习笔记13

    1.Task概述:Task是对操作系统线程的抽象,目的是使线程池能高效地管理线程的分配和回收,Task使用的底层线程属于一种共享资源,任务需要互相协作,并及时归还线程,以便用相同的共享资源(线程)满足 ...

  9. hdu 1087 最大递增和

    思路和LIS差不多,dp[i]为i结尾最大值 #include <iostream> #include <string> #include <cstring> #i ...

  10. 2018.10.17NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 +100\) 实际得分:\(100 + 100 + 60\) 辣鸡模拟赛.. 5min切掉T1,看了一下T2 T3,感觉T3会被艹爆因为太原了.. 淦了20 ...