嘟嘟嘟

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

首先要满足这个条件: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. 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器

    5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...

  2. lxc 容器基础配置篇

    一, 首先配置lxc需要的网卡断 吧eth0复制一份变为br0 配置br0 配置eth0 重启网卡   /etc/init.d/network restart 安装lxc软件 需要epel源--- y ...

  3. Eclipse快捷键--备忘

    [Ct rl+T] 搜索当前接口的实现类 1. [ALT +/]    此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ ...

  4. Android ContentProvider的介绍(很详细)

    博客分类: android进阶   一.ContentProvider的概念 ContentProvider:为存储和获取数据提供统一的接口.可以在不同的应用程序之间共享数据.Android已经为常见 ...

  5. Ansible 介绍和使用

    简介 Ansible是一个简单的自动化运维管理工具,基于Python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用.配置.编排task(持续交付.无宕机更新等).主版 ...

  6. 成长之路--Docker入门

    简介 Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机. Docker版本 Docker 划分为CE 和EE.CE 即社区版(免费,支持周期三个月),EE 即企业版,强调安全,付费使 ...

  7. 图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)

    当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作  把url中的图片文件下载到本地(或者上传到私有云中)  public String uploadUrlToOss ...

  8. 用单例模式解决临界区(CRITICAL_SECTION)的使用问题

    一.前言 最近,在项目中涉及到多线程访问临界资源的问题.为了保护临界资源,可以是使用互斥量或者是使用临界区.由于,我不需要在多进程中同步,又为了效率的考量,所以选择了使用临界区的方式.但是,在使用临界 ...

  9. ssh整合(spring + struts2 + hibernate)xml版

    1.1分层 1.2jar节点 <dependencies> <dependency> <groupId>junit</groupId> <arti ...

  10. js异步流程控制-回调

    f1为耗时操作,f2依赖f1的数据,因此f2必须在f1之后执行: 个人理解是:将f2(回调函数)的代码放在异步函数内部的最后执行,相当于把同步操作的代码融合到异步函数内部的最后: let tag = ...