P5459
[BJOI2016]回转寿司
题目描述
酷爱日料的小Z经常光顾学校东门外的回转寿司店。在这里,一盘盘寿司通过传送带依次呈现在小Z眼前。
不同的寿司带给小Z的味觉感受是不一样的,我们定义小Z对每盘寿司都有一个满意度。
例如小Z酷爱三文鱼,他对一盘三文鱼寿司的满意度为 \(10\);小Z觉得金枪鱼没有什么味道,他对一盘金枪鱼寿司的满意度只有 \(5\);小Z最近看了电影《美人鱼》,被里面的八爪鱼恶心到了,所以他对一盘八爪鱼刺身的满意度是 \(-100\)。
特别地,小Z是个著名的吃货,他吃回转寿司有一个习惯,我们称之为“狂吃不止”。具体地讲,当他吃掉传送带上的一盘寿司后,他会毫不犹豫地吃掉它后面的寿司,直到他不想再吃寿司了为止。
今天,小Z再次来到了这家回转寿司店,\(N\) 盘寿司将依次经过他的面前。其中,小Z对第 \(i\) 盘寿司的满意度为\(a_i\)。
小Z可以选择从哪盘寿司开始吃,也可以选择吃到哪盘寿司为止。他想知道共有多少种不同的选择,使得他的满意度之和不低于 \(L\),且不高于 \(R\)。
注意,虽然这是回转寿司,但是我们不认为这是一个环上的问题,而是一条线上的问题。即,小Z能吃到的是输入序列的一个连续子序列;最后一盘转走之后,第一盘并不会再出现一次。
输入格式
第一行三个正整数 \(N,L,R\),表示寿司盘数,满意度的下限和上限。
第二行包含 \(N\) 个整数 \(a_i\),表示小Z对寿司的满意度。
输出格式
一行一个整数,表示有多少种方案可以使得小Z的满意度之和不低于 \(L\) 且不高于 \(R\)。
样例 #1
样例输入 #1
5 5 9
1 2 3 4 5
样例输出 #1
6
提示
【数据范围】
\(1\le N \le 10^5\)
\(|a_i| \le 10^5\)
\(0\le L,R \le 10^9\)
最开始我是想用set+pbds rbtree做但不知为什么80pts
然后学习到了一个新东西 vector维护有序数组
v.insert(lower_bound(v.begin(),v.end(),a[i]),a[i]);
这样就能保证有序 能用二分查找
不得不说对于STL的应用要更灵活一点
Vector
#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int>v;
int a[100005];
signed main()
{
ios::sync_with_stdio(false);
int n,l,r,tot=0;
cin>>n>>l>>r;
v.push_back(0);
for(int i=1;i<=n;i++)
{
cin>>a[i];
a[i]+=a[i-1];
tot+=v.size()-(lower_bound(v.begin(),v.end(),a[i]-r)-v.begin());
tot-=v.size()-(upper_bound(v.begin(),v.end(),a[i]-l)-v.begin());
v.insert(lower_bound(v.begin(),v.end(),a[i]),a[i]);
}
cout<<tot<<"\n";
return 0;
}
set+rb_tree
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define int long long
int n,l,r,x,s[100005];
set<int>st;
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>tr;
signed main()
{
cin>>n>>l>>r;
for(int i=1;i<=n;i++)cin>>x,s[i]=s[i-1]+x;
int tot=0;
st.insert(-INT_MAX);st.insert(INT_MAX);
for(int i=1;i<=n;i++)
{
if(i==1)
{
if(s[1]>=l&&s[1]<=r)tot++;
st.insert((s[1]<<20)+i);tr.insert((s[1]<<20)+i);
}
else
{
set<int>::iterator it,itt;
it=st.lower_bound((s[i]-r)<<20);
int k1=tr.order_of_key(*it);
itt=st.upper_bound(((s[i]-l)<<20)+n);
int k2=tr.order_of_key(*itt);
// cout<<i<<":"<<s[i]<<" "<<*it<<" "<<*itt<<" "<<k1<<" "<<k2<<'\n';
if(k2-k1>0)
{
// cout<<s[i]<<" "<<k1<<" "<<k2<<"\n";
tot+=k2-k1;
}
st.insert((s[i]<<20)+i);
tr.insert((s[i]<<20)+i);
if(s[i]>=l&&s[i]<=r)tot++;
}
}
cout<<tot<<"\n";
return 0;
}
此生无悔入OI 来生AK IOI

浙公网安备 33010602011771号