QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353308#8050. Random Permutationucup-team134#TL 1ms5928kbC++177.7kb2024-03-14 02:18:562024-03-14 02:18:56

Judging History

你现在查看的是最新测评结果

  • [2024-03-14 02:18:56]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5928kb
  • [2024-03-14 02:18:56]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ld long double
#define li __int128

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<int D, typename T>struct vec : public vector<vec<D - 1, T>> {template<typename... Args>vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)) {}};
template<typename T>struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val) {}};
template<class T1,class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const deque<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T,greater<T> >& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
int ri(){int x;scanf("%i",&x);return x;}
void rd(int&x){scanf("%i",&x);}
void rd(long long&x){scanf("%lld",&x);}
void rd(double&x){scanf("%lf",&x);}
void rd(long double&x){scanf("%Lf",&x);}
void rd(string&x){cin>>x;}
void rd(char*x){scanf("%s",x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&p:x)rd(p);}
template<typename C,typename...T>void rd(C&a,T&...args){rd(a);rd(args...);}
//istream& operator>>(istream& is,__int128& a){string s;is>>s;a=0;int i=0;bool neg=false;if(s[0]=='-')neg=true,i++;for(;i<s.size();i++)a=a*10+s[i]-'0';if(neg)a*=-1;return is;}
//ostream& operator<<(ostream& os,__int128 a){bool neg=false;if(a<0)neg=true,a*=-1;ll high=(a/(__int128)1e18);ll low=(a-(__int128)1e18*high);string res;if(neg)res+='-';if(high>0){res+=to_string(high);string temp=to_string(low);res+=string(18-temp.size(),'0');res+=temp;}else res+=to_string(low);os<<res;return os;}

