// 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()<=800?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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
9776 KB |
Output is correct |
2 |
Correct |
40 ms |
12720 KB |
Output is correct |
3 |
Correct |
22 ms |
7548 KB |
Output is correct |
4 |
Correct |
42 ms |
13388 KB |
Output is correct |
5 |
Correct |
43 ms |
13840 KB |
Output is correct |
6 |
Correct |
48 ms |
14536 KB |
Output is correct |
7 |
Correct |
46 ms |
14528 KB |
Output is correct |
8 |
Correct |
50 ms |
14680 KB |
Output is correct |
9 |
Correct |
46 ms |
14476 KB |
Output is correct |
10 |
Correct |
46 ms |
14448 KB |
Output is correct |
11 |
Correct |
37 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
344 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
348 KB |
Output is correct |
18 |
Correct |
33 ms |
9776 KB |
Output is correct |
19 |
Correct |
40 ms |
12720 KB |
Output is correct |
20 |
Correct |
22 ms |
7548 KB |
Output is correct |
21 |
Correct |
42 ms |
13388 KB |
Output is correct |
22 |
Correct |
43 ms |
13840 KB |
Output is correct |
23 |
Correct |
48 ms |
14536 KB |
Output is correct |
24 |
Correct |
46 ms |
14528 KB |
Output is correct |
25 |
Correct |
50 ms |
14680 KB |
Output is correct |
26 |
Correct |
46 ms |
14476 KB |
Output is correct |
27 |
Correct |
46 ms |
14448 KB |
Output is correct |
28 |
Correct |
37 ms |
14420 KB |
Output is correct |
29 |
Correct |
75 ms |
14536 KB |
Output is correct |
30 |
Correct |
16 ms |
2908 KB |
Output is correct |
31 |
Correct |
35 ms |
5204 KB |
Output is correct |
32 |
Correct |
91 ms |
11816 KB |
Output is correct |
33 |
Correct |
37 ms |
5716 KB |
Output is correct |
34 |
Correct |
39 ms |
5852 KB |
Output is correct |
35 |
Correct |
25 ms |
3932 KB |
Output is correct |
36 |
Correct |
14 ms |
2652 KB |
Output is correct |
37 |
Correct |
18 ms |
2908 KB |
Output is correct |
38 |
Correct |
63 ms |
14512 KB |
Output is correct |
39 |
Correct |
67 ms |
14476 KB |
Output is correct |
40 |
Correct |
64 ms |
14448 KB |
Output is correct |
41 |
Correct |
64 ms |
14528 KB |
Output is correct |
42 |
Correct |
64 ms |
14532 KB |
Output is correct |
43 |
Correct |
133 ms |
15148 KB |
Output is correct |
44 |
Correct |
131 ms |
14908 KB |
Output is correct |
45 |
Correct |
140 ms |
14724 KB |
Output is correct |
46 |
Correct |
131 ms |
14864 KB |
Output is correct |
47 |
Correct |
138 ms |
15164 KB |
Output is correct |
48 |
Correct |
1222 ms |
15008 KB |
Output is correct |
49 |
Correct |
1251 ms |
14972 KB |
Output is correct |
50 |
Correct |
513 ms |
15584 KB |
Output is correct |
51 |
Correct |
522 ms |
15528 KB |
Output is correct |
52 |
Correct |
124 ms |
9936 KB |
Output is correct |
53 |
Correct |
129 ms |
9812 KB |
Output is correct |
54 |
Correct |
1204 ms |
14764 KB |
Output is correct |