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的种类数。
我们可以用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 (树状数组求逆序数 变形)的更多相关文章
- 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). 解法:分别从左到右,由右到 ...
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- 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 ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- 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 ...
随机推荐
- LintCode-Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B. The characters ...
- 写一个EF的CodeFirst的Demo
写一个EF的CodeFirst的Demo 今天打算写一个关于EF的CodeFirs的一个小Demo.先略说一个EF的三种与数据库,怎么说,叫映射么,好吧,那就这么叫吧,就是一个是ModelFirst就 ...
- windows批处理(cmd/bat)编程详解
reference: http://blog.csdn.net/bingjie1217/article/details/12947327 http://www.cnblogs.com/doit8791 ...
- 2208: [Jsoi2010]连通数 - BZOJ
Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示无边. Output 输出一行一个整数,表示该图 ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...
- 让你网页同时兼容FireFox和IE
CSS 兼容要点:DOCTYPE 影响 CSS 处理 FireFox: div 设置 margin-left, margin-right 为 auto 时已经居中, IE 不行. FireFox: b ...
- 剑指offer--面试题16
#include<stack> //思路:遍历链表过程中,将各个指针入栈,再出栈进行反转 ListNode* ReverseList(ListNode* pHead) { if(pHead ...
- SGU 114
114. Telecasting station time limit per test: 0.25 sec. memory limit per test: 4096 KB Every city in ...
- sublime text3 配置插件包记录
前言: 很多插件已经开始放弃支持ST2了,所以推荐使用ST3,大量的最新插件和最新功能已经不再支持st2了. 下载地址戳这里:http://www.sublimetext.com/3 1.所有插件 易 ...
- Git PHP提交
做了个小的DEMO,可以查看: https://github.com/feixiang/webgit.git 这几天一直在郁闷的事情. Git在shell里面执行得好好的,apache运行用户也改成了 ...