QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#827#565048#9313. Make Maxkircomiku_HYFailed.2024-09-17 15:31:132024-09-17 15:31:14

Details

Extra Test:

Accepted
time: 1ms
memory: 5688kb

input:

1
10
1 1 4 3 4 5 3 3 2 4

output:

16

result:

ok 1 number(s): "16"

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#565048#9313. Make Maxmiku_HYAC ✓519ms43808kbC++201.4kb2024-09-15 20:02:062024-09-18 15:56:25

answer

#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define endl '\n'

using namespace std;
using ll =long long;
using pll=pair<ll,ll>;
const int mod=998244353;
ll ksm(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1){
			ans=(ans*a)%mod;
		}
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
ll gcd(ll a,ll b){
	ll t=a%b;
	while(t){
		a=b;b=t;t=a%b;
	}
	return b;
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
ll get_inv(ll x){
	return ksm(x,mod-2);
}
const int N=5e5+10;
bool cmp(pair<ll,string>a,pair<ll,string>b){
	if(a.x==b.x)return a.y<b.y;
	return a.x>b.x;
}
ll a[N];
ll b[N];
void solve(){
	ll n;
	cin>>n;
	map<ll,ll>mp;
	ll idx=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];b[i]=a[i];
	}
	sort(b+1,b+1+n);
	for(int i=1;i<=n;i++){
		if(!mp[b[i]])mp[b[i]]=++idx;
	}
	for(int i=1;i<=n;i++){
		a[i]=mp[a[i]];
	}
	vector<vector<ll>>w(idx+1);
	for(int i=1;i<=n;i++){
		w[a[i]].pb(i);
	}
	set<ll>st;st.insert(0);
	st.insert(n+1);
	ll ans=0;
	for(int i=idx;i>=1;i--){
		ll mx=0;
		for(auto x:w[i]){
			auto t=st.lower_bound(x);
			t--;
			if(x<=mx){
				ans--;continue;
			}
			ans+=x-1ll-*t;
			t=st.upper_bound(x);
			ans+=*t-x-1ll;
			mx=max(mx,*t);
		}
		for(auto x:w[i])st.insert(x);
	}
	cout<<ans<<endl;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int _=1;
	cin>>_;
	while(_--)solve();
	return 0;
}