#include<bits/stdc++.h>
using namespace std;
#define NAME "CONCERT"
#define nl '\n'
#define allofa(x,sz) x,x+sz+1
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,val) memset(x,val,sizeof(x))
template<class T> T Abs(T &x) {return (x>=0 ? x : -x);};
template<class X,class Y> bool minimize(X &a, Y b){if(a>b) {a=b;return true;}return false;};
template<class X,class Y> bool maximize(X &a, Y b){if(a<b) {a=b;return true;}return false;};
typedef long long ll;
const ll mod = (long long)1e9+7;
const ll LINF = (long long)1e15;
const int INF = (int)1e9;
const int MAXN = (int)1e6+5;
bool isIncreasing;
int a[MAXN];
int n,q;
void ccps() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(NAME".inp","r")) {
freopen(NAME".inp","r",stdin);
freopen(NAME".out","w",stdout);
}
}
namespace BruteForce {
const int N = (int)1e5+5;
ll fact[N],inv[N];
bool check() {
return n<=1e3 and q<=1e3;
}
ll indianPow(ll a, ll b) {
if(b==0) return 1;
if(b==1) return a;
ll tmp = indianPow(a,b/2);
if(b%2==0) return (tmp%mod * tmp%mod)%mod;
return (tmp%mod * (tmp%mod * a%mod)%mod)%mod;
}
void init() {
fact[0]=fact[1]=inv[0]=inv[1]=1;
for (int i = 1; i<N; i++) {
fact[i]=(fact[i-1]*i)%mod;
}
inv[N-1]=indianPow(fact[N-1],mod-2);
for (int i = N-2; i>=1; i--) {
inv[i] = (inv[i+1]*(i+1))%mod;
}
}
ll nCk(int n, int k) {
return ((fact[n]%mod * inv[k]%mod) * inv[n-k]%mod)%mod;
}
void sol() {
init();
while(q--) {
int upper; cin >> upper;
ll res = 0;
int cnt = 0;
for (int i = 1; i<=n; i++) {
if(a[i]<=upper) {
// cout << a[i] << " ";
cnt++;
res++;
}
else {
// cout << cnt << " ";
res+=nCk(cnt,2);
cnt=0;
}
}
res+=nCk(cnt,2);
cout << res << nl;
}
}
}
namespace IncreasingSub {
const int N = (int)1e5+5;
ll fact[N],inv[N];
bool isSorted() {
for (int i = 2; i<=n; i++) {
if(a[i]<a[i-1]) return false;
}
return true;
}
bool check() {
return n<=1e5 and isSorted();
}
ll indianPow(ll a, ll b) {
if(b==0) return 1;
if(b==1) return a;
ll tmp = indianPow(a,b/2);
if(b%2==0) return (tmp%mod * tmp%mod)%mod;
return (tmp%mod * (tmp%mod * a%mod)%mod)%mod;
}
void init() {
fact[0]=fact[1]=inv[0]=inv[1]=1;
for (int i = 1; i<N; i++) {
fact[i]=(fact[i-1]*i)%mod;
}
inv[N-1]=indianPow(fact[N-1],mod-2);
for (int i = N-2; i>=1; i--) {
inv[i] = (inv[i+1]*(i+1))%mod;
}
}
ll nCk(int n, int k) {
return ((fact[n]%mod * inv[k]%mod) * inv[n-k]%mod)%mod;
}
int binSearch(int x) {
int l = 1, r = n;
int res = -1;
while(l<=r) {
int mid = (l+r)>>1;
if(a[mid]<=x) {
res = mid;
l = mid+1;
}
else {
r = mid-1;
}
}
return res;
}
void sol() {
init();
while(q--) {
int upper; cin >> upper;
int range = binSearch(upper);
ll res = range + nCk(range,2);
cout << res << nl;
}
}
}
signed main() {
ccps();
cin >> n >> q;
for (int i = 1; i<=n; i++) {
cin >> a[i];
}
if(BruteForce::check() and !IncreasingSub::isSorted()) BruteForce::sol();
else if(IncreasingSub::check()) IncreasingSub::sol();
}
Compilation message (stderr)
pilot.cpp: In function 'void ccps()':
pilot.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen(NAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
pilot.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen(NAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |