[ABC247E] Max Min 题解
本题需要应用容斥原理。
题目要求统计
首先,如果
例如,
所以,对于每个分割出来的子序列(暂且称为
这是容斥原理就派上用场了:
设
放代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
main(){
ios::sync_with_stdio(false);
int n,x,y; cin>>n>>x>>y;
vector<int> a(n);
for(auto &i:a)cin>>i;
auto f=[&](int x,int y){
int c=0;
for(int i=0,j=0;i<n;i++){
if(a[i]<y||a[i]>x)j=i+1;
c+=i-j+1;
}
return c;
};
cout<<f(x,y)-f(x-1,y)-f(x,y+1)+f(x-1,y+1)<<endl;
return 0;
}