Submission #437909

# Submission time Handle Problem Language Result Execution time Memory
437909 2021-06-27T09:17:33 Z dajma Arranging Shoes (IOI19_shoes) C++14
100 / 100
979 ms 152648 KB
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#endif
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << '\n'; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
typedef tree< pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
#define sz(x) (int)(x).size()
const ll MOD = 1e9 + 7 ;
const ll N = 2e5 + 10 ;
const ll INF = 1e18 + 10 ;
#define endl '\n'
// remove above line when interactive
/*
Some Notes:

-2 2 -2 2 -2 2
0 1 2 3 4 5
 0 1  0 1 


2 2 2 -2 -2 -2
3     4


*/
template <typename TYPE>
class FenwickTree
{
    private:
        TYPE * add;
        TYPE * mul;
        size_t length;

        // performs the actual range update
        void range_update_helper(size_t pos, TYPE mulfact, TYPE addfact)
        {
            pos += 1;
            while(pos <= length)
            {
                mul[pos - 1] += mulfact;
                add[pos - 1] += addfact;
                pos += pos & -pos;
            }
        }

    public:
        // constructor with vector
        FenwickTree(std::vector<TYPE>& v)
        {
            length = v.size();
            add = new TYPE[length];
            mul = new TYPE[length];

            // initialize
            for(size_t i = 0; i < length; ++i)
            {
                add[i] = 0;
                mul[i] = 0;
            }

            // point-wise updates
            for(size_t i = 0; i < length; ++i)
            {
                point_update(i, v[i]);
            }
        }

        // constructor with an array v[] of given size input_length
        FenwickTree(TYPE v[], int input_length)
        {
            length = input_length;
            add = new TYPE[length];
            mul = new TYPE[length];

            // initialize
            for(size_t i = 0; i < length; ++i)
            {
                add[i] = 0;
                mul[i] = 0;
            }

            // point-wise updates
            for(size_t i = 0; i < length; ++i)
            {
                point_update(i, v[i]);
            }
        }

        // add value 'val' to position i in a[0..length - 1]
        void point_update(size_t i, TYPE val)
        {
            range_update(i, i, val);
        }

        // add value 'val' to postions a[i..j]
        void range_update(size_t i, size_t j, TYPE val)
        {
            if(i < 0 or j < i or j >= length)
            {
                throw std::invalid_argument("subarray range invalid.");
            }
            else
            {
                range_update_helper(i, val, -val * ((TYPE) i - 1));
                range_update_helper(j, -val, val * j);
            }
        }

        // find sum of range a[i..j] (both inclusive)
        TYPE range_sum(size_t i, size_t j)
        {
            if(i < 0 or j < i or j >= length)
            {
                throw std::invalid_argument("subarray range invalid.");
            }
            else
            {
                if(i > 0)
                {
                    return prefix_sum(j) - prefix_sum(i - 1);
                }
                else
                {
                    return prefix_sum(j);
                }
            }
        }

        // find sum of range a[0..i] (both inclusive)
        TYPE prefix_sum(size_t i)
        {
            TYPE mulfact = 0;
            TYPE addfact = 0;

            size_t startindex = i;
            i += 1;

            while(i > 0)
            {
                addfact += add[i - 1];
                mulfact += mul[i - 1];
                i = i & (i - 1);
            }

            return ((TYPE) startindex) * mulfact + addfact;
        }

        // returns the value of a[i]
        TYPE point_sum(size_t i)
        {
            if(i < 0 or i >= length)
            {
                throw std::invalid_argument("invalid array index.");
            }
            else
            {
                return range_sum(i, i);
            }
        }
};

ll count_swaps(vector<int> t) {
    int n = sz(t);
    map<int, deque<int>> mp;
    for (int i = 0; i < n; i++) {
    	int x = t[i];
    	mp[x].push_back(i);
    }
    vector<int> v(n, -1);
    int cur = 0;
    for (int i = 0; i < n; i++) {
    	if (v[i] != -1) continue;
    	if (t[i] < 0) {
    		v[i] = cur++;
    		mp[t[i]].pop_front();
    		v[mp[-t[i]].front()] = cur++;
    		mp[-t[i]].pop_front();
    	}
    	else {
    		v[i] = cur + 1;
    		mp[t[i]].pop_front();
    		v[mp[-t[i]].front()] = cur;
    		mp[-t[i]].pop_front();
    		cur += 2;
    	}
    }
    debug() << imie(v);
    ll inversions = 0;
    vector<ll> c(n);
    FenwickTree<ll> fn(c);
    for (int i = 0; i < n; i++) {
    	ll curx = v[i];
    	ll cnt = curx + 1 <= n - 1 ? fn.range_sum(curx + 1, n - 1) : 0;
    	inversions += cnt;
    	fn.point_update(curx, 1LL);
    }
    return inversions;
}

