/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int const N=1e6+10;
int St[4*N]={};
int h[N];
int n,q;
int get(int l,int r,int i=1,int st=0,int en=N)
{
if (st>=l&&en<=r)
return St[i];
if (st>r||en<l)
return 0;
int mid=(st+en)/2;
return get(l,r,i*2,st,mid)+get(l,r,i*2+1,mid+1,en);
}
void update(int r,int i=1,int st=0,int en=N)
{
if (st==en)
{
St[i]=1;
return;
}
int mid=(st+en)/2;
if (r<=mid)
update(r,i*2,st,mid);
else
update(r,i*2+1,mid+1,en);
St[i]=St[i*2]+St[i*2+1];
}
inline void solve()
{
cin>>n>>q;
for (int i=0;i<n;i++)
cin>>h[i];
vector<pair<int,int>>d;
for (int i=0;i<n;i++)
d.push_back({h[i],i});
sort(begin(d),end(d));
long long ANS[q]={};
vector<pair<int,int>>Q;
for (int i=0;i<q;i++)
{
int x;
cin>>x;
Q.push_back({x,i});
}
sort(begin(Q),end(Q));
int i=0;
long long pre=0;
for (auto k:Q)
{
int mx=k.first;
vector<int>g;
long long ans=pre;
while (i<n&&d[i].first<=mx)
{
g.push_back(d[i].second);
i++;
}
for (auto j:g)
{
update(j);
int st=-1,en=j;
while (st+1<en)
{
int mid=(st+en)/2;
if (get(mid,j)==j-mid+1)
en=mid;
else
st=mid;
}
long long l1=j-en+1;
st=j,en=n;
while (st+1<en)
{
int mid=(st+en)/2;
if (get(j,mid)==mid-j+1)
st=mid;
else
en=mid;
}
long long l2=st-j+1;
ans+=(l1-1)*(l2-1);
ans+=l2+l1-2;
ans++;
}
ANS[k.second]=ans;
pre=ans;
}
for (auto i:ANS)
cout<<i<<'\n';
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
513 ms |
3796 KB |
Output is correct |
2 |
Correct |
510 ms |
4056 KB |
Output is correct |
3 |
Correct |
478 ms |
3828 KB |
Output is correct |
4 |
Correct |
460 ms |
3796 KB |
Output is correct |
5 |
Correct |
476 ms |
3800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
5940 KB |
Output is correct |
2 |
Correct |
413 ms |
6144 KB |
Output is correct |
3 |
Correct |
406 ms |
5964 KB |
Output is correct |
4 |
Correct |
420 ms |
6224 KB |
Output is correct |
5 |
Correct |
400 ms |
5968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
444 ms |
5972 KB |
Output is correct |
2 |
Correct |
411 ms |
5968 KB |
Output is correct |
3 |
Correct |
401 ms |
6192 KB |
Output is correct |
4 |
Correct |
441 ms |
6124 KB |
Output is correct |
5 |
Correct |
413 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
513 ms |
3796 KB |
Output is correct |
12 |
Correct |
510 ms |
4056 KB |
Output is correct |
13 |
Correct |
478 ms |
3828 KB |
Output is correct |
14 |
Correct |
460 ms |
3796 KB |
Output is correct |
15 |
Correct |
476 ms |
3800 KB |
Output is correct |
16 |
Correct |
346 ms |
3284 KB |
Output is correct |
17 |
Correct |
185 ms |
3540 KB |
Output is correct |
18 |
Correct |
71 ms |
3284 KB |
Output is correct |
19 |
Correct |
57 ms |
3028 KB |
Output is correct |
20 |
Correct |
250 ms |
3544 KB |
Output is correct |
21 |
Correct |
471 ms |
3544 KB |
Output is correct |
22 |
Correct |
261 ms |
3288 KB |
Output is correct |
23 |
Correct |
151 ms |
3288 KB |
Output is correct |
24 |
Correct |
95 ms |
3032 KB |
Output is correct |
25 |
Correct |
263 ms |
3288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
344 KB |
Output is correct |
26 |
Correct |
513 ms |
3796 KB |
Output is correct |
27 |
Correct |
510 ms |
4056 KB |
Output is correct |
28 |
Correct |
478 ms |
3828 KB |
Output is correct |
29 |
Correct |
460 ms |
3796 KB |
Output is correct |
30 |
Correct |
476 ms |
3800 KB |
Output is correct |
31 |
Correct |
429 ms |
5940 KB |
Output is correct |
32 |
Correct |
413 ms |
6144 KB |
Output is correct |
33 |
Correct |
406 ms |
5964 KB |
Output is correct |
34 |
Correct |
420 ms |
6224 KB |
Output is correct |
35 |
Correct |
400 ms |
5968 KB |
Output is correct |
36 |
Correct |
444 ms |
5972 KB |
Output is correct |
37 |
Correct |
411 ms |
5968 KB |
Output is correct |
38 |
Correct |
401 ms |
6192 KB |
Output is correct |
39 |
Correct |
441 ms |
6124 KB |
Output is correct |
40 |
Correct |
413 ms |
6224 KB |
Output is correct |
41 |
Correct |
346 ms |
3284 KB |
Output is correct |
42 |
Correct |
185 ms |
3540 KB |
Output is correct |
43 |
Correct |
71 ms |
3284 KB |
Output is correct |
44 |
Correct |
57 ms |
3028 KB |
Output is correct |
45 |
Correct |
250 ms |
3544 KB |
Output is correct |
46 |
Correct |
471 ms |
3544 KB |
Output is correct |
47 |
Correct |
261 ms |
3288 KB |
Output is correct |
48 |
Correct |
151 ms |
3288 KB |
Output is correct |
49 |
Correct |
95 ms |
3032 KB |
Output is correct |
50 |
Correct |
263 ms |
3288 KB |
Output is correct |
51 |
Correct |
528 ms |
5964 KB |
Output is correct |
52 |
Correct |
510 ms |
5964 KB |
Output is correct |
53 |
Correct |
530 ms |
5964 KB |
Output is correct |
54 |
Correct |
505 ms |
5712 KB |
Output is correct |
55 |
Correct |
530 ms |
6036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
604 KB |
Output is correct |
25 |
Correct |
3 ms |
344 KB |
Output is correct |
26 |
Correct |
513 ms |
3796 KB |
Output is correct |
27 |
Correct |
510 ms |
4056 KB |
Output is correct |
28 |
Correct |
478 ms |
3828 KB |
Output is correct |
29 |
Correct |
460 ms |
3796 KB |
Output is correct |
30 |
Correct |
476 ms |
3800 KB |
Output is correct |
31 |
Correct |
429 ms |
5940 KB |
Output is correct |
32 |
Correct |
413 ms |
6144 KB |
Output is correct |
33 |
Correct |
406 ms |
5964 KB |
Output is correct |
34 |
Correct |
420 ms |
6224 KB |
Output is correct |
35 |
Correct |
400 ms |
5968 KB |
Output is correct |
36 |
Correct |
444 ms |
5972 KB |
Output is correct |
37 |
Correct |
411 ms |
5968 KB |
Output is correct |
38 |
Correct |
401 ms |
6192 KB |
Output is correct |
39 |
Correct |
441 ms |
6124 KB |
Output is correct |
40 |
Correct |
413 ms |
6224 KB |
Output is correct |
41 |
Correct |
346 ms |
3284 KB |
Output is correct |
42 |
Correct |
185 ms |
3540 KB |
Output is correct |
43 |
Correct |
71 ms |
3284 KB |
Output is correct |
44 |
Correct |
57 ms |
3028 KB |
Output is correct |
45 |
Correct |
250 ms |
3544 KB |
Output is correct |
46 |
Correct |
471 ms |
3544 KB |
Output is correct |
47 |
Correct |
261 ms |
3288 KB |
Output is correct |
48 |
Correct |
151 ms |
3288 KB |
Output is correct |
49 |
Correct |
95 ms |
3032 KB |
Output is correct |
50 |
Correct |
263 ms |
3288 KB |
Output is correct |
51 |
Correct |
528 ms |
5964 KB |
Output is correct |
52 |
Correct |
510 ms |
5964 KB |
Output is correct |
53 |
Correct |
530 ms |
5964 KB |
Output is correct |
54 |
Correct |
505 ms |
5712 KB |
Output is correct |
55 |
Correct |
530 ms |
6036 KB |
Output is correct |
56 |
Execution timed out |
1035 ms |
47908 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |