Submission #1003892

#TimeUsernameProblemLanguageResultExecution timeMemory
1003892vjudge1Ekoeko (COCI21_ekoeko)C++17
110 / 110
33 ms5276 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 5e5+10;
const int inf = 1e18+10;
const int lim = 2e5;
int bit[lim+10];
void upd(int pos, int val) {
	for(int i = pos; i <= lim; i+= i&-i) {
		bit[i]+= val;
	}
}

int qry(int pos) {
	int val = 0;
	for(int i = pos; i > 0; i-= i&-i) {
		val+= bit[i];
	}
	return val;
}

int32_t main() {
	// #ifndef ONLINE_JUDGE
	// freopen("in.in","r",stdin);
	// freopen("out.out","w",stdout);
	// #endif

	int n;
	cin >> n;
	string s;
	cin >> s;

	map<char,int> cnt;
	for(auto x : s) {
		cnt[x]++;
	}

	string t;
	for(int i = 0; i < 2*n; i++) {
		if(cnt[s[i]] > 0) {
			cnt[s[i]]-= 2;
			t+= s[i];
		}
	}
	map<char,vector<int>> post;
	for(int i = 0; i < t.size(); i++) {
		post[t[i]].pb(i);
	}

	for(auto x : post) {
		reverse(all(post[x.fr]));
	}

	for(auto x : s) {
		cnt[x]++;
	}
	int l = 1;
	int r = n+1;
	vector<int> pos(2*n);

	for(int i = 0; i < 2*n; i++) {
		if(cnt[s[i]] > 0) {
			cnt[s[i]]-= 2;
			pos[i] = l;
			l++;
		}
		else {
			pos[i] = r+post[s[i]].back();
			post[s[i]].pop_back();
		}
	}

	int ans = 0;
	for(int i = 0; i < 2*n; i++) {
		ans+= i-qry(pos[i]);
		upd(pos[i],1);
	}
	cout << ans << endl;
}

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:51:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |  for(int i = 0; i < t.size(); i++) {
      |                 ~~^~~~~~~~~~
#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...