CodeForces 459D Pashmak and Parmida's problem
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 ≤ rand 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.
Input
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).
Output
Print a single integer — the answer to the problem.
Sample Input
7
1 2 1 1 2 2 1
8
3
1 1 1
1
5
1 2 3 4 5
0 这道题,虽然请教了暾暾和蒙蒙,但毕竟他们只是提供了思路,所以对于我自己居然手撸了线段树这件事情我还是比较满意的。感觉到自己的成长,fighting!!!
这个其实是一个比较简单的线段树问题。线段树的根节点rt保留介于l和r之间的相似的数的个数。
建树的过程:
rt=1,l=1,r=n; tree[rt]=x x表示从j到n,与aj相同的元素的个数。
在建树的过程中,查询从1到i,与a[i]相同的元素的个数k大于1到k-1的个数。
由于建树是从后向前查询。所以建树完成后,查询也刚好完成,并且得到结果。
#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
using namespace std;
const int maxx = +;
map<int,int>mf;
map<int,int>mb;
int fro[maxx];
int bes[maxx];
int num[maxx];
int tree[maxx<<];
void add (int index,int l,int r,int rt)
{
tree[rt]++;
if(l==r) return;
int mid=(l+r)>>;
if(index<=mid)
add(index,l,mid,rt<<);
else
add(index,mid+,r,rt<<|);
}
int query(int inl,int inr,int l,int r,int rt)
{
//cout<<rt<<endl;
// printf("rt: %d l: %d r: %d **%d***%d\n",rt,l,r,inl,inr);
// char a;
//cin>>a;
if(inl>inr) return ;
int re=;
if(inr==r&&inl==l) return tree[rt];
int mid=(l+r)>>;
if(inr<=mid) re= query(inl,inr,l,mid,rt<<);
else if(inl>mid) re= query(inl,inr,mid+,r,rt<<|);
else if(inl<=mid&&inr>mid) {re=(query(inl,mid,l,mid,rt<<)+query(mid+,inr,mid+,r,rt<<|));}
//cout<<re<<endl;
return re;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
mf.clear();
mb.clear();
memset(tree,,sizeof(tree));
for(int i=;i<n;i++)
{
scanf("%d",&num[i]);
}
for(int i=;i<n;i++)
{
mf[num[i]]++;
fro[i]=mf[num[i]];
}
//cout<<fro[2]<<endl;
long long ans=;
for(int i=n-;i>;i--)
{
mb[num[i]]++;
add(mb[num[i]],,n,);
ans+=query(,fro[i-]-,,n,);
}
// for(int i=0;i<28+5;i++)
// cout<<tree[i]<<endl;
//cout<<"ss"<<endl;
// int ans=0;
//for(int i=0;i<n;i++)
// {
// }
printf("%I64d\n",ans);
}
}
CodeForces 459D Pashmak and Parmida's problem的更多相关文章
- 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 D. Pashmak and Parmida's problem
http://codeforces.com/contest/459/problem/D 题意:给你n个数,然后统计多少组(i,j)使得f(1,i,ai)>f(j,n,aj); 思路:先从左往右统 ...
- 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 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
D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megabytes i ...
- 【Codeforces 459D】Pashmak and Parmida's problem
[链接] 我是链接,点我呀:) [题意] 定义两个函数 f和g f(i)表示a[1..i]中等于a[i]的数字的个数 g(i)表示a[i..n]中等于a[i]的数字的个数 让你求出来(i,j) 这里i ...
- codeforces 459 D. Pashmak and Parmida's problem(思维+线段树)
题目链接:http://codeforces.com/contest/459/problem/D 题意:给出数组a,定义f(l,r,x)为a[]的下标l到r之间,等于x的元素数.i和j符合f(1,i, ...
- 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 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预处理出 ...
随机推荐
- django 实现上传文件功能
需求:自己写一个文件上传功能 代码: urls.py from django.conf.urls import url from django.contrib import admin from ap ...
- jQuery代码性能优化
代码优化是一个很重要的开发态度,一点点的优化对于程序来讲可能是微乎其微的,但是把所有的一点都加起来就能够达到水滴石穿的效果,所以要在平时的开发过程中养成优化代码的好习惯. 1. 检测元素是否存在 避免 ...
- 通过form表单上传文件获取后台传来的数据
小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...
- spring+mybatis 多数据源切换
摘自: http://www.oschina.net/code/snippet_347813_12525 1. 代码: DbContextHolder public class DbContextHo ...
- php 获得linux 机器的性能
<?php $str = shell_exec('more /proc/stat'); $pattern = "/(cpu[0-9]? )[\s]+([0-9]+)[\s]+([0-9 ...
- 友元程序集(C# 和 Visual Basic)
友元程序集(C# 和 Visual Basic) Visual Studio 2013 “友元程序集”是一种能够访问其他程序集的 Friend (Visual Basic) 或 internal (C ...
- Android MVP 构架初试
目前讨论MVP MVVM 的架构也来越多,这种构架也很适合Android.研究MVP记录如下 源码地址RxMVP分支Tag02 原有的MVC构架 刚开始接触Android的时候会觉得Android的整 ...
- #define 与 typedef
1.#define:预编译指令,在预编译的时候,进行文本替换. 2.typedef:编译时处理,为类型取一个别名. 3.#define与typedef的顺序是相反的,记住typedef就行了.type ...
- CentOS系统时间同步(NTP)
CentOS系统时间同步的步骤如下: 新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步把当前时区调整为上海就是+8区,想改 ...
- iOS arc和非arc 适用 宏
iOS arc和非arc 适用 宏 1:使用宏 + (void)showAlertWithMessage:(NSString *)messages { dispatch_async(dispatch_ ...