CF459D Pashmak and Parmida's problem (树状数组)
Codeforces Round #261 (Div. 2)
题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数。i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数。
题解:使用树状数组统计小于某数的元素数量。
我们可以先把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。
核心代码(b和d是map,用来统计元素个数,超碉):
ll farm(){
int i;
ll re=;
b.clear();d.clear();
REPD(i,n){
b[a[i]]++;
update(b[a[i]],);
}
REP(i,n){
d[a[i]]++;
update(b[a[i]],-);
b[a[i]]--;
re+=sum(d[a[i]]-);
//cout<<i<<' '<<d[a[i]]-1<<' '<<sum(d[a[i]]-1)<<' '<<re<<endl;
}
return re;
}
全代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define REPD(i,n) for(i=(n)-1;i>=0;i--)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
const int maxn=;
ll c[maxn];
int a[maxn];
int n; int lowbit(int x){
return x&-x;
} void update(int x,int y){
if(!x)return;
while(x<=n){
c[x]+=y;
x+=lowbit(x);
}
} ll sum(int x){
ll re=;
while(x>){
re+=c[x];
x-=lowbit(x);
}
return re;
} map<int,int> b,d; ll farm(){
int i;
ll re=;
b.clear();d.clear();
REPD(i,n){
b[a[i]]++;
update(b[a[i]],);
}
REP(i,n){
d[a[i]]++;
update(b[a[i]],-);
b[a[i]]--;
re+=sum(d[a[i]]-);
//cout<<i<<' '<<d[a[i]]-1<<' '<<sum(d[a[i]]-1)<<' '<<re<<endl;
}
return re;
} int main(){
int i;
scanf("%d",&n);
REP(i,n) scanf("%d",&a[i]);
printf("%I64d\n",farm());
return ;
}
CF459D 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). 解法:分别从左到右,由右到 ...
- cf459D Pashmak and Parmida's problem
D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力
分析:a^b+2(a&b)=a+b so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...
- 2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)
题意 2e5的数组,q个操作 1.将\(a[x]\)改为y 2.求下标l到r内所有的\(a[i]\)通过加法不能构成的最小的值 思路 通过二操作可以知道需要提取l到r内的值及其数量,而提取下标为l到r ...
- codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)
题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...
- CF #261 div2 D. Pashmak and Parmida's problem (树状数组版)
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...
- 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预处理出 ...
- codeforces459D:Pashmak and Parmida's problem
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
- CodeForces 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
随机推荐
- 【译】用boosting构建简单的目标分类器
用boosting构建简单的目标分类器 原文 boosting提供了一个简单的框架,用来构建鲁棒性的目标检测算法.这里提供了必要的函数来实现它:100% MATLAB实现,作为教学工具希望让它简单易得 ...
- linux中/和/root(~) 和 /home
winodws是森林型目录结构,它有很多根,如C.D.E.F等都是它的根目录,然后在其实创建子目录linux是树型目录结构,它只有一个根就是/目录,然后在/目录在有子目录如/root./home./e ...
- springMVC-自定义数据类型转换器
自定义类型转换器 201603005,今天想了一个问题,Spring中的Conventer是如何实现的,因为他没有绑定类中的属性,它怎么知道要将那个String转换?看了几遍的书也没有找到,后来想想, ...
- CMSEASY /lib/tool/front_class.php、/lib/default/user_act.php arbitrary user password reset vulnerability
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 攻击者通过构造特殊的HTTP包,可以直接重置任意用户(包括管理员)的密码 ...
- C++ Pitfalls 之 reference to an object in a dynamically allocated containter
(留坑待填) Extraction from the C++ Programming Language 4th. ed., Bjarne Stroustrup 31.3.3 Size and Cap ...
- HDU 5726 GCD
传送门 GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- django入门记录 1
步骤: 1 安装python和django 2 创建项目python-admin startproject mysite(此处可以替换) 3 至少需要一个数据表,所以要创建一个表 python ...
- Java反射API使用实例
/** * 访问Class对应的类所包含的注释:getAnnotation();getDelaredAnnotation(); * 访问Class对应的类所包含的内部类:getDecl ...
- Beta版本——第七次冲刺博客
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...
- 实现BPEL4WS演示:教程
http://www.ibm.com/developerworks/cn/education/webservices/ws-bpelws/bpel_tutorial_cn.html 开始 什么是Bus ...