/*signed main(){
    fastio
    assert(count_swaps({-2, 2, 2, -2, -2, 2}) == 1);
    assert(count_swaps({2, 1, -1, -2}) == 4);
    return 0; 
}*/

Compilation message

shoes.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    4 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 7 ms 1008 KB Output is correct
21 Correct 7 ms 972 KB Output is correct
22 Correct 99 ms 8132 KB Output is correct
23 Correct 99 ms 8168 KB Output is correct
24 Correct 115 ms 8240 KB Output is correct
25 Correct 107 ms 8240 KB Output is correct
26 Correct 103 ms 9412 KB Output is correct
27 Correct 113 ms 9120 KB Output is correct
28 Correct 97 ms 9420 KB Output is correct
29 Correct 106 ms 9428 KB Output is correct
30 Correct 111 ms 9384 KB Output is correct
31 Correct 104 ms 9440 KB Output is correct
32 Correct 96 ms 9412 KB Output is correct
33 Correct 98 ms 9152 KB Output is correct
34 Correct 98 ms 9440 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 92 ms 9336 KB Output is correct
38 Correct 96 ms 9488 KB Output is correct
39 Correct 104 ms 9412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 838 ms 151300 KB Output is correct
6 Correct 239 ms 21112 KB Output is correct
7 Correct 840 ms 152580 KB Output is correct
8 Correct 99 ms 9412 KB Output is correct
9 Correct 792 ms 152608 KB Output is correct
10 Correct 543 ms 80680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 2 ms 588 KB Output is correct
41 Correct 4 ms 1740 KB Output is correct
42 Correct 3 ms 1740 KB Output is correct
43 Correct 3 ms 1612 KB Output is correct
44 Correct 3 ms 1356 KB Output is correct
45 Correct 2 ms 1228 KB Output is correct
46 Correct 2 ms 1140 KB Output is correct
47 Correct 2 ms 972 KB Output is correct
48 Correct 2 ms 716 KB Output is correct
49 Correct 1 ms 588 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 3 ms 1740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Correct 7 ms 1008 KB Output is correct
55 Correct 7 ms 972 KB Output is correct
56 Correct 99 ms 8132 KB Output is correct
57 Correct 99 ms 8168 KB Output is correct
58 Correct 115 ms 8240 KB Output is correct
59 Correct 107 ms 8240 KB Output is correct
60 Correct 103 ms 9412 KB Output is correct
61 Correct 113 ms 9120 KB Output is correct
62 Correct 97 ms 9420 KB Output is correct
63 Correct 106 ms 9428 KB Output is correct
64 Correct 111 ms 9384 KB Output is correct
65 Correct 104 ms 9440 KB Output is correct
66 Correct 96 ms 9412 KB Output is correct
67 Correct 98 ms 9152 KB Output is correct
68 Correct 98 ms 9440 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 92 ms 9336 KB Output is correct
72 Correct 96 ms 9488 KB Output is correct
73 Correct 104 ms 9412 KB Output is correct
74 Correct 1 ms 204 KB Output is correct
75 Correct 1 ms 268 KB Output is correct
76 Correct 1 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 838 ms 151300 KB Output is correct
79 Correct 239 ms 21112 KB Output is correct
80 Correct 840 ms 152580 KB Output is correct
81 Correct 99 ms 9412 KB Output is correct
82 Correct 792 ms 152608 KB Output is correct
83 Correct 543 ms 80680 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 1 ms 332 KB Output is correct
86 Correct 1 ms 332 KB Output is correct
87 Correct 2 ms 588 KB Output is correct
88 Correct 4 ms 1740 KB Output is correct
89 Correct 3 ms 1740 KB Output is correct
90 Correct 3 ms 1612 KB Output is correct
91 Correct 3 ms 1356 KB Output is correct
92 Correct 2 ms 1228 KB Output is correct
93 Correct 2 ms 1140 KB Output is correct
94 Correct 2 ms 972 KB Output is correct
95 Correct 2 ms 716 KB Output is correct
96 Correct 1 ms 588 KB Output is correct
97 Correct 1 ms 332 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 3 ms 1740 KB Output is correct
100 Correct 3 ms 1740 KB Output is correct
101 Correct 55 ms 14020 KB Output is correct
102 Correct 17 ms 3888 KB Output is correct
103 Correct 979 ms 152604 KB Output is correct
104 Correct 780 ms 152648 KB Output is correct
105 Correct 705 ms 90392 KB Output is correct
106 Correct 811 ms 152520 KB Output is correct
107 Correct 297 ms 24644 KB Output is correct
108 Correct 437 ms 40596 KB Output is correct
109 Correct 107 ms 9436 KB Output is correct
110 Correct 111 ms 9384 KB Output is correct
111 Correct 776 ms 136516 KB Output is correct
112 Correct 806 ms 120648 KB Output is correct
113 Correct 301 ms 26276 KB Output is correct
114 Correct 773 ms 152648 KB Output is correct
115 Correct 790 ms 152580 KB Output is correct