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预处理出 ...
随机推荐
- go语言基础之字符串类型 和 字符与字符串类型的区别
1.字符串类型 示例1: package main //必须有一个main包 import "fmt" func main() { var str1 string str1 = & ...
- jquery操作CSS样式全记录
$(this).click(function(){ if($(this).hasClass(“zxx_fri_on”)){ $(this).removeClass(“zxx_fri_on”); ...
- ECharts学习总结(五):echarts的Option概览
注:下面内容摘自echarts官网,原文地址:http://echarts.baidu.com/doc/doc.html#%E9%80%89%E9%A1%B9 option 图表选项,包含图表实例任何 ...
- grafana-zabbix图形简单配置
连接zabbix数据库 加入dashboard Home--Add--加入dashboad 设置dashboad 设置名字,和标签tag,tag可在输入后回车加入多个 加入简单的一张图,測试能否获取到 ...
- spring autowired还需要在xml中申明bean ?
如果未自动扫描spring管理的类,则需要在xml中申明.如果自动扫描包下的类,则不需要 (如果配置了自动扫描,还是不行还需要进行手动在xml中声明,则就是工程建立的有问题,包的路径等问题)
- netlink error: too many arguments to function 'netlink_kernel_create'
2.6版本的 netlink_kernel_create(&init_net, NETLINK_TEST, 0, NULL, kernel_receive ,THIS_MODULE); 3 ...
- .NET破解之爱奇迪(三)
本教程只能用于学习研究,不可进行任何商业用途.如有使用,请购买正版,尊重他人劳动成果和知识产权! .NET破解之爱奇迪(一) .NET破解之爱奇迪(二) 一打开软件,就看到各种注册和未注册提示信息,就 ...
- 河流Shader
原地址:http://www.unity蛮牛.com/blog-2321-336.html Shader "Custom/TextureEffect" { Properties { ...
- [转发]using的几种用法
1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...
- chrome 如何官网下载谷歌浏览器离线安装包
如何官网下载chrome谷歌浏览器离线安装包 CreateTime--2017年7月4日08:38:30Author:Marydon 参考链接:http://jingyan.baidu.com/a ...