QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#741799#8050. Random PermutationUESTC_NLNSTL 1ms7772kbC++14775b2024-11-13 15:14:382024-11-13 15:14:41

Judging History

This is the latest submission verdict.

  • [2024-11-13 15:14:41]
  • Judged
  • Verdict: TL
  • Time: 1ms
  • Memory: 7772kb
  • [2024-11-13 15:14:38]
  • Submitted

answer

#include<bits/stdc++.h>
#define lowbit(i) ((i)&(-i))
#define INF 0x3f3f3f3f
using namespace std;
const int N=300005;
int a[N],b[N],n,mx,pos[N],h[N+N];
long long ans;
void solve(int l,int r,int x){
	for(int i=1;i<=n;++i)b[i]=(a[i]<a[x])?-1:1;
	int s=0,as=0;
	for(int i=x;i>=l;--i)s+=b[i],h[s+n]++;
	s=0;
	for(int i=x+1;i<=r;++i)as+=h[n+2-s]+h[n+1-s],s+=b[i];
	as+=h[n+2-s]+h[n+1-s];
	s=0;
	for(int i=x;i>=l;--i)s+=b[i],h[s+n]--;
	ans+=1ll*as*a[x];
	// cerr<<a[x]<<" "<<as<<"\n";
}
signed main(){
	cin.tie(0),cout.tie(0),ios::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i],pos[a[i]]=i;
	for(int i=1;i<=n;++i){
		if(abs(i-n/2)<=500)solve(1,n,pos[i]);
		else solve(max(1,pos[i]-500),min(n,pos[i]+500),pos[i]);
	}cout<<ans;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7772kb

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...

output:


result: