当前位置:网站首页>教育Codeforces轮41(额定Div。2)大肠Tufurama
教育Codeforces轮41(额定Div。2)大肠Tufurama
2022-08-10 12:56:00 【Snow_raw】
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
题意: 一共有 n n n Season TV,There are TV shows every season a i a_i ai 集,有一天, Polycarp Want to search for the first 7 7 7 季第 3 3 3 集时,Discovery can only search for the first 3 3 3 季第 7 7 7 集.It was later discovered upon interrogation,如果搜索 x x x 季 y y y 集,Once it has appeared before y y y 季 x x x set then x x x 季 y y y The set can never be searched,给出 n n n Season TV series and the number of episodes per season, Ask how many episodes exist but we couldn't search ?
思路: Because the number of episodes in each season will be less than the current one a i a_i ai quarterly contribution,例如 a 1 = 8 a_1=8 a1=8,那么如果 2 − 8 2-8 2−8 Season exists 1 1 1 Sets will not be searchable then 1 1 1 集.那么第 1 1 1 Ji's contribution to the answer is that 8 − 2 + 1 = 7 8-2+1=7 8−2+1=7 But if there are a few seasons without episodes 1 1 1 集,Then there will be no contribution,Then don't add it.So we can open a priority queue to store all numbers with the first key,The second key value is stored in the subscript.Once currently enumerated i i i greater than the value of the top element of the heap,Then put the second key of the top element of the heap in the tree array − 1 -1 −1 ,当用树状数组 a s k ask ask When the contribution of this element is naturally subtracted,直接开一个 b b b Sorted array is equivalent to priority queue.
代码:
#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;
}
边栏推荐
猜你喜欢
随机推荐
如何培养ui设计师的设计思维?
线代 | 秒杀方法与技巧
Merge similar items in LeetCode simple questions
Guo Jingjing's personal chess teaching, the good guy is a robot
Digicert EV证书签名后出现“证书对于请求用法无效”的解决方案
camshift achieves target tracking
九宫格抽奖动效
es6-promise对象详解
MySQL相关问题整理
CURRENT_TIMESTAMP(6) 函数是否存在问题?
H264 GOP 扫盲
LeetCode 146. LRU Cache
Overseas media publicity. What problems should domestic media pay attention to?
phpstrom 快速注释:
阿里架构师整理一份企业级SSM架构实战文档,让你熟悉底层原理
IDC第一的背后,阿里云在打造怎样的一朵“视频云”?
「企业架构」应用架构概述
讯飞创意组别 全国选拔赛成绩公布说明
一文详解 implementation api embed
海外邮件发送指南(二)








