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预处理出 ...
随机推荐
- [Todo]对于thrift和protobuf比较好的描述
比较跨语言通讯框架:thrift和Protobuf 全部thrift protobuf 前两天想在微博上发表一个观点:在现在的技术体系中,能用于描述通讯协议的方式很多,xml,json,protobu ...
- C#实现两个数据库之间的数据上报
用VS2008实现本地数据库上传数据到远程数据.数据能够是一个表,或一个表的部分数据.或查询数据.或数据编辑后上传. 其他VS版本号.复制当中代码就能够.未使用其他不论什么插件.有具体凝视. 单独页面 ...
- 浅析ActiveReport中数据下拉列表的交互性
虽说做Cognos已经很久了,Cognos的active report还很少开发过,于是便做了一些小的尝试,下面就以具体实例来分析一下在report studio的活动报表中数据下拉列表和列表报表以及 ...
- C#应用视频教程2.4 OPENGL虚拟仿真介绍
这一部分我们首先实现视图控制(包括了平移/旋转/缩放),前面我们已经讲过,通过lookat一个函数,或者通过translate+rotate两个函数,都能实现视图的控制(两个函数的方式比较简单,但是通 ...
- Dreamweaver界面主要菜单功能介绍
启动界面有四个功能:主要使用新建HTML,其中HTML有很多版本,由于国内IE6.0占据了将近百分之七十的比例,所以最新的HTML1.1对他支持的不好,我们主要使用XHTML 1.0 Transiti ...
- sed 命令编辑文本
1.sed 概述 sed 是一个非交互式文本编辑器.它能够对文本文件和标准输入进行编辑,标准输入能够是来自键盘输入.文件重定向.字符串.变量.甚至来自于管道文本. 2.sed工作流程简述 sed在处理 ...
- STL - 函数作为算法的参数
函数作为参数,相当于C++的函数指针, C#的委托 for_each函数参数: #include <iostream> #include <algorithm> #includ ...
- PL/SQL 之 基础
PL/SQL(Procedural Language extensions to SQL)是Oracle 对标准 SQL 语言的过程化扩展,是专门用于各种环境下对 Oracle 数据库进行访问和开发的 ...
- Mac下终端显示多彩化
终端使用 ls -G 自已添加 Gnu 颜色配置 alias 使用 基本用法: alias 的基本使用方法为:alias 新的命令='原命令 -选项/参数'.举例说明,alias l=‘ls -lsh ...
- 微信群的id
今天网速慢了,竟然把微信群的id卡出来了,记录一下. 格式应该是一个像QQ群一样的数字,然后+@chatroom 看图! 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论.