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(j,n,a[j]),预处理完毕,接下来,从左往右扫过去,每次从树状数组中删去a[i],因为i != j,i不能用作后面的统计,然后统计getsum(inc[a[i]]-1),
(inc表示从左到右),即查询比此时的a[i]小的f(j,n,a[j])个数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
#define lll __int64
using namespace std;
using namespace __gnu_cxx;
#define N 1000007
#define M 22 int c[N],a[N];
map<int,int> inc,des;
int n; int lowbit(int x)
{
return x & (-x);
} void modify(int x,int val)
{
while(x <= n)
{
c[x] += val;
x += lowbit(x);
}
} int getsum(int x)
{
int res = ;
while(x > )
{
res += c[x];
x -= lowbit(x);
}
return res;
} int main()
{
int i;
inc.clear();
des.clear();
memset(c,,sizeof(c));
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>=;i--)
{
des[a[i]]++;
modify(des[a[i]],);
}
lll ans = ;
for(i=;i<=n;i++)
{
inc[a[i]]++;
modify(des[a[i]],-);
des[a[i]]--;
ans += getsum(inc[a[i]]-);
}
printf("%I64d\n",ans);
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的种类数. 我们可以用map预处理出 ... 
- 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 #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 ... 
- Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题
		题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ... 
随机推荐
- ArrayList集合
			//在使用ArrayList时别忘了引用命名空间 using System.Collections;//首先得导入命名空间 //01.添加方法 add方法 //告诉内存,我要存储内容 ArrayLis ... 
- c++中string的常用函数说明
			string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ... 
- JS 节流
			作为前端的小白,在做项目的过程中,一般只考虑到实现功能,并没有考虑到性能的问题. 比如说,下拉加载更多的这个功能和resize()是特别耗费性能的.此时就要想到节流了. 节流:就是然一个函数无法在短时 ... 
- JS写返回上一级
			应产品需求,自己的网站上要有返回上一级的需求,几经周折,做个小总结. (1): $("XX").on("click",function(){ wind ... 
- sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户
			现象: sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户 处理办法: 1.Open the Exchange Management Shell 2.输入: New-Mana ... 
- ExtJs中xtype与组件类的对应表
			from:http://blog.163.com/jx_dream/blog/static/117056627201223091021410/ 核心提示:我们在使用 ExtJs 创建组件时最容易理解的 ... 
- 第一个WCF的程序
			第一个WCF的程序,按照书上的基本已经完成,就是创建配置文件那里卡住了,因为书上写的不太全,明天再进行深入调试,输入http://127.0.0.1:3721/calculatorservice/me ... 
- [Android]Android Debug key 的制作
			Android Debug key 的制作 背景 在Android App 开发过程中,我们经常会使用一些第三方的服务,但是很多的第三方服务都会要求我们提供包名,签名安装包,这时候,我们在日常调试时, ... 
- C语言-09-文件操作
			文件类型 文本文件(ASCII) 简单的文本文件,可用任何文字处理程序阅读 二进制文件 包含 在ASCII及扩展ASCII字符中编写的数据或程序指令 的文件,通常图形文件及文字处理程序等计算机程序都属 ... 
- XAlign:用于代码对齐的Xcode插件
			除下面的插件对齐,xcode自带有cmd+v,在没有复制或者剪切的情况下,直接按cmd+v会有着对齐参数标签冒号的作用. XAlign 是一个 Xcode 的实用插件,用于对齐规范代码.除了插件作者 ... 
