제출 #531699

#제출 시각아이디문제언어결과실행 시간메모리
531699errorgornIzbori (COCI22_izbori)C++17
110 / 110
142 ms15660 KiB
//雪花飄飄北風嘯嘯
//天地一片蒼茫
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
 
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
 
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
 
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
int fen[200005];
 
void add(int i){
	while (i<200005){
		fen[i]++;
		i+=i&-i;
	}
}
 
int query(int i){
	int res=0;
	while (i){
		res+=fen[i];
		i-=i&-i;
	}
	return res;
}
 
int n;
int arr[200005];
vector<int> pos[200005];
 
ll solve(vector<int> v){
	v.pub(n+1);
	
	vector<ii> ranges={ {0,-(v[0]-1)} };
	rep(x,0,sz(v)-1){
		ranges.pub({ranges.back().se+1,ranges.back().se-(v[x+1]-v[x])+2});
	}
	
	vector<int> imp={-(int)1e9};
	rep(x,0,sz(ranges)) imp.pub(ranges[x].fi),imp.pub(ranges[x].fi-1);
	sort(all(imp));
	imp.erase(unique(all(imp)),imp.end());
	
	rep(x,1,sz(imp)) fen[x]=0;
	
	//for (auto it:ranges) cout<<it.fi<<"_"<<it.se<<" "; cout<<endl;
	
	int pos=find(all(imp),ranges[0].fi)-imp.begin()-1;
	
	ll res=0;
	for (auto it:ranges){
		pos++;
		while (true){
			res+=query(pos-1),add(pos);
			if (imp[pos-1]<it.se) break;
			pos--;
		}
	}
	//cout<<res<<endl;
	return res;
}

void read(int &x){
	x=0;
	char ch=getchar_unlocked();
	while (ch&16) x=x*10+(ch&15),ch=getchar_unlocked();
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	read(n);
	rep(x,0,n) read(arr[x]);
	
	vector<int> uni(arr,arr+n);
	sort(all(uni));
	rep(x,0,n) arr[x]=lb(all(uni),arr[x])-uni.begin();
	
	rep(x,0,n) pos[arr[x]].pub(x+1);
	
	ll ans=0;
	rep(x,0,n) ans+=solve(pos[x]);
	
	cout<<ans<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...