当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓
如何培养ui设计师的设计思维?
CV复习:空洞卷积
培训机构学习费用是多少呢?
自定义过滤器和拦截器实现ThreadLocal线程封闭
three.js blur glass effect
MySQL索引的B+树到底有多高?
Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
Crypto Gaming: The Future of Gaming
How to cultivate the design thinking of ui designers?
随机推荐
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
LeetCode 86. Delimited Linked List
Configuration swagger
LeetCode 362. Design Hit Counter
技术人必看!数据治理是什么?它对数据中台建设重要吗?
Custom filters and interceptors implement ThreadLocal thread closure
动态规划之最长回文子串
Nanodlp v2.2/v3.0光固化电路板,机械开关/光电开关/接近开关的接法和系统状态电平设置
爱可可AI前沿推介(8.10)
IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
Dining (web stream)
An enhanced dynamic packet buffer management.论文核心部分
浮动及其特点
LeetCode 146. LRU Cache
It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
47Haproxy Cluster
tommy's spell
Excel函数公式大全—HLOOKUP函数
制品库是什么?