#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long int
#define vi vector <long long int >
#define pii pair<long long int, long long int>
#define pb push_back
#define pf push_front
#define mod 1000000007
#define ff fflush()
#define mp make_pair
#define vvi vector<vector<ll>>
#define vvpii vector<vector<pair<long long int , long long int >>>
#define vpii vector <pair<long long int , long long int>>
struct node {
ll val,f,h,sub;
struct node *l;
struct node *r;
};
ll freq(struct node *cur){
if(cur==NULL)
return 0;
else
return cur->sub;
}
ll height(struct node *cur){
if(cur==NULL)
return 0;
else
return cur->h;
}
struct node *leftR(struct node *cur){
struct node *y = cur->r->l;
struct node *x = cur->r;
cur->r = y;
cur->sub = freq(cur->l) + freq(cur->r)+cur->f;
cur->h = max(height(cur->l),height(cur->r))+1;
x->l = cur;
x->sub = freq(x->r)+ freq(x->l) + x->f;
x->h = max(height(x->l),height(x->r))+1;
return x;
}
struct node *rightR(struct node *cur){
struct node *y = cur->l->r;
struct node *x = cur->l;
cur->l = y;
cur->sub = freq(cur->l) + freq(cur->r)+cur->f;
cur->h = max(height(cur->l),height(cur->r))+1;
x->r = cur;
x->sub = freq(x->r)+ freq(x->l) + x->f;
x->h = max(height(x->l),height(x->r))+1;
return x;
}
struct node * insert(struct node *cur , ll val , ll * ptr){
if(cur==NULL){
cur = (struct node *)malloc(sizeof(struct node) );
cur->val = val; cur->f = 1; cur->h = 1; cur->sub = 1; cur->l = NULL ; cur->r = NULL;
return cur;
}
if(cur->val==val){
(*ptr) += freq(cur->l);
cur->f++;
cur->sub++;
return cur;
}
else if((cur->val)>val){
cur->sub++;
cur->l = insert(cur->l , val , ptr);
}
else{
cur->sub++;
(*ptr) += freq(cur->l)+cur->f;
cur->r = insert(cur->r , val , ptr);
}
cur->h = max(height(cur->r),height(cur->l))+ 1;
if(height(cur->l)-height(cur->r)>1)
cur = rightR(cur);
else if(height(cur->r)-height(cur->l)>1)
cur = leftR(cur);
return cur;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
ll a[n],c1[n] , c2[n];
for(ll i=0;i<n;i++){
cin>>a[i];
}
struct node *root = NULL;
for(ll i=0;i<n;i++){
ll count = 0;
root = insert(root , a[i], &count);
c1[i] = count;
}
struct node *root1 = NULL;
for(ll i=n-1;i>=0;i--){
ll count = 0;
root1 = insert(root1 , a[i], &count);
c2[i] = count;
}
ll ans = 0;
for(ll i=0;i<n;i++){
// cout<<c1[i]<<" "<<c2[i]<<endl;
ans = ans + c1[i]*c2[i];
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
290 ms |
50396 KB |
Output is correct |
3 |
Correct |
277 ms |
50332 KB |
Output is correct |
4 |
Correct |
279 ms |
50340 KB |
Output is correct |
5 |
Correct |
281 ms |
50340 KB |
Output is correct |
6 |
Correct |
283 ms |
50432 KB |
Output is correct |
7 |
Correct |
281 ms |
50432 KB |
Output is correct |
8 |
Correct |
268 ms |
50436 KB |
Output is correct |
9 |
Correct |
234 ms |
32292 KB |
Output is correct |
10 |
Correct |
238 ms |
32280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
7928 KB |
Output is correct |
2 |
Correct |
36 ms |
7852 KB |
Output is correct |
3 |
Correct |
34 ms |
7852 KB |
Output is correct |
4 |
Correct |
35 ms |
7928 KB |
Output is correct |
5 |
Correct |
37 ms |
7828 KB |
Output is correct |
6 |
Correct |
34 ms |
7848 KB |
Output is correct |
7 |
Correct |
37 ms |
7848 KB |
Output is correct |
8 |
Correct |
29 ms |
7920 KB |
Output is correct |
9 |
Correct |
29 ms |
7848 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
7928 KB |
Output is correct |
2 |
Correct |
36 ms |
7852 KB |
Output is correct |
3 |
Correct |
34 ms |
7852 KB |
Output is correct |
4 |
Correct |
35 ms |
7928 KB |
Output is correct |
5 |
Correct |
37 ms |
7828 KB |
Output is correct |
6 |
Correct |
34 ms |
7848 KB |
Output is correct |
7 |
Correct |
37 ms |
7848 KB |
Output is correct |
8 |
Correct |
29 ms |
7920 KB |
Output is correct |
9 |
Correct |
29 ms |
7848 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
63 ms |
8208 KB |
Output is correct |
12 |
Correct |
68 ms |
8140 KB |
Output is correct |
13 |
Correct |
66 ms |
8112 KB |
Output is correct |
14 |
Correct |
66 ms |
8140 KB |
Output is correct |
15 |
Correct |
82 ms |
8204 KB |
Output is correct |
16 |
Correct |
66 ms |
8228 KB |
Output is correct |
17 |
Correct |
64 ms |
8216 KB |
Output is correct |
18 |
Correct |
48 ms |
8208 KB |
Output is correct |
19 |
Correct |
47 ms |
8192 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
8 ms |
1868 KB |
Output is correct |
12 |
Correct |
8 ms |
1868 KB |
Output is correct |
13 |
Correct |
8 ms |
1992 KB |
Output is correct |
14 |
Correct |
8 ms |
1964 KB |
Output is correct |
15 |
Correct |
10 ms |
1920 KB |
Output is correct |
16 |
Correct |
9 ms |
1984 KB |
Output is correct |
17 |
Correct |
9 ms |
1988 KB |
Output is correct |
18 |
Correct |
7 ms |
1228 KB |
Output is correct |
19 |
Correct |
4 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
7928 KB |
Output is correct |
2 |
Correct |
36 ms |
7852 KB |
Output is correct |
3 |
Correct |
34 ms |
7852 KB |
Output is correct |
4 |
Correct |
35 ms |
7928 KB |
Output is correct |
5 |
Correct |
37 ms |
7828 KB |
Output is correct |
6 |
Correct |
34 ms |
7848 KB |
Output is correct |
7 |
Correct |
37 ms |
7848 KB |
Output is correct |
8 |
Correct |
29 ms |
7920 KB |
Output is correct |
9 |
Correct |
29 ms |
7848 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
63 ms |
8208 KB |
Output is correct |
12 |
Correct |
68 ms |
8140 KB |
Output is correct |
13 |
Correct |
66 ms |
8112 KB |
Output is correct |
14 |
Correct |
66 ms |
8140 KB |
Output is correct |
15 |
Correct |
82 ms |
8204 KB |
Output is correct |
16 |
Correct |
66 ms |
8228 KB |
Output is correct |
17 |
Correct |
64 ms |
8216 KB |
Output is correct |
18 |
Correct |
48 ms |
8208 KB |
Output is correct |
19 |
Correct |
47 ms |
8192 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
275 ms |
21064 KB |
Output is correct |
22 |
Correct |
291 ms |
20892 KB |
Output is correct |
23 |
Correct |
337 ms |
20904 KB |
Output is correct |
24 |
Correct |
280 ms |
20860 KB |
Output is correct |
25 |
Correct |
311 ms |
20872 KB |
Output is correct |
26 |
Correct |
295 ms |
20904 KB |
Output is correct |
27 |
Correct |
321 ms |
20960 KB |
Output is correct |
28 |
Correct |
184 ms |
20952 KB |
Output is correct |
29 |
Correct |
191 ms |
20916 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
290 ms |
50396 KB |
Output is correct |
3 |
Correct |
277 ms |
50332 KB |
Output is correct |
4 |
Correct |
279 ms |
50340 KB |
Output is correct |
5 |
Correct |
281 ms |
50340 KB |
Output is correct |
6 |
Correct |
283 ms |
50432 KB |
Output is correct |
7 |
Correct |
281 ms |
50432 KB |
Output is correct |
8 |
Correct |
268 ms |
50436 KB |
Output is correct |
9 |
Correct |
234 ms |
32292 KB |
Output is correct |
10 |
Correct |
238 ms |
32280 KB |
Output is correct |
11 |
Correct |
35 ms |
7928 KB |
Output is correct |
12 |
Correct |
36 ms |
7852 KB |
Output is correct |
13 |
Correct |
34 ms |
7852 KB |
Output is correct |
14 |
Correct |
35 ms |
7928 KB |
Output is correct |
15 |
Correct |
37 ms |
7828 KB |
Output is correct |
16 |
Correct |
34 ms |
7848 KB |
Output is correct |
17 |
Correct |
37 ms |
7848 KB |
Output is correct |
18 |
Correct |
29 ms |
7920 KB |
Output is correct |
19 |
Correct |
29 ms |
7848 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
63 ms |
8208 KB |
Output is correct |
22 |
Correct |
68 ms |
8140 KB |
Output is correct |
23 |
Correct |
66 ms |
8112 KB |
Output is correct |
24 |
Correct |
66 ms |
8140 KB |
Output is correct |
25 |
Correct |
82 ms |
8204 KB |
Output is correct |
26 |
Correct |
66 ms |
8228 KB |
Output is correct |
27 |
Correct |
64 ms |
8216 KB |
Output is correct |
28 |
Correct |
48 ms |
8208 KB |
Output is correct |
29 |
Correct |
47 ms |
8192 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
8 ms |
1868 KB |
Output is correct |
42 |
Correct |
8 ms |
1868 KB |
Output is correct |
43 |
Correct |
8 ms |
1992 KB |
Output is correct |
44 |
Correct |
8 ms |
1964 KB |
Output is correct |
45 |
Correct |
10 ms |
1920 KB |
Output is correct |
46 |
Correct |
9 ms |
1984 KB |
Output is correct |
47 |
Correct |
9 ms |
1988 KB |
Output is correct |
48 |
Correct |
7 ms |
1228 KB |
Output is correct |
49 |
Correct |
4 ms |
588 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
275 ms |
21064 KB |
Output is correct |
52 |
Correct |
291 ms |
20892 KB |
Output is correct |
53 |
Correct |
337 ms |
20904 KB |
Output is correct |
54 |
Correct |
280 ms |
20860 KB |
Output is correct |
55 |
Correct |
311 ms |
20872 KB |
Output is correct |
56 |
Correct |
295 ms |
20904 KB |
Output is correct |
57 |
Correct |
321 ms |
20960 KB |
Output is correct |
58 |
Correct |
184 ms |
20952 KB |
Output is correct |
59 |
Correct |
191 ms |
20916 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
556 ms |
50392 KB |
Output is correct |
62 |
Correct |
533 ms |
50392 KB |
Output is correct |
63 |
Correct |
566 ms |
50444 KB |
Output is correct |
64 |
Correct |
590 ms |
50436 KB |
Output is correct |
65 |
Correct |
638 ms |
50452 KB |
Output is correct |
66 |
Correct |
593 ms |
50376 KB |
Output is correct |
67 |
Correct |
545 ms |
50460 KB |
Output is correct |
68 |
Correct |
445 ms |
32364 KB |
Output is correct |
69 |
Correct |
344 ms |
25360 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |