Submission #1063568

#TimeUsernameProblemLanguageResultExecution timeMemory
1063568aaaaaarrozArranging Shoes (IOI19_shoes)C++17
0 / 100
0 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){
    		bool menor=(nose.second.first<nose.second.second);
    		cnt+=(max(nose.second.second,nose.second.first)-min(nose.second.second,nose.second.first)-menor-st.query(min(nose.second.second,nose.second.first)+1,max(nose.second.second,nose.second.first)-1));
    		st.update(max(nose.second.second,nose.second.first),1);
    	}
    	return cnt;
    }

Compilation message (stderr)

shoes.cpp: In member function 'T IterativeSegmentTree<T, m_>::query(int, int)':
shoes.cpp:24:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   24 |         if (!hasL) return ansR; if (!hasR) return ansL;
      |         ^~
shoes.cpp:24:33: 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:49:102: warning: 'ansR' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |       cnt+=(max(nose.second.second,nose.second.first)-min(nose.second.second,nose.second.first)-menor-st.query(min(nose.second.second,nose.second.first)+1,max(nose.second.second,nose.second.first)-1));
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...