/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int const N=1e6+10;
int fen[N]={};
int h[N];
int n,q;
int sum(int r)
{
int s=0;
for (;r>=0;r=(r&(r+1))-1)
s+=fen[r];
return s;
}
int get(int l,int r)
{
return sum(r)-sum(l-1);
}
void update(int i)
{
for (;i<n;i=(i|(i+1)))
fen[i]++;
}
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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
408 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
3448 KB |
Output is correct |
2 |
Correct |
83 ms |
3420 KB |
Output is correct |
3 |
Correct |
79 ms |
3288 KB |
Output is correct |
4 |
Correct |
87 ms |
3352 KB |
Output is correct |
5 |
Correct |
87 ms |
3248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
5532 KB |
Output is correct |
2 |
Correct |
76 ms |
5572 KB |
Output is correct |
3 |
Correct |
88 ms |
5456 KB |
Output is correct |
4 |
Correct |
79 ms |
5704 KB |
Output is correct |
5 |
Correct |
73 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
5712 KB |
Output is correct |
2 |
Correct |
73 ms |
5616 KB |
Output is correct |
3 |
Correct |
80 ms |
5708 KB |
Output is correct |
4 |
Correct |
76 ms |
5576 KB |
Output is correct |
5 |
Correct |
78 ms |
5976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
82 ms |
3448 KB |
Output is correct |
12 |
Correct |
83 ms |
3420 KB |
Output is correct |
13 |
Correct |
79 ms |
3288 KB |
Output is correct |
14 |
Correct |
87 ms |
3352 KB |
Output is correct |
15 |
Correct |
87 ms |
3248 KB |
Output is correct |
16 |
Correct |
56 ms |
2776 KB |
Output is correct |
17 |
Correct |
37 ms |
3028 KB |
Output is correct |
18 |
Correct |
21 ms |
2772 KB |
Output is correct |
19 |
Correct |
18 ms |
2464 KB |
Output is correct |
20 |
Correct |
45 ms |
3032 KB |
Output is correct |
21 |
Correct |
76 ms |
3288 KB |
Output is correct |
22 |
Correct |
48 ms |
2776 KB |
Output is correct |
23 |
Correct |
38 ms |
2776 KB |
Output is correct |
24 |
Correct |
24 ms |
2524 KB |
Output is correct |
25 |
Correct |
47 ms |
2776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
408 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
82 ms |
3448 KB |
Output is correct |
27 |
Correct |
83 ms |
3420 KB |
Output is correct |
28 |
Correct |
79 ms |
3288 KB |
Output is correct |
29 |
Correct |
87 ms |
3352 KB |
Output is correct |
30 |
Correct |
87 ms |
3248 KB |
Output is correct |
31 |
Correct |
72 ms |
5532 KB |
Output is correct |
32 |
Correct |
76 ms |
5572 KB |
Output is correct |
33 |
Correct |
88 ms |
5456 KB |
Output is correct |
34 |
Correct |
79 ms |
5704 KB |
Output is correct |
35 |
Correct |
73 ms |
5456 KB |
Output is correct |
36 |
Correct |
72 ms |
5712 KB |
Output is correct |
37 |
Correct |
73 ms |
5616 KB |
Output is correct |
38 |
Correct |
80 ms |
5708 KB |
Output is correct |
39 |
Correct |
76 ms |
5576 KB |
Output is correct |
40 |
Correct |
78 ms |
5976 KB |
Output is correct |
41 |
Correct |
56 ms |
2776 KB |
Output is correct |
42 |
Correct |
37 ms |
3028 KB |
Output is correct |
43 |
Correct |
21 ms |
2772 KB |
Output is correct |
44 |
Correct |
18 ms |
2464 KB |
Output is correct |
45 |
Correct |
45 ms |
3032 KB |
Output is correct |
46 |
Correct |
76 ms |
3288 KB |
Output is correct |
47 |
Correct |
48 ms |
2776 KB |
Output is correct |
48 |
Correct |
38 ms |
2776 KB |
Output is correct |
49 |
Correct |
24 ms |
2524 KB |
Output is correct |
50 |
Correct |
47 ms |
2776 KB |
Output is correct |
51 |
Correct |
101 ms |
5452 KB |
Output is correct |
52 |
Correct |
103 ms |
5536 KB |
Output is correct |
53 |
Correct |
102 ms |
5444 KB |
Output is correct |
54 |
Correct |
108 ms |
5376 KB |
Output is correct |
55 |
Correct |
104 ms |
5456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
408 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
82 ms |
3448 KB |
Output is correct |
27 |
Correct |
83 ms |
3420 KB |
Output is correct |
28 |
Correct |
79 ms |
3288 KB |
Output is correct |
29 |
Correct |
87 ms |
3352 KB |
Output is correct |
30 |
Correct |
87 ms |
3248 KB |
Output is correct |
31 |
Correct |
72 ms |
5532 KB |
Output is correct |
32 |
Correct |
76 ms |
5572 KB |
Output is correct |
33 |
Correct |
88 ms |
5456 KB |
Output is correct |
34 |
Correct |
79 ms |
5704 KB |
Output is correct |
35 |
Correct |
73 ms |
5456 KB |
Output is correct |
36 |
Correct |
72 ms |
5712 KB |
Output is correct |
37 |
Correct |
73 ms |
5616 KB |
Output is correct |
38 |
Correct |
80 ms |
5708 KB |
Output is correct |
39 |
Correct |
76 ms |
5576 KB |
Output is correct |
40 |
Correct |
78 ms |
5976 KB |
Output is correct |
41 |
Correct |
56 ms |
2776 KB |
Output is correct |
42 |
Correct |
37 ms |
3028 KB |
Output is correct |
43 |
Correct |
21 ms |
2772 KB |
Output is correct |
44 |
Correct |
18 ms |
2464 KB |
Output is correct |
45 |
Correct |
45 ms |
3032 KB |
Output is correct |
46 |
Correct |
76 ms |
3288 KB |
Output is correct |
47 |
Correct |
48 ms |
2776 KB |
Output is correct |
48 |
Correct |
38 ms |
2776 KB |
Output is correct |
49 |
Correct |
24 ms |
2524 KB |
Output is correct |
50 |
Correct |
47 ms |
2776 KB |
Output is correct |
51 |
Correct |
101 ms |
5452 KB |
Output is correct |
52 |
Correct |
103 ms |
5536 KB |
Output is correct |
53 |
Correct |
102 ms |
5444 KB |
Output is correct |
54 |
Correct |
108 ms |
5376 KB |
Output is correct |
55 |
Correct |
104 ms |
5456 KB |
Output is correct |
56 |
Execution timed out |
1025 ms |
43768 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |