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 ...
随机推荐
- js中arguments对象和this对象
js中arguments对象和this属性 如果不注重复习,花时间准备的材料毫无意义 arguments对象和this对象都是对象 直接来代码 <!DOCTYPE html> <ht ...
- 《Android编程权威指南》PhotoGallery应用梳理
PhotoGalley是<Android编程权威指南>书中另外一个重要的应用.
- Linux系统的LOG日志文件及入侵后日志的清除
UNIX网管员主要是靠系统的LOG,来获得入侵的痕迹.当然也有第三方工具记录入侵系统的 痕迹,UNIX系统存放LOG文件,普通位置如下: /usr/adm - 早期版本的UNIX/var/adm - ...
- XML之序列化C#实体类,DataTable,List
1. static void Main(string[] args) { #region 实体类 Request patientIn = new Request(); patientIn.System ...
- 【Django】ORM操作#1
目录 一.介绍 概念 由来 优势 劣势 总结 二.Django中的ORM Django项目使用MySQL Model 快速入门 1. AutoField 2. IntegerField 3. Char ...
- cache基本结构
下图为direct mapped set associative fully associative图示 direct mapped,相当于set number为1 fully ...
- SpringBoot 增加 拦截器 判断是否登录
1.创建拦截器 package com.example.demo.interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactor ...
- Spring MVC源码——Servlet WebApplicationContext
上一篇笔记(Spring MVC源码——Root WebApplicationContext)中记录了下 Root WebApplicationContext 的初始化代码.这一篇来看 Servlet ...
- win7安装python开发环境,执行python
在win7上安装python的开发环境是很easy的事情 Step1:下载python安装文件 url:https://www.python.org/download 去这里找到你想要下载的文件 St ...
- (七十一)关于UITableView退出崩溃的问题和滚动究竟部的方法
[TableView退出崩溃的问题] 近期在使用TableView时偶然发如今TableView中数据较多时,假设在滚动过程中退出TableView到上一界面.会引起程序的崩溃.经过网上查阅和思考我发 ...