#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
long long int n, q, a[N], b[N], fenwick[N];
void upd(int node, ll val){
for(int i = node; i <= q; i += (i & -i))
fenwick[i] += val;
}
ll query(int node){
long long int sum = 0;
for(int i = node; i >= 1; i -= (i & -i))
sum += fenwick[i];
return sum;
}
void solve(){
cin >> n >> q;
for(int i = 1; i <= n; ++i)
cin >> a[i];
vector<pair<int, int> > v;
v.push_back({0, 0});
for(int i = 1; i <= q; ++i){
cin >> b[i];
v.push_back({b[i], i});
}
int pre_greater[n + 1];
stack<int> st;
for(int i = n; i >= 1; --i){
while(!st.empty() and a[st.top()] < a[i]){
pre_greater[st.top()] = i;
st.pop();
}
st.push(i);
}
while(!st.empty()){
pre_greater[st.top()] = 0;
st.pop();
}
stack<int> stk;
int nxt_greater[n + 1];
for(int i = 1; i <= n; ++i){
while(!stk.empty() && a[stk.top()] <= a[i]){
nxt_greater[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
while(!stk.empty()){
nxt_greater[stk.top()] = n + 1;
stk.pop();
}
sort(v.begin(), v.end());
for(ll i = 1; i <= n; ++i){
ll j = pre_greater[i], k = nxt_greater[i];
ll mx = a[i];
ll low = 1, high = v.size(), ans = -1;
while(low <= high){
int mid = (low + high) / 2;
if(v[mid].first >= mx){
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
if(ans == -1) continue;
upd(ans, (i - j)*(k - i));
}
ll ans[q + 1];
for(int i = 1; i <= q; ++i)
ans[v[i].second] = query(i);
for(int i = 1; i <= q; ++i)
cout << ans[i] << "\n";
}
int main(){
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2384 KB |
Output is correct |
2 |
Correct |
13 ms |
2540 KB |
Output is correct |
3 |
Correct |
13 ms |
2348 KB |
Output is correct |
4 |
Correct |
13 ms |
2256 KB |
Output is correct |
5 |
Correct |
12 ms |
2256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
6724 KB |
Output is correct |
2 |
Correct |
45 ms |
6944 KB |
Output is correct |
3 |
Correct |
44 ms |
6880 KB |
Output is correct |
4 |
Correct |
45 ms |
7012 KB |
Output is correct |
5 |
Correct |
44 ms |
6880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
7232 KB |
Output is correct |
2 |
Correct |
46 ms |
7216 KB |
Output is correct |
3 |
Correct |
46 ms |
7360 KB |
Output is correct |
4 |
Correct |
47 ms |
7360 KB |
Output is correct |
5 |
Correct |
47 ms |
7624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
12 ms |
2384 KB |
Output is correct |
12 |
Correct |
13 ms |
2540 KB |
Output is correct |
13 |
Correct |
13 ms |
2348 KB |
Output is correct |
14 |
Correct |
13 ms |
2256 KB |
Output is correct |
15 |
Correct |
12 ms |
2256 KB |
Output is correct |
16 |
Correct |
12 ms |
2256 KB |
Output is correct |
17 |
Correct |
17 ms |
2476 KB |
Output is correct |
18 |
Correct |
13 ms |
2516 KB |
Output is correct |
19 |
Correct |
12 ms |
2256 KB |
Output is correct |
20 |
Correct |
14 ms |
2440 KB |
Output is correct |
21 |
Correct |
11 ms |
2260 KB |
Output is correct |
22 |
Correct |
13 ms |
2408 KB |
Output is correct |
23 |
Correct |
13 ms |
2384 KB |
Output is correct |
24 |
Correct |
13 ms |
2260 KB |
Output is correct |
25 |
Correct |
13 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
12 ms |
2384 KB |
Output is correct |
27 |
Correct |
13 ms |
2540 KB |
Output is correct |
28 |
Correct |
13 ms |
2348 KB |
Output is correct |
29 |
Correct |
13 ms |
2256 KB |
Output is correct |
30 |
Correct |
12 ms |
2256 KB |
Output is correct |
31 |
Correct |
50 ms |
6724 KB |
Output is correct |
32 |
Correct |
45 ms |
6944 KB |
Output is correct |
33 |
Correct |
44 ms |
6880 KB |
Output is correct |
34 |
Correct |
45 ms |
7012 KB |
Output is correct |
35 |
Correct |
44 ms |
6880 KB |
Output is correct |
36 |
Correct |
46 ms |
7232 KB |
Output is correct |
37 |
Correct |
46 ms |
7216 KB |
Output is correct |
38 |
Correct |
46 ms |
7360 KB |
Output is correct |
39 |
Correct |
47 ms |
7360 KB |
Output is correct |
40 |
Correct |
47 ms |
7624 KB |
Output is correct |
41 |
Correct |
12 ms |
2256 KB |
Output is correct |
42 |
Correct |
17 ms |
2476 KB |
Output is correct |
43 |
Correct |
13 ms |
2516 KB |
Output is correct |
44 |
Correct |
12 ms |
2256 KB |
Output is correct |
45 |
Correct |
14 ms |
2440 KB |
Output is correct |
46 |
Correct |
11 ms |
2260 KB |
Output is correct |
47 |
Correct |
13 ms |
2408 KB |
Output is correct |
48 |
Correct |
13 ms |
2384 KB |
Output is correct |
49 |
Correct |
13 ms |
2260 KB |
Output is correct |
50 |
Correct |
13 ms |
2384 KB |
Output is correct |
51 |
Correct |
56 ms |
6560 KB |
Output is correct |
52 |
Correct |
62 ms |
6848 KB |
Output is correct |
53 |
Correct |
56 ms |
6592 KB |
Output is correct |
54 |
Correct |
60 ms |
6752 KB |
Output is correct |
55 |
Correct |
57 ms |
6576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
472 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
0 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
12 ms |
2384 KB |
Output is correct |
27 |
Correct |
13 ms |
2540 KB |
Output is correct |
28 |
Correct |
13 ms |
2348 KB |
Output is correct |
29 |
Correct |
13 ms |
2256 KB |
Output is correct |
30 |
Correct |
12 ms |
2256 KB |
Output is correct |
31 |
Correct |
50 ms |
6724 KB |
Output is correct |
32 |
Correct |
45 ms |
6944 KB |
Output is correct |
33 |
Correct |
44 ms |
6880 KB |
Output is correct |
34 |
Correct |
45 ms |
7012 KB |
Output is correct |
35 |
Correct |
44 ms |
6880 KB |
Output is correct |
36 |
Correct |
46 ms |
7232 KB |
Output is correct |
37 |
Correct |
46 ms |
7216 KB |
Output is correct |
38 |
Correct |
46 ms |
7360 KB |
Output is correct |
39 |
Correct |
47 ms |
7360 KB |
Output is correct |
40 |
Correct |
47 ms |
7624 KB |
Output is correct |
41 |
Correct |
12 ms |
2256 KB |
Output is correct |
42 |
Correct |
17 ms |
2476 KB |
Output is correct |
43 |
Correct |
13 ms |
2516 KB |
Output is correct |
44 |
Correct |
12 ms |
2256 KB |
Output is correct |
45 |
Correct |
14 ms |
2440 KB |
Output is correct |
46 |
Correct |
11 ms |
2260 KB |
Output is correct |
47 |
Correct |
13 ms |
2408 KB |
Output is correct |
48 |
Correct |
13 ms |
2384 KB |
Output is correct |
49 |
Correct |
13 ms |
2260 KB |
Output is correct |
50 |
Correct |
13 ms |
2384 KB |
Output is correct |
51 |
Correct |
56 ms |
6560 KB |
Output is correct |
52 |
Correct |
62 ms |
6848 KB |
Output is correct |
53 |
Correct |
56 ms |
6592 KB |
Output is correct |
54 |
Correct |
60 ms |
6752 KB |
Output is correct |
55 |
Correct |
57 ms |
6576 KB |
Output is correct |
56 |
Correct |
791 ms |
66548 KB |
Output is correct |
57 |
Correct |
812 ms |
65272 KB |
Output is correct |
58 |
Correct |
744 ms |
61904 KB |
Output is correct |
59 |
Correct |
782 ms |
63596 KB |
Output is correct |
60 |
Correct |
828 ms |
67728 KB |
Output is correct |