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 ...
随机推荐
- tab选项卡代码
$('.case_header ul li').click(function(){ $(this).addClass('active').siblings().removeClass('active' ...
- Button 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 08_代码块丶继承和final
Day07笔记 课程内容 1.封装 2.静态 3.工具类 4.Arrays工具类 封装 概述 1.封装:隐藏事物的属性和实现细节,对外提供公共的访问方式 2.封装的好处: 隐藏了事物的实现细节 提高了 ...
- Liunx查看后1000行的命令以及查看中间部分
linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...
- CODING 告诉你如何建立一个 Scrum 团队
原文地址:https://www.atlassian.com/agile/scrum/roles 翻译君:CODING 敏杰小王子 Scrum 当中有三个角色:PO(product owner),敏捷 ...
- 使用DOM4J 对xml解析操作
参考自:https://blog.csdn.net/redarmy_chen/article/details/12969219 dom4j是一个Java的XML API,类似于jdom,用来读写XML ...
- 送礼物「JSOI 2015」RMQ+01分数规划
[题目描述] 礼品店一共有N件礼物排成一列,每件礼物都有它的美观度.排在第\(i(1\leq i\leq N)\)个位置的礼物美观度为正整数\(A_I\).JYY决定选出其中连续的一段,即编号为礼物\ ...
- Java实现ZooKeeper的zNode监控
上一篇文章已经完成了ZooKeeper的基本搭建和使用的介绍,现在开始用代码说话.参考 https://zookeeper.apache.org/doc/current/javaExample.htm ...
- Python模拟登录淘宝
最近想爬取淘宝的一些商品,但是发现如果要使用搜索等一些功能时基本都需要登录,所以就想出一篇模拟登录淘宝的文章!看了下网上有很多关于模拟登录淘宝,但是基本都是使用scrapy.pyppeteer.sel ...
- Feign详细构建过程及自定义扩展
探究清楚 feign 的原理,自定义 feign 功能 **spring-cloud-openfeign-core-2.1.1.RELEASE.jar** 中 **HystrixFeign** 的详细 ...