#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int ar[1000005];
int asum[1000005];
int ssum[1000005];
int rsum[1000005];
vector<int>v;
struct node{
int val;
int sz;
node *l;
node *r;
node(int x=0){
val=x;
l=NULL;
r=NULL;
sz=0;
}
};
node *rt=NULL;
vector<int>qr;
stack<node*>s;
void dfs(node *u){
u->sz=1;
if(u->l){
dfs(u->l);
u->sz+=u->l->sz;
}
if(u->r){
dfs(u->r);
u->sz+=u->r->sz;
}
//cerr<<u->val<<" "<<u->sz<<"\n";
}
void dfs2(node *u,int pn=0){
asum[u->val]+=(u->sz)*((u->sz)+1)/2;
ssum[pn-1]+=(u->sz)*((u->sz)+1)/2;
if(u->l)dfs2(u->l,u->val);
if(u->r)dfs2(u->r,u->val);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
for(int i=0;i<q;i++){
int a;cin>>a;
qr.push_back(a);
}
//sort(qr.begin(),qr.end(),greater<pair<int,int> >());
for(int i=1;i<=n;i++){
int x=ar[i];
node *p=NULL;
while(!s.empty()&&s.top()->val<x){
p=s.top();
s.pop();
}
if(s.empty()){
rt=new node(x);
rt->l=p;
s.push(rt);
}else{
s.top()->r=new node(x);
s.top()->r->l=p;
s.push(s.top()->r);
}
}
dfs(rt);
dfs2(rt,1e6+2);
long long sum=0;
//cerr<<endl;
for(int i=0;i<=1e6;i++){
sum+=asum[i];
rsum[i]=sum;
sum-=ssum[i];
//cerr<<"i:"<<i<<" "<<asum[i]<<" "<<ssum[i]<<"\n";
}
for(auto x:qr)cout<<rsum[x]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
24524 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
24668 KB |
Output is correct |
15 |
Correct |
3 ms |
12960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
24524 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
24668 KB |
Output is correct |
15 |
Correct |
3 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
25044 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
24524 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
24668 KB |
Output is correct |
15 |
Correct |
3 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
25044 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
4 ms |
25180 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
25180 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
19548 KB |
Output is correct |
2 |
Correct |
18 ms |
29784 KB |
Output is correct |
3 |
Correct |
15 ms |
19292 KB |
Output is correct |
4 |
Correct |
17 ms |
29276 KB |
Output is correct |
5 |
Correct |
14 ms |
19164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
23808 KB |
Output is correct |
2 |
Correct |
29 ms |
24024 KB |
Output is correct |
3 |
Correct |
26 ms |
23768 KB |
Output is correct |
4 |
Correct |
27 ms |
24280 KB |
Output is correct |
5 |
Correct |
26 ms |
23760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
34252 KB |
Output is correct |
2 |
Correct |
30 ms |
33996 KB |
Output is correct |
3 |
Correct |
35 ms |
33940 KB |
Output is correct |
4 |
Correct |
30 ms |
34520 KB |
Output is correct |
5 |
Correct |
31 ms |
34252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
14 ms |
19548 KB |
Output is correct |
12 |
Correct |
18 ms |
29784 KB |
Output is correct |
13 |
Correct |
15 ms |
19292 KB |
Output is correct |
14 |
Correct |
17 ms |
29276 KB |
Output is correct |
15 |
Correct |
14 ms |
19164 KB |
Output is correct |
16 |
Correct |
14 ms |
19800 KB |
Output is correct |
17 |
Correct |
19 ms |
30300 KB |
Output is correct |
18 |
Correct |
19 ms |
30556 KB |
Output is correct |
19 |
Correct |
15 ms |
19804 KB |
Output is correct |
20 |
Correct |
23 ms |
30556 KB |
Output is correct |
21 |
Correct |
15 ms |
19804 KB |
Output is correct |
22 |
Correct |
17 ms |
30040 KB |
Output is correct |
23 |
Correct |
15 ms |
20060 KB |
Output is correct |
24 |
Correct |
18 ms |
30044 KB |
Output is correct |
25 |
Correct |
14 ms |
20052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
24524 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
24668 KB |
Output is correct |
15 |
Correct |
3 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
25044 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
4 ms |
25180 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
25180 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
14 ms |
19548 KB |
Output is correct |
27 |
Correct |
18 ms |
29784 KB |
Output is correct |
28 |
Correct |
15 ms |
19292 KB |
Output is correct |
29 |
Correct |
17 ms |
29276 KB |
Output is correct |
30 |
Correct |
14 ms |
19164 KB |
Output is correct |
31 |
Correct |
28 ms |
23808 KB |
Output is correct |
32 |
Correct |
29 ms |
24024 KB |
Output is correct |
33 |
Correct |
26 ms |
23768 KB |
Output is correct |
34 |
Correct |
27 ms |
24280 KB |
Output is correct |
35 |
Correct |
26 ms |
23760 KB |
Output is correct |
36 |
Correct |
29 ms |
34252 KB |
Output is correct |
37 |
Correct |
30 ms |
33996 KB |
Output is correct |
38 |
Correct |
35 ms |
33940 KB |
Output is correct |
39 |
Correct |
30 ms |
34520 KB |
Output is correct |
40 |
Correct |
31 ms |
34252 KB |
Output is correct |
41 |
Correct |
14 ms |
19800 KB |
Output is correct |
42 |
Correct |
19 ms |
30300 KB |
Output is correct |
43 |
Correct |
19 ms |
30556 KB |
Output is correct |
44 |
Correct |
15 ms |
19804 KB |
Output is correct |
45 |
Correct |
23 ms |
30556 KB |
Output is correct |
46 |
Correct |
15 ms |
19804 KB |
Output is correct |
47 |
Correct |
17 ms |
30040 KB |
Output is correct |
48 |
Correct |
15 ms |
20060 KB |
Output is correct |
49 |
Correct |
18 ms |
30044 KB |
Output is correct |
50 |
Correct |
14 ms |
20052 KB |
Output is correct |
51 |
Correct |
29 ms |
22232 KB |
Output is correct |
52 |
Correct |
32 ms |
32212 KB |
Output is correct |
53 |
Correct |
31 ms |
22188 KB |
Output is correct |
54 |
Correct |
31 ms |
32216 KB |
Output is correct |
55 |
Correct |
28 ms |
22108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
4 ms |
18348 KB |
Output is correct |
3 |
Correct |
3 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
12892 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
14428 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
24524 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
24668 KB |
Output is correct |
15 |
Correct |
3 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
25180 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
25044 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
4 ms |
25180 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
25180 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
14 ms |
19548 KB |
Output is correct |
27 |
Correct |
18 ms |
29784 KB |
Output is correct |
28 |
Correct |
15 ms |
19292 KB |
Output is correct |
29 |
Correct |
17 ms |
29276 KB |
Output is correct |
30 |
Correct |
14 ms |
19164 KB |
Output is correct |
31 |
Correct |
28 ms |
23808 KB |
Output is correct |
32 |
Correct |
29 ms |
24024 KB |
Output is correct |
33 |
Correct |
26 ms |
23768 KB |
Output is correct |
34 |
Correct |
27 ms |
24280 KB |
Output is correct |
35 |
Correct |
26 ms |
23760 KB |
Output is correct |
36 |
Correct |
29 ms |
34252 KB |
Output is correct |
37 |
Correct |
30 ms |
33996 KB |
Output is correct |
38 |
Correct |
35 ms |
33940 KB |
Output is correct |
39 |
Correct |
30 ms |
34520 KB |
Output is correct |
40 |
Correct |
31 ms |
34252 KB |
Output is correct |
41 |
Correct |
14 ms |
19800 KB |
Output is correct |
42 |
Correct |
19 ms |
30300 KB |
Output is correct |
43 |
Correct |
19 ms |
30556 KB |
Output is correct |
44 |
Correct |
15 ms |
19804 KB |
Output is correct |
45 |
Correct |
23 ms |
30556 KB |
Output is correct |
46 |
Correct |
15 ms |
19804 KB |
Output is correct |
47 |
Correct |
17 ms |
30040 KB |
Output is correct |
48 |
Correct |
15 ms |
20060 KB |
Output is correct |
49 |
Correct |
18 ms |
30044 KB |
Output is correct |
50 |
Correct |
14 ms |
20052 KB |
Output is correct |
51 |
Correct |
29 ms |
22232 KB |
Output is correct |
52 |
Correct |
32 ms |
32212 KB |
Output is correct |
53 |
Correct |
31 ms |
22188 KB |
Output is correct |
54 |
Correct |
31 ms |
32216 KB |
Output is correct |
55 |
Correct |
28 ms |
22108 KB |
Output is correct |
56 |
Correct |
282 ms |
103720 KB |
Output is correct |
57 |
Correct |
306 ms |
105144 KB |
Output is correct |
58 |
Correct |
269 ms |
100160 KB |
Output is correct |
59 |
Correct |
290 ms |
101880 KB |
Output is correct |
60 |
Correct |
288 ms |
106548 KB |
Output is correct |