当前位置:网站首页>Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
2022-08-10 12:05:00 【Snow_raw】
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
题意: 一共有 n n n 季电视剧,每季电视剧有 a i a_i ai 集,有一天, Polycarp 想要搜索第 7 7 7 季第 3 3 3 集时,发现只能搜索到第 3 3 3 季第 7 7 7 集。后来经讯问发现,如果搜索 x x x 季 y y y 集,一旦前面出现过 y y y 季 x x x 集那么 x x x 季 y y y 集永远无法搜索到,给出 n n n 季电视剧以及每季的集数, 问有多少集是存在但我们搜索不到的 ?
思路: 因为每一季的集数都会对小于当前 a i a_i ai 的季产生贡献,例如 a 1 = 8 a_1=8 a1=8,那么如果 2 − 8 2-8 2−8 季存在 1 1 1 集那么都会搜索不到第 1 1 1 集。那么第 1 1 1 季对答案带来的贡献就是 8 − 2 + 1 = 7 8-2+1=7 8−2+1=7 但是如果其中有几季没有第 1 1 1 集,那么就无法产生贡献,那么就不用加上。因此我们可以开一个优先队列将所有数以第一关键字存值,第二关键值存下标存入。一旦当前枚举的 i i i 大于堆顶元素的值,那么就将树状数组中堆顶元素的第二关键字 − 1 -1 −1 ,当用树状数组 a s k ask ask 时也就自然减去了该元素带来的贡献,直接开一个 b b b 数组排序后和优先队列等效。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
const int N = 2e5+10;
int a[N];
PII b[N];
int tr[N];
int n;
int lowbit(int x){
return x&-x;}
void add(int x,int v){
for(int i=x;i<=n;i+=lowbit(i))tr[i]+=v;
}
int ask(int x){
int ans=0;
for(int i=x;i;i-=lowbit(i))ans+=tr[i];
return ans;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];b[i]={
a[i],i};}
sort(b+1,b+1+n);
int cnt=1;
int ans=0;
for(int i=1;i<=n;i++){
while(cnt<=n&&b[cnt].first<i){
add(b[cnt].second,-1);
cnt++;
}
ans+=ask(min(i-1,a[i]));
add(i,1);
}
cout<<ans<<endl;
return 0;
}
边栏推荐
- LeetCode 237. Delete a node in a linked list
- dedecms支持Word内容一键导入
- StarRocks on AWS Review | Data Everywhere Series Event Shenzhen Station ended successfully
- 如何培养ui设计师的设计思维?
- What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch
- Guo Jingjing's personal chess teaching, the good guy is a robot
- Crypto Gaming: The Future of Gaming
- 堪称神级的阿里巴巴“高并发”教程——基础+实战+源码+面试+架构 全包了
- 培训机构学习费用是多少呢?
- 2016,还是到了最后
猜你喜欢

自定义过滤器和拦截器实现ThreadLocal线程封闭

How to do foreign media publicity to grasp the key points

爱可可AI前沿推介(8.10)

技术人必看!数据治理是什么?它对数据中台建设重要吗?

Does face attendance choose face comparison 1:1 or face search 1:N?

22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发

wirshark 常用操作及 tcp 三次握手过程实例分析

Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi

three.js模糊玻璃效果

基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
随机推荐
Crypto Gaming: The Future of Gaming
three.js blur glass effect
Mysql—— 内连接、左连接、右连接以及全连接查询
odps sql 不支持 unsupported feature CREATE TEMPORARY
Data Analysis of Time Series (5): Simple Prediction Method
可视化服务编排在金融APP中的实践
神经网络学习-正则化
第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
LeetCode 146. LRU Cache
协程与任务
娄底疾控中心实验室设计理念说明
培训机构学习费用是多少呢?
IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
可视化服务编排在金融APP中的实践
Highways「建议收藏」
[List merge] Combine multiple lists into one list
Solve the idea that unit tests cannot use Scanner
部署项目半途而废后续
An enhanced dynamic packet buffer management. The core part of the paper
多线程下自旋锁设计基本思想