#include<bits/stdc++.h>
using namespace std;
void so(int x,int y, long long int a[300001],int b[300001],int c[300001])
{
if(x==y)
{
b[x]=0;
c[x]=0;
return;
}
if(y==x+1)
{
if(a[x]<a[x+1])
{
b[x]=0;
c[x]=0;
b[x+1]=1;
c[x+1]=0;
}
else if(a[x]>a[x+1])
{
b[x]=0;
c[x]=0;
b[x+1]=0;
c[x+1]=1;
long long int temp=a[x];
a[x]=a[x+1];
a[x+1]=temp;
}
else
{
b[x]=0;
c[x]=0;
b[x+1]=0;
c[x+1]=0;
}
return;
}
int ha=(y-x+1)/2;
so(x,x+ha-1,a,b,c);
so(x+ha,y,a,b,c);
long long int a1[y-x+1]={};
int b1[y-x+1]={};
int c1[y-x+1]={};
int p1=x,p11=x;
int p2=x+ha;
int p=0;
while(p1<x+ha || p2<=y)
{
if(p1>=x+ha)
{
if(a[p2]>a[p1-1])
{
p11=x+ha;
}
a1[p]=a[p2];
b1[p]=b[p2]+(p11-x);
c1[p]=c[p2];
p++;
p2++;
}
else if(p2>y)
{
a1[p]=a[p1];
b1[p]=b[p1];
c1[p]=c[p1]+y-x-ha+1;
p++;
p1++;
}
else if(a[p1]<=a[p2])
{
a1[p]=a[p1];
b1[p]=b[p1];
c1[p]=c[p1]+(p2-x-ha);
if(p1>x && a[p1]!=a[p1-1])
{
p11=p1;
}
p++;
p1++;
}
else
{
if(p1>x && a[p2]>a[p1-1])
{
p11=p1;
}
a1[p]=a[p2];
b1[p]=b[p2]+(p11-x);
c1[p]=c[p2];
p++;
p2++;
}
}
for(int i=0;i<=y-x;i++)
{
a[x+i]=a1[i];
b[x+i]=b1[i];
c[x+i]=c1[i];
}
return;
}
int main()
{
int n;
scanf("%d",&n);
long long int h[300001]={};
int bf[300001]={};
int af[300001]={};
for(int i=0;i<n;i++)
{
scanf("%lld",&h[i]);
}
so(0,n-1,h,bf,af);
long long int ans=0;
for(int i=0;i<n;i++)
{
ans+=(long long)1*bf[i]*af[i];
}
printf("%lld",ans);
}
Compilation message
Mountains.cpp: In function 'int main()':
Mountains.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
109 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Mountains.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
115 | scanf("%lld",&h[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
106 ms |
9708 KB |
Output is correct |
3 |
Correct |
105 ms |
9836 KB |
Output is correct |
4 |
Correct |
105 ms |
9836 KB |
Output is correct |
5 |
Correct |
107 ms |
9880 KB |
Output is correct |
6 |
Correct |
104 ms |
9836 KB |
Output is correct |
7 |
Correct |
125 ms |
9836 KB |
Output is correct |
8 |
Correct |
109 ms |
9836 KB |
Output is correct |
9 |
Correct |
107 ms |
9836 KB |
Output is correct |
10 |
Correct |
107 ms |
9836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
9712 KB |
Output is correct |
2 |
Correct |
78 ms |
9708 KB |
Output is correct |
3 |
Correct |
77 ms |
9708 KB |
Output is correct |
4 |
Correct |
82 ms |
9708 KB |
Output is correct |
5 |
Correct |
79 ms |
9808 KB |
Output is correct |
6 |
Correct |
80 ms |
9708 KB |
Output is correct |
7 |
Correct |
81 ms |
9708 KB |
Output is correct |
8 |
Correct |
72 ms |
9708 KB |
Output is correct |
9 |
Correct |
71 ms |
9708 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
9712 KB |
Output is correct |
2 |
Correct |
78 ms |
9708 KB |
Output is correct |
3 |
Correct |
77 ms |
9708 KB |
Output is correct |
4 |
Correct |
82 ms |
9708 KB |
Output is correct |
5 |
Correct |
79 ms |
9808 KB |
Output is correct |
6 |
Correct |
80 ms |
9708 KB |
Output is correct |
7 |
Correct |
81 ms |
9708 KB |
Output is correct |
8 |
Correct |
72 ms |
9708 KB |
Output is correct |
9 |
Correct |
71 ms |
9708 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
87 ms |
9708 KB |
Output is correct |
12 |
Correct |
102 ms |
9836 KB |
Output is correct |
13 |
Correct |
90 ms |
9708 KB |
Output is correct |
14 |
Correct |
87 ms |
9668 KB |
Output is correct |
15 |
Correct |
87 ms |
9708 KB |
Output is correct |
16 |
Correct |
87 ms |
9708 KB |
Output is correct |
17 |
Correct |
87 ms |
9708 KB |
Output is correct |
18 |
Correct |
72 ms |
9708 KB |
Output is correct |
19 |
Correct |
71 ms |
9708 KB |
Output is correct |
20 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
3 ms |
4972 KB |
Output is correct |
7 |
Correct |
3 ms |
4972 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4972 KB |
Output is correct |
10 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
3 ms |
4972 KB |
Output is correct |
7 |
Correct |
3 ms |
4972 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4972 KB |
Output is correct |
10 |
Correct |
3 ms |
4972 KB |
Output is correct |
11 |
Correct |
7 ms |
5300 KB |
Output is correct |
12 |
Correct |
7 ms |
5484 KB |
Output is correct |
13 |
Correct |
7 ms |
5356 KB |
Output is correct |
14 |
Correct |
7 ms |
5356 KB |
Output is correct |
15 |
Correct |
7 ms |
5356 KB |
Output is correct |
16 |
Correct |
7 ms |
5356 KB |
Output is correct |
17 |
Correct |
7 ms |
5372 KB |
Output is correct |
18 |
Correct |
7 ms |
5356 KB |
Output is correct |
19 |
Correct |
6 ms |
5248 KB |
Output is correct |
20 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
9712 KB |
Output is correct |
2 |
Correct |
78 ms |
9708 KB |
Output is correct |
3 |
Correct |
77 ms |
9708 KB |
Output is correct |
4 |
Correct |
82 ms |
9708 KB |
Output is correct |
5 |
Correct |
79 ms |
9808 KB |
Output is correct |
6 |
Correct |
80 ms |
9708 KB |
Output is correct |
7 |
Correct |
81 ms |
9708 KB |
Output is correct |
8 |
Correct |
72 ms |
9708 KB |
Output is correct |
9 |
Correct |
71 ms |
9708 KB |
Output is correct |
10 |
Correct |
4 ms |
4972 KB |
Output is correct |
11 |
Correct |
87 ms |
9708 KB |
Output is correct |
12 |
Correct |
102 ms |
9836 KB |
Output is correct |
13 |
Correct |
90 ms |
9708 KB |
Output is correct |
14 |
Correct |
87 ms |
9668 KB |
Output is correct |
15 |
Correct |
87 ms |
9708 KB |
Output is correct |
16 |
Correct |
87 ms |
9708 KB |
Output is correct |
17 |
Correct |
87 ms |
9708 KB |
Output is correct |
18 |
Correct |
72 ms |
9708 KB |
Output is correct |
19 |
Correct |
71 ms |
9708 KB |
Output is correct |
20 |
Correct |
3 ms |
4972 KB |
Output is correct |
21 |
Correct |
110 ms |
9708 KB |
Output is correct |
22 |
Correct |
103 ms |
9728 KB |
Output is correct |
23 |
Correct |
104 ms |
9708 KB |
Output is correct |
24 |
Correct |
107 ms |
9580 KB |
Output is correct |
25 |
Correct |
106 ms |
9836 KB |
Output is correct |
26 |
Correct |
104 ms |
9708 KB |
Output is correct |
27 |
Correct |
104 ms |
9708 KB |
Output is correct |
28 |
Correct |
78 ms |
9708 KB |
Output is correct |
29 |
Correct |
80 ms |
9836 KB |
Output is correct |
30 |
Correct |
3 ms |
4972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4972 KB |
Output is correct |
2 |
Correct |
106 ms |
9708 KB |
Output is correct |
3 |
Correct |
105 ms |
9836 KB |
Output is correct |
4 |
Correct |
105 ms |
9836 KB |
Output is correct |
5 |
Correct |
107 ms |
9880 KB |
Output is correct |
6 |
Correct |
104 ms |
9836 KB |
Output is correct |
7 |
Correct |
125 ms |
9836 KB |
Output is correct |
8 |
Correct |
109 ms |
9836 KB |
Output is correct |
9 |
Correct |
107 ms |
9836 KB |
Output is correct |
10 |
Correct |
107 ms |
9836 KB |
Output is correct |
11 |
Correct |
78 ms |
9712 KB |
Output is correct |
12 |
Correct |
78 ms |
9708 KB |
Output is correct |
13 |
Correct |
77 ms |
9708 KB |
Output is correct |
14 |
Correct |
82 ms |
9708 KB |
Output is correct |
15 |
Correct |
79 ms |
9808 KB |
Output is correct |
16 |
Correct |
80 ms |
9708 KB |
Output is correct |
17 |
Correct |
81 ms |
9708 KB |
Output is correct |
18 |
Correct |
72 ms |
9708 KB |
Output is correct |
19 |
Correct |
71 ms |
9708 KB |
Output is correct |
20 |
Correct |
4 ms |
4972 KB |
Output is correct |
21 |
Correct |
87 ms |
9708 KB |
Output is correct |
22 |
Correct |
102 ms |
9836 KB |
Output is correct |
23 |
Correct |
90 ms |
9708 KB |
Output is correct |
24 |
Correct |
87 ms |
9668 KB |
Output is correct |
25 |
Correct |
87 ms |
9708 KB |
Output is correct |
26 |
Correct |
87 ms |
9708 KB |
Output is correct |
27 |
Correct |
87 ms |
9708 KB |
Output is correct |
28 |
Correct |
72 ms |
9708 KB |
Output is correct |
29 |
Correct |
71 ms |
9708 KB |
Output is correct |
30 |
Correct |
3 ms |
4972 KB |
Output is correct |
31 |
Correct |
3 ms |
4972 KB |
Output is correct |
32 |
Correct |
3 ms |
4972 KB |
Output is correct |
33 |
Correct |
3 ms |
4972 KB |
Output is correct |
34 |
Correct |
3 ms |
4972 KB |
Output is correct |
35 |
Correct |
3 ms |
4972 KB |
Output is correct |
36 |
Correct |
3 ms |
4972 KB |
Output is correct |
37 |
Correct |
3 ms |
4972 KB |
Output is correct |
38 |
Correct |
3 ms |
4972 KB |
Output is correct |
39 |
Correct |
3 ms |
4972 KB |
Output is correct |
40 |
Correct |
3 ms |
4972 KB |
Output is correct |
41 |
Correct |
7 ms |
5300 KB |
Output is correct |
42 |
Correct |
7 ms |
5484 KB |
Output is correct |
43 |
Correct |
7 ms |
5356 KB |
Output is correct |
44 |
Correct |
7 ms |
5356 KB |
Output is correct |
45 |
Correct |
7 ms |
5356 KB |
Output is correct |
46 |
Correct |
7 ms |
5356 KB |
Output is correct |
47 |
Correct |
7 ms |
5372 KB |
Output is correct |
48 |
Correct |
7 ms |
5356 KB |
Output is correct |
49 |
Correct |
6 ms |
5248 KB |
Output is correct |
50 |
Correct |
3 ms |
4972 KB |
Output is correct |
51 |
Correct |
110 ms |
9708 KB |
Output is correct |
52 |
Correct |
103 ms |
9728 KB |
Output is correct |
53 |
Correct |
104 ms |
9708 KB |
Output is correct |
54 |
Correct |
107 ms |
9580 KB |
Output is correct |
55 |
Correct |
106 ms |
9836 KB |
Output is correct |
56 |
Correct |
104 ms |
9708 KB |
Output is correct |
57 |
Correct |
104 ms |
9708 KB |
Output is correct |
58 |
Correct |
78 ms |
9708 KB |
Output is correct |
59 |
Correct |
80 ms |
9836 KB |
Output is correct |
60 |
Correct |
3 ms |
4972 KB |
Output is correct |
61 |
Correct |
134 ms |
15212 KB |
Output is correct |
62 |
Correct |
135 ms |
15212 KB |
Output is correct |
63 |
Correct |
135 ms |
15212 KB |
Output is correct |
64 |
Correct |
136 ms |
15340 KB |
Output is correct |
65 |
Correct |
136 ms |
15212 KB |
Output is correct |
66 |
Correct |
133 ms |
15212 KB |
Output is correct |
67 |
Correct |
133 ms |
15212 KB |
Output is correct |
68 |
Correct |
135 ms |
15340 KB |
Output is correct |
69 |
Correct |
108 ms |
11500 KB |
Output is correct |
70 |
Correct |
3 ms |
4972 KB |
Output is correct |