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,a[i])>f(j,n,a[j]),而且要求i<j,求i和j的种类数。
题解:先预处理一下设map be[a[i]]表示f(1,i,a[i])的值,然后在倒着来设af[a[j]]表示f(j,n,a[j])的值。
然后再用线段树更新一下长度为af[a[j]]的值然后再在树上查询小于be[a[j-1]]长度的一共有多少就行。
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
using namespace std;
const int M = 1e6 + 10;
map<int , int>be , af;
int a[M];
struct TnT {
int l , r , sum;
}T[M << 2];
void pushup(int i) {
T[i].sum = T[i << 1].sum + T[(i << 1) | 1].sum;
}
void build(int l , int r , int i) {
int mid = (l + r) >> 1;
T[i].l = l , T[i].r = r , T[i].sum = 0;
if(l == r) return ;
build(l , mid , i << 1);
build(mid + 1 , r , (i << 1) | 1);
pushup(i);
}
void updata(int pos , int i) {
int mid = (T[i].l + T[i].r) >> 1;
if(T[i].l == T[i].r && T[i].l == pos) {
T[i].sum++;
return ;
}
if(mid < pos) {
updata(pos , (i << 1) | 1);
}
else {
updata(pos , i << 1);
}
pushup(i);
}
int query(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> 1;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
pushup(i);
if(mid < l) {
return query(l , r , (i << 1) | 1);
}
else if(mid >= r) {
return query(l , r , i << 1);
}
else {
return query(l , mid , i << 1) + query(mid + 1 , r , (i << 1) | 1);
}
}
int main() {
int n;
scanf("%d" , &n);
be.clear() , af.clear();
for(int i = 1 ; i <= n ; i++) {
scanf("%d" , &a[i]);
be[a[i]]++;
}
build(1 , n , 1);
long long ans = 0;
be[a[n]]--;
for(int i = n - 1 ; i >= 1 ; i--) {
af[a[i + 1]]++;
updata(af[a[i + 1]] , 1);
if(be[a[i]] > 1)
ans += (long long)query(1 , be[a[i]] - 1 , 1);
be[a[i]]--;
}
printf("%I64d\n" , ans);
return 0;
}
codeforces 459 D. Pashmak and Parmida's problem(思维+线段树)的更多相关文章
- 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 ...
- 【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 459D Pashmak and Parmida's problem
Pashmak and Parmida's problem Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- 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 ...
- BZOJ_2298_[HAOI2011]problem a_线段树
BZOJ_2298_[HAOI2011]problem a_线段树 Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话( ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
随机推荐
- 使用JavaScript的XMLHttpRequest+fromdata 传递blob到后端
需要上传网页录音文件到服务器,写的艰辛,终于好了,C#端的代码失败的留作纪念,JS端也有失败的案例,就不放上来了 JavaScript: var form = new FormData(); form ...
- AUTOSAR学习之RTE - 可运行实体
本文介绍RTE的运行体(runnable). An AUTOSAR component defines one or more "runnable entities". A run ...
- python basemap readshapefile二三事
今天要用到basemap读取shp文件报错,查了很多资料,都没有解决. 先是: fig,ax = plt.subplots(figsize=(15,10)) from mpl_toolkits.bas ...
- gRPC的简单使用
目录 前言 gRPC的简单介绍 基本用法 服务的定义 服务端代码编写 客户端代码编写 运行效果 服务治理(注册与发现) .NET Core 2.x 和 .NET Core 3.0的细微区别 扩展阅读 ...
- 章节十五、7- 配置文件-Console Logging
一.创建xml文件 1.创建xml文件 在项目中我们需要专门建一个文件夹来放xml文件或者是其它文件. 2.然后对文件夹进行命名 3.选择new 其它 4.选择XML File 5.给xml文件命名 ...
- python调用支付宝支付接口
python调用支付宝支付接口详细示例—附带Django demo代码 项目演示: 一.输入金额 二.跳转到支付宝付款 三.支付成功 四.跳转回自己网站 在使用支付宝接口的前期准备: 1.支付宝公 ...
- 100天搞定机器学习|Day22 机器为什么能学习?
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- 机器学习tips
1 为什么随机梯度下降法能work? https://www.zhihu.com/question/27012077中回答者李文哲的解释 2 随机梯度下降法的好处? (1)加快训练速度(2)噪音可 ...
- java优雅注释原则和代码格式列举
一.java的三种注释类型 单行注释:// ...... 块注释:/* ...... */ 文档注释:/** ...... */ 二.指导原则 注释不能美化糟糕的代码,碰到糟糕的代码就重新写吧. 用代 ...
- 消息中间件——RabbitMQ(二)各大主流消息中间件综合对比介绍!
前言 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流的消息中间件,如老牌的Act ...