// is sqrt intended
// and is n^1.5 log^0.5 n intended
// i guess the tl is 3sec so
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int hi(int a, int b, int c, int d, int e) {
// [a,b] + [c,d] <= e.
if(b-a<=d-c) {swap(a,c);swap(b,d);}
if(e<a+c) return 0;
if(e>b+d) return (b-a+1)*(d-c+1);
if(e<=a+d) return (e-a-c+1)*(e-a-c+2)/2;
if(e>=b+c) return (b-a+1)*(d-c+1)-(b+d-e)*(b+d+1-e)/2;
return (e-a-d) * (d-c+1) + (d-c+1) * (d-c+2) / 2;
}
int calcA(int n, vector<int> &v) {
int s = v.size(), ans = 0;
for(int i=0;i<s;i++) for(int j=i;j<s;j++) {
int l = (i?v[i-1]+1:0);
int r = (j==s-1?n-1:v[j+1]-1);
ans += hi(-v[i],-l,v[j],r,(j-i)<<1);
}
return ans;
}
// 2pre[r] - r > 2pre[l] - l
struct fenwick {
int n; vector<int> fw;
fenwick(int _n): n(_n), fw(_n+1,0) {}
inline void up(int x) {for(;x<=n;x+=(x&(-x))) ++fw[x];}
inline int qry(int x) {
int ans = 0;
for(;x;x-=(x&(-x))) ans+=fw[x];
return ans;
}
};
int calcB(int n, vector<int> &v) {
fenwick fw(n<<1|1);
fw.up(n+1);
int a[n]; fill(a,a+n,-1); for(auto i: v) a[i]=1;
int ans = 0;
for(int i=0,s=0;i<n;i++) {
s += a[i];
ans += fw.qry(s+n);
fw.up(s+n+1);
}
return ans;
}
main() {
int n; cin >> n;
int a[n], b[n]; for(int i=0;i<n;i++) {cin>>a[i];b[i]=a[i];}
sort(b,b+n);
for(int i=0;i<n;i++) a[i]=lower_bound(b,b+n,a[i])-b;
vector<int> v[n];
for(int i=0;i<n;i++) v[a[i]].push_back(i);
int ans = 0;
for(int i=0;i<n;i++) {
/*
if(calcA(n,v[i]) != calcB(n,v[i])) {
for(auto j: v[i]) cout << j << " "; cout << "\n";
cout << calcA(n,v[i]) << " " << calcB(n,v[i]);
return -1;
}
*/
ans += (v[i].size()<=666?calcA(n,v[i]):calcB(n,v[i]));
}
cout << ans;
}
Compilation message
Main.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
520 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
10184 KB |
Output is correct |
2 |
Correct |
39 ms |
13052 KB |
Output is correct |
3 |
Correct |
26 ms |
7800 KB |
Output is correct |
4 |
Correct |
43 ms |
13492 KB |
Output is correct |
5 |
Correct |
49 ms |
14320 KB |
Output is correct |
6 |
Correct |
46 ms |
15044 KB |
Output is correct |
7 |
Correct |
46 ms |
15040 KB |
Output is correct |
8 |
Correct |
48 ms |
15088 KB |
Output is correct |
9 |
Correct |
46 ms |
14880 KB |
Output is correct |
10 |
Correct |
47 ms |
14828 KB |
Output is correct |
11 |
Correct |
44 ms |
15300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
520 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
34 ms |
10184 KB |
Output is correct |
19 |
Correct |
39 ms |
13052 KB |
Output is correct |
20 |
Correct |
26 ms |
7800 KB |
Output is correct |
21 |
Correct |
43 ms |
13492 KB |
Output is correct |
22 |
Correct |
49 ms |
14320 KB |
Output is correct |
23 |
Correct |
46 ms |
15044 KB |
Output is correct |
24 |
Correct |
46 ms |
15040 KB |
Output is correct |
25 |
Correct |
48 ms |
15088 KB |
Output is correct |
26 |
Correct |
46 ms |
14880 KB |
Output is correct |
27 |
Correct |
47 ms |
14828 KB |
Output is correct |
28 |
Correct |
44 ms |
15300 KB |
Output is correct |
29 |
Correct |
72 ms |
16764 KB |
Output is correct |
30 |
Correct |
16 ms |
3152 KB |
Output is correct |
31 |
Correct |
35 ms |
5724 KB |
Output is correct |
32 |
Correct |
91 ms |
12832 KB |
Output is correct |
33 |
Correct |
45 ms |
6052 KB |
Output is correct |
34 |
Correct |
41 ms |
6464 KB |
Output is correct |
35 |
Correct |
25 ms |
4360 KB |
Output is correct |
36 |
Correct |
14 ms |
2664 KB |
Output is correct |
37 |
Correct |
17 ms |
3416 KB |
Output is correct |
38 |
Correct |
63 ms |
14952 KB |
Output is correct |
39 |
Correct |
65 ms |
15044 KB |
Output is correct |
40 |
Correct |
64 ms |
14964 KB |
Output is correct |
41 |
Correct |
63 ms |
14976 KB |
Output is correct |
42 |
Correct |
64 ms |
14928 KB |
Output is correct |
43 |
Correct |
132 ms |
17000 KB |
Output is correct |
44 |
Correct |
133 ms |
16984 KB |
Output is correct |
45 |
Correct |
131 ms |
16772 KB |
Output is correct |
46 |
Correct |
131 ms |
16776 KB |
Output is correct |
47 |
Correct |
148 ms |
16944 KB |
Output is correct |
48 |
Correct |
1222 ms |
15648 KB |
Output is correct |
49 |
Correct |
1221 ms |
15728 KB |
Output is correct |
50 |
Correct |
513 ms |
16156 KB |
Output is correct |
51 |
Correct |
515 ms |
16100 KB |
Output is correct |
52 |
Correct |
124 ms |
10688 KB |
Output is correct |
53 |
Correct |
135 ms |
10684 KB |
Output is correct |
54 |
Correct |
1204 ms |
15460 KB |
Output is correct |