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预处理出 ...
随机推荐
- Linux 查看当前时间和修改系统时间
一.查看和修改Linux的时区 1. 查看当前时区 命令: date -R 2. 修改设置Linux服务器时区 方法 A 命令 : tzselect 方法 B 仅限于RedHat Linux 和 Ce ...
- 【Linux】linux bash shell之变量替换::=句法、=句法、:-句法、-句法、=?句法、?句法、:+句法、+句法
linux bash shell之变量替换::=句法.=句法.:-句法.-句法.=?句法.?句法.:+句法.+句法 linux bash shell之变量替换::=句法.=句法.:-句法.-句法. ...
- Thinkphp错误-phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连
phpMyAdmin 尝试连接到 MySQL 服务器,但服务器拒绝连接.您应该检查配置文件中的主机.用户名和密码,并确认这些信息与 MySQL 服务器管理员所给出的信息一致. 原因有可能是因为修改了m ...
- nGrinder工具进行接口性能测试
1.背景 之前在这篇文章中性能测试初探—接口性能测试介绍过nGrinder,本文将介绍在nGrinder脚本中使用资源文件中数据作为接口参数和解析生成的CSV结果,生成TPS标准差,TPS波动率,最小 ...
- 非阻塞I/O
http://blog.163.com/tyw_andy/blog/static/1167902120099163252164/ 套接口缺省是阻塞的.这一点意味着当发出一个不能立即完成的套接口调用时, ...
- 协议名称处理函数xxxprotoxxx()
为了方便操作,Linux提供了一组用于查询协议的值及名称的函数. xxxprotoxxx()函数: 上面的函数对文件/etc/protocols中的记录进行操作,文件中记录了协议的名称.值和别名等值, ...
- 解决Fiddler不能监听Java HttpURLConnection请求的方法
在默认情况下,Fiddler不能监听Java HttpURLConnection请求.究其原因,Java的网络通信协议栈可能浏览器的通信协议栈略有区别,Fiddler监听Http请求的原理是 在应用程 ...
- webpack 实用配置总结
1.webpack.config.js配置文件为: //处理共用.通用的js var webpack = require('webpack'); //处理html模板 var htmlWebpackP ...
- TensorFlow编译androiddemo
首先是把tensorflow克隆到本地一份. git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git 既 ...
- <续>调度算法补充
cpmpute->executors: 1.从storm配置获取<compoent-id,parallelism>集合 2.storm-task-info 获得<task-id ...