/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
const int N=2e5+5;
int n,arr[N];
ll solve(int l,int r){
if(l>r){
return 0;
}
if(l==r){
return 1;
}
int mid=l+r>>1;
ll ret=0;
set<int>s;
map<int,int>fr;
for(int i=mid;i<=r;i++){
fr[arr[i]]++;
if(fr[arr[i]]*2>i-mid+1){
s.insert(arr[i]);
}
}
fr.clear();
for(int i=mid;i>=l;i--){
fr[arr[i]]++;
if(fr[arr[i]]*2>mid-i){
s.insert(arr[i]);
}
}
for(auto x:s){// atmost log n numbers
fr.clear();
int prefix=0;
fr[prefix]++;
for(int i=l;i<mid;i++){
prefix+=(arr[i]==x?1:-1);
fr[prefix]++;
}
int sz=(r-l+1);
for(int i=-sz;i<=sz;i++){
fr[i]+=fr[i-1];
}
for(int i=mid;i<=r;i++){
prefix+=(arr[i]==x?1:-1);
ret+=fr[prefix-1];
}
}
return ret+solve(l,mid-1)+solve(mid+1,r);
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
/// freopen("input.in", "r", stdin);
/// freopen("output.out", "w", stdout);
#endif
fast
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<solve(0,n-1)<<endl;
return 0;
}
Compilation message
Main.cpp: In function 'll solve(int, int)':
Main.cpp:47:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
852 KB |
Output is correct |
10 |
Correct |
8 ms |
604 KB |
Output is correct |
11 |
Correct |
9 ms |
852 KB |
Output is correct |
12 |
Correct |
9 ms |
604 KB |
Output is correct |
13 |
Correct |
9 ms |
752 KB |
Output is correct |
14 |
Correct |
9 ms |
604 KB |
Output is correct |
15 |
Correct |
9 ms |
612 KB |
Output is correct |
16 |
Correct |
9 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
978 ms |
25856 KB |
Output is correct |
2 |
Correct |
1354 ms |
33780 KB |
Output is correct |
3 |
Correct |
736 ms |
19980 KB |
Output is correct |
4 |
Correct |
1416 ms |
35344 KB |
Output is correct |
5 |
Correct |
1470 ms |
36704 KB |
Output is correct |
6 |
Correct |
1566 ms |
38796 KB |
Output is correct |
7 |
Correct |
1560 ms |
38996 KB |
Output is correct |
8 |
Correct |
1569 ms |
38796 KB |
Output is correct |
9 |
Correct |
1626 ms |
39052 KB |
Output is correct |
10 |
Correct |
1574 ms |
38788 KB |
Output is correct |
11 |
Correct |
994 ms |
38628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
12 ms |
852 KB |
Output is correct |
10 |
Correct |
8 ms |
604 KB |
Output is correct |
11 |
Correct |
9 ms |
852 KB |
Output is correct |
12 |
Correct |
9 ms |
604 KB |
Output is correct |
13 |
Correct |
9 ms |
752 KB |
Output is correct |
14 |
Correct |
9 ms |
604 KB |
Output is correct |
15 |
Correct |
9 ms |
612 KB |
Output is correct |
16 |
Correct |
9 ms |
600 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
978 ms |
25856 KB |
Output is correct |
19 |
Correct |
1354 ms |
33780 KB |
Output is correct |
20 |
Correct |
736 ms |
19980 KB |
Output is correct |
21 |
Correct |
1416 ms |
35344 KB |
Output is correct |
22 |
Correct |
1470 ms |
36704 KB |
Output is correct |
23 |
Correct |
1566 ms |
38796 KB |
Output is correct |
24 |
Correct |
1560 ms |
38996 KB |
Output is correct |
25 |
Correct |
1569 ms |
38796 KB |
Output is correct |
26 |
Correct |
1626 ms |
39052 KB |
Output is correct |
27 |
Correct |
1574 ms |
38788 KB |
Output is correct |
28 |
Correct |
994 ms |
38628 KB |
Output is correct |
29 |
Correct |
1010 ms |
38792 KB |
Output is correct |
30 |
Correct |
292 ms |
7716 KB |
Output is correct |
31 |
Correct |
677 ms |
15632 KB |
Output is correct |
32 |
Correct |
1963 ms |
37792 KB |
Output is correct |
33 |
Correct |
747 ms |
16624 KB |
Output is correct |
34 |
Correct |
788 ms |
17372 KB |
Output is correct |
35 |
Correct |
477 ms |
11432 KB |
Output is correct |
36 |
Correct |
265 ms |
7020 KB |
Output is correct |
37 |
Correct |
320 ms |
8440 KB |
Output is correct |
38 |
Correct |
1856 ms |
39200 KB |
Output is correct |
39 |
Correct |
1738 ms |
39188 KB |
Output is correct |
40 |
Correct |
1922 ms |
39204 KB |
Output is correct |
41 |
Correct |
1840 ms |
39416 KB |
Output is correct |
42 |
Correct |
1947 ms |
39188 KB |
Output is correct |
43 |
Correct |
1777 ms |
40764 KB |
Output is correct |
44 |
Correct |
1769 ms |
40772 KB |
Output is correct |
45 |
Correct |
1756 ms |
40772 KB |
Output is correct |
46 |
Correct |
1781 ms |
40908 KB |
Output is correct |
47 |
Correct |
1767 ms |
40772 KB |
Output is correct |
48 |
Correct |
1901 ms |
39724 KB |
Output is correct |
49 |
Correct |
1907 ms |
39636 KB |
Output is correct |
50 |
Correct |
1844 ms |
39604 KB |
Output is correct |
51 |
Correct |
1922 ms |
39364 KB |
Output is correct |
52 |
Correct |
1879 ms |
39532 KB |
Output is correct |
53 |
Correct |
1813 ms |
39528 KB |
Output is correct |
54 |
Correct |
1793 ms |
39652 KB |
Output is correct |