Submission #758794

#TimeUsernameProblemLanguageResultExecution timeMemory
758794BlagojArranging Shoes (IOI19_shoes)C++17
100 / 100
536 ms149452 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()

struct FenwickTree {
	vector<ll> fwt;

	FenwickTree(int n) {
		fwt.resize(n);
	}

	void add(int n) {
		for (n++; n < fwt.size(); n += n & -n) {
			fwt[n]++;
		}
	}

	ll get(int n) {
		ll s = 0;
		for (n++; n > 0; n -= n & -n) {
			s += fwt[n];
		}
		return s;
	}

};

long long count_swaps(vector<int> s) {
	map<int, queue<int>> mp;
	for (int i = 0; i < s.size(); i++) {
		mp[s[i]].push(i);
	}
	int d[s.size() + 2], cur = 1;
	memset(d, 0, sizeof(d));
	for (int i = 0; i < s.size(); i++) {
		if (d[i] != 0) continue;
		if (s[i] < 0) {
			d[i] = cur;
			mp[s[i]].pop();
			d[mp[-s[i]].front()] = cur + 1;
			mp[-s[i]].pop();
		}
		else {
			d[i] = cur + 1;
			mp[s[i]].pop();
			d[mp[-s[i]].front()] = cur;
			mp[-s[i]].pop();
		}
		cur += 2;
	}
	ll ans = 0;
	FenwickTree fwt(s.size() + 5);
	for (int i = s.size() - 1; i >= 0; i--) {
		ans += fwt.get(d[i] - 1);
		fwt.add(d[i]);
	}
	return ans;
}

Compilation message (stderr)

shoes.cpp: In member function 'void FenwickTree::add(int)':
shoes.cpp:18:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |   for (n++; n < fwt.size(); n += n & -n) {
      |             ~~^~~~~~~~~~~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:35:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  for (int i = 0; i < s.size(); i++) {
      |                  ~~^~~~~~~~~~
shoes.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |  for (int i = 0; i < s.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...
#Verdict Execution timeMemoryGrader output
Fetching results...