const int N=3e5+5,C=103;
int n;
int p[N];
int mapa[N];
int undoMap[N];
ll ans=0;
vector<int> vals;
// {{A, even/odd},{m[i]+1,m[i+1]-1}}
vector<pair<pair<int,int>,pair<int,int>>> na,nb;
int cnt[2][2*C+1][N];
int mxC=0;
void pr2(int n){
	for(int i=0;i<2;i++){
		for(int j=-C;j<=C;j++){
			for(int k=0;k<n;k++){
				cnt[i][j+C][k]=0;
			}
		}
	}
	for(auto p:nb){
		cnt[p.f.s][p.f.f+C][p.s.f]++;
		cnt[p.f.s][p.f.f+C][p.s.s+1]--;
	}
	for(int i=0;i<2;i++){
		for(int j=-C;j<=C;j++){
			for(int k=1;k<n;k++){
				cnt[i][j+C][k]+=cnt[i][j+C][k-1];
			}
		}
	}
	for(auto p:na){
		for(int b=0;b<2;b++){
			int v2;
			if(p.f.s==0){
				v2=-p.f.f;
			}
			else{
				if(b==0){
					v2=-p.f.f;
				}
				else{
					v2=-p.f.f-1;
				}
			}
			if(v2<-C||v2>C)continue;
			int br=cnt[b][v2+C][p.s.f-1];
			/*if(br){
				cout << p << " " << b << " " << br << endl;
			}*/
			if(br){
				mxC=max(mxC,abs(p.f.f));
			}
			ans+=(ll)undoMap[p.s.f-1] * br;
		}
	}
}
void process(){
	na.clear();
	nb.clear();
	int n=sz(vals);
	int m=(n-1)/2;
	set<int> low,high;
	for(int i=m;i>=0;i--){
		int x=vals[i];
		if(sz(low)==0){
			low.insert(x);
		}
		else{
			int a=*low.rbegin();
			if(x>a){
				high.insert(x);
			}
			else{
				low.insert(x);
			}
			while(sz(low)-1>sz(high)){
				int b=*low.rbegin();
				low.erase(b);
				high.insert(b);
			}
			while(sz(high)>sz(low)){
				int b=*high.begin();
				high.erase(b);
				low.insert(b);
			}
		}
		//cout << i << low << high << endl;
		int s=sz(low)+sz(high);
		auto it=high.begin();
		for(int a=0;a<sz(high);a++){
			if(a+1>C)break;
			int tr=*it;
			it++;
			int nxt;
			if(it==high.end()){
				nxt=n;
			}
			else{
				nxt=*it;
			}
			na.pb({{a+1,s&1},{tr+1,nxt-1}});
		}
		auto it2=low.end();
		for(int a=0;a<=sz(low);a++){
			if(a>C)break;
			int nxt;
			if(it2==low.end()){
				if(sz(high)==0){
					nxt=n;
				}
				else
					nxt=*high.begin();
			}
			else{
				nxt=*it2;
			}
			int tr;
			if(a==sz(low)){
				tr=-1;
			}
			else{
				it2--;
				tr=*it2;
			}
			na.pb({{-a,s&1},{tr+1,nxt-1}});
		}
	}
	low.clear();
	high.clear();
	for(int i=m+1;i<n;i++){
		int x=vals[i];
		if(sz(low)==0){
			low.insert(x);
		}
		else{
			int a=*low.rbegin();
			if(x>a){
				high.insert(x);
			}
			else{
				low.insert(x);
			}
			while(sz(low)-1>sz(high)){
				int b=*low.rbegin();
				low.erase(b);
				high.insert(b);
			}
			while(sz(high)>sz(low)){
				int b=*high.begin();
				high.erase(b);
				low.insert(b);
			}
		}
		//cout << i << low << high << endl;
		int s=sz(low)+sz(high);
		auto it=high.begin();
		for(int a=0;a<sz(high);a++){
			if(a+1>=C)break;
			int tr=*it;
			it++;
			int nxt;
			if(it==high.end()){
				nxt=n;
			}
			else{
				nxt=*it;
			}
			nb.pb({{a+1,s&1},{tr+1,nxt-1}});
		}
		auto it2=low.end();
		for(int a=0;a<=sz(low);a++){
			if(a>=C)break;
			int nxt;
			if(it2==low.end()){
				if(sz(high)==0){
					nxt=n;
				}
				else
					nxt=*high.begin();
			}
			else{
				nxt=*it2;
			}
			int tr;
			if(a==sz(low)){
				tr=-1;
			}
			else{
				it2--;
				tr=*it2;
			}
			nb.pb({{-a,s&1},{tr+1,nxt-1}});
		}
	}
	//cout << na << endl << nb << endl;
	pr2(n);
	swap(na,nb);
	//printf("Swap!\n");
	pr2(n);
}
void solve(int l,int r){
	if(r-l<5){
		vector<int> a;
		for(int i=l;i<=r;i++){
			for(int j=i;j<=r;j++){
				a.clear();
				for(int k=i;k<=j;k++){
					a.pb(p[k]);
				}
				sort(all(a));
				ans+=a[(sz(a)-1)/2];
			}
		}
		return;
	}
	int m=(l+r)>>1;
	solve(l,m);
	solve(m+1,r);
	
	// Solve it :D
	vals.clear();
	for(int i=l;i<=r;i++){
		vals.pb(p[i]);
	}
	sort(all(vals));
	for(int i=0;i<sz(vals);i++){
		mapa[vals[i]]=i;
		undoMap[i]=vals[i];
	}
	vals.clear();
	for(int i=l;i<=r;i++){
		vals.pb(mapa[p[i]]);
	}
	//cout << vals << endl;
	process();
}
bool my=0;
int main()
{
	if(my){
		n=ri();
		vector<int> pr(n);
		iota(all(pr),1);
		shuffle(all(pr),rng);
		for(int i=0;i<n;i++){
			p[i]=pr[i];
		}
		//cout << pr << endl;
	}
	else{
		n=ri();
		for(int i=0;i<n;i++){
			p[i]=ri();
		}
	}
	solve(0,n-1);
	/*ll brute=0;
	vector<int> a;
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			a.clear();
			for(int k=i;k<=j;k++){
				a.pb(p[k]);
			}
			sort(all(a));
			brute+=a[(sz(a)-1)/2];
		}
	}
	if(ans!=brute)
	printf("%lld %lld\n",ans,brute);*/
	//printf("%i\n",mxC);
	printf("%lld\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: