Submission #1355059

#TimeUsernameProblemLanguageResultExecution timeMemory
1355059eliArranging Shoes (IOI19_shoes)C++20
10 / 100
24 ms67756 KiB
#include "shoes.h"
#include<bits/stdc++.h>
using namespace std;

#define endl "\n"
#define out(x) #x <<" = "<<x<<"; "
const long long MAX = 1e5 + 10, MAX_ = 2 * 1e5 + 10;
int ll[MAX_], n, m[MAX_];

struct Fenuik
{
	long long sum[MAX_];

	void update_(int x, int val){

		while(x <= n){
			sum[x]+= val;
			x += (x & (-x));
		}

	}

	long long sum_(int x){
		long long ans = 0;

		while(x >= 1){
			ans += sum[x];
			x -= (x & (-x));
		}

		return ans;
	}
}f;

stack<pair<int, bool>> st[MAX];

long long count_swaps(std::vector<int> s) {
	n = s.size();
	long long ans = 0;


	for(int i = 0; i < n; i++){
		//cout<<out(i)<<endl;
		if(st[abs(s[i])].empty()) st[abs(s[i])].push({i, (s[i] >0)? 1:0});
		else{
			if(st[abs(s[i])].top().second && s[i] < 0){
				ll[st[abs(s[i])].top().first] = i;
				st[abs(s[i])].pop();
				//cout<<out(i)<<out(st[abs(s[i])].top().first)<<endl;
			}else if(!st[abs(s[i])].top().second && s[i] > 0){
				ll[st[abs(s[i])].top().first] = i;
				st[abs(s[i])].pop();
				//cout<<out(i)<<out(st[abs(s[i])].top().first)<<endl;
			}else{
				st[abs(s[i])].push({i, (s[i] >0)? 1:0});
			}
		}
	}
	for(int i = 0; i < n; i++) m[i] = 1;
	for(int i = 0; i < n; i++){
		if(m[i] == 0) continue;
		for(int j = i + 1; j <= ll[i]; j++){
			ans += m[j];
		}
		//cout<<out(i)<<out(ll[i])<<out(ans)<<endl;
		m[ll[i]] = 0;
		if(s[i] < 0) ans--;
	}
	//for(int i = 1; i <= n; i++) f.update_(i, 1);
	/*for(int i = 0; i < n; i++){
		if(s[i] == 0) continue;

		ans += f.sum_(ll[i] + 1) - f.sum_(i + 1);
		f.update_(ll[i] + 1, - 1);
		//cout<<out(ans)<<out(ll[i])<<out(i)<<endl;
		if(s[i] < 0) ans --;
		s[ll[i]] = 0;
	}*/
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...