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 her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of
n integers a1, a2, ..., an.
Let's denote f(l, r, x) the number of indices
k such that: l ≤ k ≤ r and
ak = x. His task is to calculate the number of pairs of indicies
i, j (1 ≤ i < j ≤ n) such that
f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
The first line of the input contains an integer n
(1 ≤ n ≤ 106). The second line contains
n space-separated integers
a1, a2, ..., an
(1 ≤ ai ≤ 109).
Print a single integer — the answer to the problem.
7
1 2 1 1 2 2 1
8
3
1 1 1
1
5
1 2 3 4 5
0
树状数组写起来更方便。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <cctype>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int mod = 99999997;
const int MAX = 0x3f3f3f3f;
const int maxn = 1000010;
int n, a, b;
int in[maxn], f[maxn], vis[maxn], l[maxn], r[maxn], tt[maxn];
int c[maxn];
int bs(int v, int x, int y) {
while(x < y) {
int m = (x+y) >> 1;
if(in[m] >= v) y = m;
else x = m+1;
}
return x;
}
int main()
{
cin >> n;
for(int i = 0; i < n; i++) {
scanf("%d", &in[i]);
tt[i] = in[i];
}
sort(in, in+n);
int m = unique(in, in+n) - in;
for(int i = 0; i < n; i++) {
f[i] = bs(tt[i], 0, m-1) + 1;
vis[ f[i] ]++;
l[i+1] = vis[ f[i] ];
}
memset(vis, 0, sizeof(vis));
for(int i = n-1; i >= 0; i--) {
f[i] = bs(tt[i], 0, m-1) + 1;
vis[ f[i] ]++;
r[i+1] = vis[ f[i] ];
}
LL sum = 0;
for(int i = 1; i <= n; i++) {
for(int j = r[i]+1; j <= maxn; j += j&-j) sum += c[j];
for(int j = l[i]; j > 0; j -= j&-j) c[j]++;
}
cout << sum << endl;
return 0;
}
CF #261 div2 D. Pashmak and Parmida's problem (树状数组版)的更多相关文章
- 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 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- codeforces 459D - Pashmak and Parmida's problem【离散化+处理+逆序对】
题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...
- 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 216D Spider's Web 树状数组+模拟
题目链接:http://codeforces.com/problemset/problem/216/D 题意: 对于一个梯形区域,假设梯形左边的点数!=梯形右边的点数,那么这个梯形为红色.否则为绿色, ...
- BestCoder Round #65 HDOJ5592 ZYB's Premutation(树状数组+二分)
ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 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 ...
- 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 ...
随机推荐
- [BZOJ4026]dC Loves Number Theory 欧拉函数+线段树
链接 题意:给定长度为 \(n\) 的序列 A,每次求区间 \([l,r]\) 的乘积的欧拉函数 题解 考虑离线怎么搞,将询问按右端点排序,然后按顺序扫这个序列 对于每个 \(A_i\) ,枚举它的质 ...
- JavaWeb简单介绍
服务器端编程 技术种类 Servlet JSP Struts Spring Hibernate EJB Web Service Web服务器 IIS Apache Tomcat (提供对JSP和Ser ...
- 78.Nodejs基础中间件Connect
转自:https://www.cnblogs.com/chris-oil/p/5625437.html 前言 “中间件”在软件领域是一个非常广的概念,除操作系统的软件都可以称为中间件,比如,消息中间件 ...
- 访问 IIS 元数据库失败 解决办法
装了VS2005再装IIS,结果出了些小问题访问IIS元数据库失败思考可能是次序出了问题,解决 1.打开CMD,进入 C:\WINDOWS\Microsoft.NET\Framework\v2.0.5 ...
- 做raid1 经验总结
背景: MHP 3楼一台ThinkServer TS200 老服务器做RAID1,硬盘为: 2块3T RAID 设定里显示: 制作RAID1 只有700G多点可做. 系统安装: 可以正常在700G ...
- Vue常见面试题汇总
Vue框架常见面试题 1.active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么 ...
- tensorflow学习之路---解决过拟合
''' 思路:1.调用数据集 2.定义用来实现神经元功能的函数(包括解决过拟合) 3.定义输入和输出的数据4.定义隐藏层(函数)和输出层(函数) 5.分析误差和优化数据(改变权重)6.执行神经网络 ' ...
- Springboot源码——应用程序上下文分析
前两篇(Spring MVC源码——Root WebApplicationContext 和 Spring MVC源码——Servlet WebApplicationContext)讲述了spring ...
- 洛谷 P2690 接苹果
P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...
- SQL char字段类型排序
我是做的ACCESS时候需要对字段的值进行排序,字段格式是char类型的,但是存的值是数字.现在需要对该字段进行排序. 通过查找,找到以下两种方法,记录下来. 1. 你可以转换成int型再排序 sel ...