题目链接

题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数。i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数。

我们可以用map预处理出 f(1, i, a[i]) 和 f(j, n, a[j]) ,记为s1[n], s2[n]。

这样就变成求满足 s1[i] > s[j], i < j 情况的数量了,你会发现跟求逆序对一样

分析:

做题的时候想到过逆序数,但是很快放弃了,还是理解不深刻吧,,,233.

看了这个博客以后才明白这些过程:http://www.cnblogs.com/yuiffy/p/3916512.html

使用树状数组统计小于某数的元素数量。

我们可以先把f(1,i,a[i])和f(j,n,a[j])写出来,观察一下,例如样例1:

n=7

A  1  2  1  1  2  2  1

R  4  3  3  2  2  1  1

L  1  1  2  3  2  3  4

其中A为给定的数组,Rj为f(j,n,a[j]),Li为f(1,i,a[i])。

对每个Li,我们要统计的其实就是符合(j>i,且Rj<Li)的Rj的个数。就是这个Li右边有多少个比它小的Rj。

这样我们可以用树状数组,把Rj各个数的数量全存到树状数组里,例如这个样例就是4有1个,3有2个,2有2个,1有2个。然后从左到右遍历Li,每次从树状数组里删掉Rj,并且求sum(Li-1),也就是树状数组中1~Li-1的和,也就是比Li小的元素个数。

例如这个样例,到L3时,树状数组里记了2个1和2个2(1个4和2个3在之前被删掉了),L3=2,则sum(L3-1)=sum(2)=1的数量+2的数量=3。ans+=3。

其实从左到右加一遍以后,树状数组是这个样子的:

下标 1  2  3  4

值    2  2  2  1

不能不说这个过程还是挺难想的

时间复杂度是O(n*log(n));

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <algorithm>
#define LL __int64
const int maxn = +;
using namespace std;
int c[maxn], a[maxn], n; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int d)
{
while(x <= n)
{
c[x] += d;
x +=lowbit(x);
}
}
LL sum(int x)
{
LL ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void slove()
{
map<int, int>x, y;
LL ans = ;
int i;
memset(c, , sizeof(c)); for(i = ; i <= n; i++)
{
scanf("%d", &a[i]);
x[a[i]] ++;
add(x[a[i]], ); //其实树状数组里的下标代表个数,数组里的数代表扫过去的依次产生的该个数的数量。
}
for(i = ; i <= n; i++)
{
y[a[i]] ++; //要查找的数量++
add(x[a[i]], -); //依次把原来的-1
x[a[i]] --; //x[]里也要减去
ans += sum(y[a[i]]-); //求从1到 比当前的个数少一的个数的和。
}
printf("%I64d\n", ans);
} int main()
{
while(~scanf("%d", &n))
{
slove();
}
return ;
}

Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)的更多相关文章

  1. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  2. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  3. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  5. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  6. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  7. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  8. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

随机推荐

  1. win8安装matlab7.0

    win8和win7下安装matlab7.0要注意许多地方,其实安装最新版一般都是没有问题的. 不过最新版太大,校园网下载太难,所以还是用7.0 基本上在百度经验上已经包括了大部分的注意事项了,可以参考 ...

  2. Understand User's Intent from Speech and Text

    http://research.microsoft.com/en-us/projects/IntentUnderstanding/ Understanding what users like to d ...

  3. SVN--VisualSVN server 服务端和 TortoiseSVN客户端的基础使用

    前言 在上一文http://www.cnblogs.com/wql025/p/5177699.html中,我们讲到了使用SVN的第一步,即下载.安装SVN的服务端软件--VisualSVN serve ...

  4. WinForm点击按钮在对应的panel里画图

    panel在form1里,button在form1上方,panel在下面. 主要是在button1的click时间获取panel的画笔. 下面的不行,在panel里获取画笔,然后传到button1,根 ...

  5. EBP与ESP寄存器的使用

    push ebp mov esp,ebp esp是堆栈指针 ebp是基址指针 这两条指令的意思是将栈顶指向ebp的地址 ---------------------------------------- ...

  6. Java Memory Basic

    转自: http://www.blogjava.net/justinchen/archive/2009/justinchen/archive/2009/01/08/248738.html GC and ...

  7. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  8. [转] ADO.NET实体框架引发争论

    转自:http://developer.51cto.com/art/200811/76356.htm 2008-11-11 14:00 朱永光译 infoq 我要评论(0) 一个在ADO.NET实体框 ...

  9. hdu 3123 GCC

    这题分2种情况: 1) n>=m时,k!%m=0(k>=m),所以只需令n=m-1即可: 2) n<m时,正常情况处理即可. ;}

  10. QTP重要功能总结

    以下为QTP最应掌握的.最常用的功能(以下仅提供菜单入口,其他还有很多入口,但功能都是一样的) 1.QTP上方菜单栏->Tools->Object Spy(对象探测器)----多个入口 功 ...