Submission #1063578

#TimeUsernameProblemLanguageResultExecution timeMemory
1063578aaaaaarrozArranging Shoes (IOI19_shoes)C++17
10 / 100
1 ms348 KiB
#include <bits/stdc++.h>
using namespace std;
template<class T, T m_(T, T)> struct IterativeSegmentTree{
	int n; vector<T> ST;
	IterativeSegmentTree(){}
	IterativeSegmentTree(vector<T> &a){
	n = a.size(); ST.resize(n << 1);
	for (int i=n;i<(n<<1);i++)ST[i]=a[i-n];
	for (int i=n-1;i>0;i--)ST[i]=m_(ST[i<<1],ST[i<<1|1]);
	}
	void update(int pos, T val){ // replace with val
	ST[pos += n] += val;
	for (pos >>= 1; pos > 0; pos >>= 1)
		ST[pos] = m_(ST[pos<<1], ST[pos<<1|1]);
	}
	T query(int l, int r){ // [l, r]
	T ansL, ansR; bool hasL = 0, hasR = 0;
	for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
		if (l & 1) 
		ansL=(hasL?m_(ansL,ST[l++]):ST[l++]),hasL=1;
		if (r & 1) 
		ansR=(hasR?m_(ST[--r],ansR):ST[--r]),hasR=1;
	}
	if (!hasL) return ansR; if (!hasR) return ansL;
	return m_(ansL, ansR);
	}
};
int merge(int a, int b){
	return a+b;
}
long long count_swaps(vector<int> v) {
	int n=v.size();
	vector<int>vacio(n,0);
	IterativeSegmentTree<int,merge>st(vacio);
	long long cnt=0;
	map<int, pair<int,int>>pos;
	int position=0;
	for(int zapato:v){
		if(zapato<0){
			pos[zapato].first=position;
		}
		else{
			pos[zapato].second=position;
		}
		position++;
	}
	for(auto &nose:pos){
		int max_pos=max(nose.second.first,nose.second.second);
		int min_pos=min(nose.second.first,nose.second.second);
		int der=max_pos+st.query(0,max_pos);
		int izq=min_pos+st.query(0,min_pos);
		bool menor=(nose.second.first<nose.second.second);
		cnt+=(der-izq-menor);
		st.update(max_pos,1);
	}
	return cnt;
}

Compilation message (stderr)

shoes.cpp: In member function 'T IterativeSegmentTree<T, m_>::query(int, int)':
shoes.cpp:24:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   24 |  if (!hasL) return ansR; if (!hasR) return ansL;
      |  ^~
shoes.cpp:24:26: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   24 |  if (!hasL) return ansR; if (!hasR) return ansL;
      |                          ^~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:51:37: warning: 'ansR' may be used uninitialized in this function [-Wmaybe-uninitialized]
   51 |   int izq=min_pos+st.query(0,min_pos);
      |                                     ^
shoes.cpp:50:37: warning: 'ansR' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |   int der=max_pos+st.query(0,max_pos);
      |                                     ^
#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...