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 (树状数组)的更多相关文章

  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. 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 ...

  3. 玲珑学院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 ...

  4. 2019icpc徐州现场赛 H Yuuki and a problem (树状数组套主席树)

    题意 2e5的数组,q个操作 1.将\(a[x]\)改为y 2.求下标l到r内所有的\(a[i]\)通过加法不能构成的最小的值 思路 通过二操作可以知道需要提取l到r内的值及其数量,而提取下标为l到r ...

  5. 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 ...

  6. CF #261 div2 D. Pashmak and Parmida&#39;s problem (树状数组版)

    Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...

  7. 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预处理出  ...

  8. codeforces459D:Pashmak and Parmida's problem

    Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...

  9. CodeForces 459D Pashmak and Parmida's problem

    Pashmak and Parmida's problem Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

随机推荐

  1. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)

    3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 168  Solved:  ...

  3. [Noi2016十连测第五场]二进制的世界

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. Linq 101 工具和源码

    工具如图: 源码: https://git.oschina.net/yudaming/Linq101

  5. ecshop /includes/init.php Arbitrary User Login Vul

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对用户输入的cookie,判断免登的逻辑中存在漏洞,导致黑客可以直接通过 ...

  6. [iOS 利用MapKit和CoreLocation框架打造精简的定位和导航]

    运行效果:            一.利用<CoreLocation/CoreLocation.h>定位 创建变量 CLLocationManager *locationManager , ...

  7. log4j属性详解

    Log4j有三个主要的组件:Loggers(记录器),Appenders  (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使用这三个组件可以轻 ...

  8. MFC 文件对话框

    文件对话框的分类 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如,很多编辑软件像记事本等都有"打开"选项,选择" ...

  9. DropZone

    JavaScript 文件拖拽上传插件 dropzone.js 介绍 February 19, 2014 / 编程指南 dropzone.js 是一个开源的 JavaScript 库,提供 AJAX ...

  10. Delicious Retouch 3

    今天发现一个photoshop的插件:Delicious Retouch 3,磨皮的,特好用,各种磨皮方法的合集.今后都不敢说自己会磨皮了. 插件的界面 插件的使用教程 链接:http://pan.b ...