Submission #430496

# Submission time Handle Problem Language Result Execution time Memory
430496 2021-06-16T14:41:31 Z vulpes2 Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
8764 ms 199068 KB
//#include <atcoder/maxflow.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
 

#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <bitset>
#include <fstream>
 
 
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

 
//using namespace __gnu_pbds;
//using namespace atcoder;
using namespace std;

const ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//const ll modd = 998244353;

template<class T>
class SegmentTree2 {
    public:
     
      SegmentTree2(const vector<T>& data, function<T(T,T)> f_, T zero_value = 0) : zv(zero_value), f(f_) {   
          Initialize(data);
      }

      SegmentTree2(int n, function<T(T,T)> f_, T zero_value = 0) : zv(zero_value), f(f_) {
          vector<T> temp(n, zv);
          Initialize(temp);
      }

      T operator[](int i) {
          return tree[B+i];
      }

      T GetEvaluation(int L, int R) {   // "min/max" on interval [L,R); 0-based indexing, as usual
        if (R<=L) {  return zv;  }
        return GetEvaluationHelper(L, R, 0, B, 1);
      }


      void SetVal(int i, T val) {
			tree[B+i] = val;
			for(int j = (B + i) / 2; j >= 1; j /= 2) {
				tree[j] = f(tree[2*j], tree[2*j+1]);
			}
      }

      private:
        vector<T> tree;
        int B;  // power of two greater than size of input data
        T zv;
        function<T(T,T)> f;

        void Initialize(const vector<T>& data) {
    	  B = 1;
          while(B < data.size()) {B *= 2;  }
          tree = std::move(vector<T>(2*B, zv));
          copy(data.begin(), data.end(), next(tree.begin(), B));
          for(int i = B - 1; i >= 1; --i) {
  	  	     tree[i] = f(tree[2*i], tree[2*i+1]);
          }
        }

      T GetEvaluationHelper(int L, int R, int start, int length, int tree_index) {
          if (L==R) {  return zv; }
          if ((L==start) && (R==start+length)) {   return tree[tree_index];  }
          int midpoint = start + length/2;

          T left_ = zv, right_ = zv;
          if (L<=min(midpoint,R)) {
            left_ = GetEvaluationHelper(L, min(midpoint,R), start, length/2, 2*tree_index);
          }
          if (max(midpoint,L)<=R) {
            right_ = GetEvaluationHelper(max(midpoint,L), R, midpoint, length/2, 2*tree_index+1);
          }
          return f(left_, right_);
      }

};


template<class T, class S>
class LazySegmentTree {
    // Lazy propagation segment tree, containing elts of type T; S is type ("paramter space type") for all possible function we'll be
    // applying to elts (thee funcs should commute with f; f should be associative)
    public:
      typedef function<T(T)> application_type;
     
      // f is combining function, should be associative; zero_value is identity for f as a grupoid operation;
      // com is composition of application functions, specifically com(a1, a2) is a1 \circ a2 ie com(a1,a2)(t)=a1(a2(t))
      // param is parametrization of application functions
      // id_ is parameter for identity application function
      // typical example if you have min segtree, and you want to assign values in a lazy way to interval:
      // T=int,S=int, f(x,y) = min(x,y), zero_value = +infty"=modd"; com(a,b)=a; 
      // param(a) = [](int a){ return function<int(int)>([](int x){return a;}); }
      // CAREFUL!!!! in param make sure if you capture anything in return function, you do it by value, not by reference!!!!!!!!!!!!!!!!!!!
      // id_ = say modd+1; and param(modd+1) should be identity, and comm should accomodate that
      
      LazySegmentTree(const vector<T>& data, function<T(T,T)> f_, T zero_value,
        function<S(S,S)> com, function<application_type(S)> param, S id_) : zv(zero_value), f(f_), composition(com),
            paramtrization(param), ident(id_) {   
          Initialize(data);
      }

      T operator[](int i) {
          return GetEvaluation(i, i+1);
      }

      T GetEvaluation(int L, int R) {   // "min/max" on interval [L,R); 0-based indexing, as usual
        if (R<=L) {  return zv;  }
        return GetEvaluationHelper(L, R, 0, B, 1, ident);
      }

      void SetVal(int L, int R, S g) {  // set on interval [L,R); 0-based indexing
          if (R<=L) {  return;  }
          SetValHelper(L, R, 0, B, 1, g);
      }

      private:
        vector<T> tree;
        vector<S> application_function_below;
        int B;  // power of two greater than size of input data
        T zv;
        function<T(T,T)> f;
        function<S(S,S)> composition;
        function<application_type(S)> paramtrization;
        S ident;

        void Initialize(const vector<T>& data) {
    	  B = 1;
          while(B < data.size()) {B *= 2;  }
          tree = vector<T>(2*B, zv);
          copy(data.begin(), data.end(), next(tree.begin(), B));
          for(int i = B - 1; i >= 1; --i) {
  	  	     tree[i] = f(tree[2*i], tree[2*i+1]);
          }
          application_function_below = vector<S>(2*B, ident);
        }

      T GetEvaluationHelper(int L, int R, int start, int length, int tree_index, S accumulate) {
          if (L==R) {  return zv; }
          if ((L==start) && (R==start+length)) {   return paramtrization(accumulate)(tree[tree_index]);  }
          int midpoint = start + length/2;
          T left_ = zv, right_ = zv;

          if (L<=min(midpoint,R)) {
            left_ = GetEvaluationHelper(L, min(midpoint,R), start, length/2, 2*tree_index, composition(accumulate, application_function_below[tree_index]));
          }
          if (max(midpoint,L)<=R) {
            right_ = GetEvaluationHelper(max(midpoint,L), R, midpoint, length/2, 2*tree_index+1, composition(accumulate, application_function_below[tree_index]));
          }
          return f(left_, right_);
      }

      void SetValHelper(int L, int R, int start, int length, int tree_index, S g) {
          if (L==R) {  return; }
          if ((L==start) && (R==start+length)) {
              tree[tree_index] = paramtrization(g)(tree[tree_index]);
              application_function_below[tree_index] = composition(g, application_function_below[tree_index]);
              return;  }
          int midpoint = start + length/2;
          application_function_below[2*tree_index] = composition(application_function_below[tree_index], application_function_below[2*tree_index]);
          application_function_below[2*tree_index+1] = composition(application_function_below[tree_index], application_function_below[2*tree_index+1]);
          tree[2*tree_index] = paramtrization(application_function_below[tree_index])(tree[2*tree_index]);
          tree[2*tree_index+1] = paramtrization(application_function_below[tree_index])(tree[2*tree_index+1]);
          application_function_below[tree_index] = ident;

          if (L<=min(midpoint,R)) {
            SetValHelper(L, min(midpoint,R), start, length/2, 2*tree_index, g);
          }
          if (max(midpoint,L)<=R) {
            SetValHelper(max(midpoint,L), R, midpoint, length/2, 2*tree_index+1, g);
          }
          tree[tree_index] = f(tree[2*tree_index], tree[2*tree_index+1]);
      }

};


std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
    map<int,int> dict;
    for(auto x : A) {  dict[x] = 0;   }
    for(auto x : V) {  dict[x] = 0;   }
    int i = 0;
    for(auto it = dict.begin(); it != dict.end(); ++it) {
        it->second = i; ++i;
    }
    for(auto& x : A) { x = dict[x]; }
    for(auto& x : V) { x = dict[x]; }

    vector<ll> initial(dict.size(), -modd);
    vector<int> a_copy(A);
    sort(a_copy.begin(), a_copy.end());
    vector<int> count_(dict.size(), 0);
    vector<set<int>> rightmost(dict.size());
    forr(i,0,A.size()) {
        int k = upper_bound(a_copy.begin(), a_copy.end(), A[i]) - a_copy.begin();
        --k;
        initial[A[i]] = i-k;
        ++count_[A[i]];
        rightmost[A[i]].insert(i);
    }
    LazySegmentTree<ll,ll> segtree(initial, [](ll x, ll y){ return max(x,y); }, -modd*modd,
      [](ll a, ll b){ return a+b; }, [](ll a){ return [a](int x){ return x+a; }; }, 0);
    SegmentTree2<int> counttree(count_, [](int x, int y){ return x+y; }, 0);

	int Q=X.size();
	std::vector<int> answer(Q);
	for (int j=0;j<Q;j++) {
        if (A[X[j]]==V[j]) {
            answer[j] = segtree.GetEvaluation(0, initial.size());
            continue;
        }
        if (V[j]<A[X[j]]) {
            segtree.SetVal(V[j], A[X[j]], -1);
        } else {
            segtree.SetVal(A[X[j]], V[j], +1);
        }

        rightmost[A[X[j]]].erase(X[j]);
        rightmost[V[j]].insert(X[j]);

        counttree.SetVal(A[X[j]], counttree[A[X[j]]] - 1);
        counttree.SetVal(V[j], counttree[V[j]]+1);

        if (!rightmost[A[X[j]]].empty()) {
          int p = segtree[A[X[j]]];
          int new_val = *prev(rightmost[A[X[j]]].end())-(counttree.GetEvaluation(0, A[X[j]]+1)-1);
          segtree.SetVal(A[X[j]], A[X[j]]+1, -p+ new_val);
        } else {
          segtree.SetVal(A[X[j]], A[X[j]]+1, -modd);
        }

        int p = segtree[V[j]];
        int new_val = *prev(rightmost[V[j]].end())-(counttree.GetEvaluation(0, V[j]+1)-1);
        segtree.SetVal(V[j], V[j]+1, -p+ new_val);
        A[X[j]] = V[j];

		answer[j] = segtree.GetEvaluation(0, initial.size());
	}
	return answer;
}

Compilation message

bubblesort2.cpp: In function 'std::vector<int> countScans(std::vector<int>, std::vector<int>, std::vector<int>)':
bubblesort2.cpp:29:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 | #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
      |                                                     ^
bubblesort2.cpp:224:5: note: in expansion of macro 'forr'
  224 |     forr(i,0,A.size()) {
      |     ^~~~
bubblesort2.cpp: In instantiation of 'void LazySegmentTree<T, S>::Initialize(const std::vector<_Tp>&) [with T = long long int; S = long long int]':
bubblesort2.cpp:130:11:   required from 'LazySegmentTree<T, S>::LazySegmentTree(const std::vector<_Tp>&, std::function<T(T, T)>, T, std::function<S(S, S)>, std::function<std::function<T(T)>(S)>, S) [with T = long long int; S = long long int]'
bubblesort2.cpp:232:86:   required from here
bubblesort2.cpp:159:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |           while(B < data.size()) {B *= 2;  }
      |                 ~~^~~~~~~~~~~~~
bubblesort2.cpp: In instantiation of 'void SegmentTree2<T>::Initialize(const std::vector<_Tp>&) [with T = int]':
bubblesort2.cpp:51:11:   required from 'SegmentTree2<T>::SegmentTree2(const std::vector<_Tp>&, std::function<T(T, T)>, T) [with T = int]'
bubblesort2.cpp:233:75:   required from here
bubblesort2.cpp:84:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |           while(B < data.size()) {B *= 2;  }
      |                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 516 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 12 ms 972 KB Output is correct
4 Correct 11 ms 972 KB Output is correct
5 Correct 10 ms 972 KB Output is correct
6 Correct 10 ms 972 KB Output is correct
7 Correct 13 ms 972 KB Output is correct
8 Correct 13 ms 972 KB Output is correct
9 Correct 11 ms 972 KB Output is correct
10 Correct 13 ms 972 KB Output is correct
11 Correct 12 ms 972 KB Output is correct
12 Correct 14 ms 972 KB Output is correct
13 Correct 13 ms 972 KB Output is correct
14 Correct 11 ms 972 KB Output is correct
15 Correct 11 ms 972 KB Output is correct
16 Correct 10 ms 972 KB Output is correct
17 Correct 12 ms 972 KB Output is correct
18 Correct 10 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 516 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 12 ms 972 KB Output is correct
4 Correct 11 ms 972 KB Output is correct
5 Correct 10 ms 972 KB Output is correct
6 Correct 10 ms 972 KB Output is correct
7 Correct 13 ms 972 KB Output is correct
8 Correct 13 ms 972 KB Output is correct
9 Correct 11 ms 972 KB Output is correct
10 Correct 13 ms 972 KB Output is correct
11 Correct 12 ms 972 KB Output is correct
12 Correct 14 ms 972 KB Output is correct
13 Correct 13 ms 972 KB Output is correct
14 Correct 11 ms 972 KB Output is correct
15 Correct 11 ms 972 KB Output is correct
16 Correct 10 ms 972 KB Output is correct
17 Correct 12 ms 972 KB Output is correct
18 Correct 10 ms 972 KB Output is correct
19 Correct 42 ms 3148 KB Output is correct
20 Correct 50 ms 3460 KB Output is correct
21 Correct 59 ms 3384 KB Output is correct
22 Correct 56 ms 3404 KB Output is correct
23 Correct 56 ms 3148 KB Output is correct
24 Correct 55 ms 3208 KB Output is correct
25 Correct 47 ms 3092 KB Output is correct
26 Correct 56 ms 3096 KB Output is correct
27 Correct 47 ms 2884 KB Output is correct
28 Correct 46 ms 2980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 1956 KB Output is correct
2 Correct 109 ms 3112 KB Output is correct
3 Correct 197 ms 4232 KB Output is correct
4 Correct 179 ms 4220 KB Output is correct
5 Correct 190 ms 4212 KB Output is correct
6 Correct 180 ms 4288 KB Output is correct
7 Correct 162 ms 4228 KB Output is correct
8 Correct 168 ms 4212 KB Output is correct
9 Correct 172 ms 4208 KB Output is correct
10 Correct 93 ms 4200 KB Output is correct
11 Correct 102 ms 4804 KB Output is correct
12 Correct 101 ms 4796 KB Output is correct
13 Correct 138 ms 4776 KB Output is correct
14 Correct 117 ms 4828 KB Output is correct
15 Correct 116 ms 4860 KB Output is correct
16 Correct 143 ms 4852 KB Output is correct
17 Correct 144 ms 4836 KB Output is correct
18 Correct 147 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 516 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 12 ms 972 KB Output is correct
4 Correct 11 ms 972 KB Output is correct
5 Correct 10 ms 972 KB Output is correct
6 Correct 10 ms 972 KB Output is correct
7 Correct 13 ms 972 KB Output is correct
8 Correct 13 ms 972 KB Output is correct
9 Correct 11 ms 972 KB Output is correct
10 Correct 13 ms 972 KB Output is correct
11 Correct 12 ms 972 KB Output is correct
12 Correct 14 ms 972 KB Output is correct
13 Correct 13 ms 972 KB Output is correct
14 Correct 11 ms 972 KB Output is correct
15 Correct 11 ms 972 KB Output is correct
16 Correct 10 ms 972 KB Output is correct
17 Correct 12 ms 972 KB Output is correct
18 Correct 10 ms 972 KB Output is correct
19 Correct 42 ms 3148 KB Output is correct
20 Correct 50 ms 3460 KB Output is correct
21 Correct 59 ms 3384 KB Output is correct
22 Correct 56 ms 3404 KB Output is correct
23 Correct 56 ms 3148 KB Output is correct
24 Correct 55 ms 3208 KB Output is correct
25 Correct 47 ms 3092 KB Output is correct
26 Correct 56 ms 3096 KB Output is correct
27 Correct 47 ms 2884 KB Output is correct
28 Correct 46 ms 2980 KB Output is correct
29 Correct 22 ms 1956 KB Output is correct
30 Correct 109 ms 3112 KB Output is correct
31 Correct 197 ms 4232 KB Output is correct
32 Correct 179 ms 4220 KB Output is correct
33 Correct 190 ms 4212 KB Output is correct
34 Correct 180 ms 4288 KB Output is correct
35 Correct 162 ms 4228 KB Output is correct
36 Correct 168 ms 4212 KB Output is correct
37 Correct 172 ms 4208 KB Output is correct
38 Correct 93 ms 4200 KB Output is correct
39 Correct 102 ms 4804 KB Output is correct
40 Correct 101 ms 4796 KB Output is correct
41 Correct 138 ms 4776 KB Output is correct
42 Correct 117 ms 4828 KB Output is correct
43 Correct 116 ms 4860 KB Output is correct
44 Correct 143 ms 4852 KB Output is correct
45 Correct 144 ms 4836 KB Output is correct
46 Correct 147 ms 4856 KB Output is correct
47 Correct 1843 ms 67976 KB Output is correct
48 Correct 7817 ms 184204 KB Output is correct
49 Correct 8204 ms 198956 KB Output is correct
50 Correct 8409 ms 198972 KB Output is correct
51 Correct 8270 ms 198972 KB Output is correct
52 Correct 8764 ms 198960 KB Output is correct
53 Correct 8655 ms 198968 KB Output is correct
54 Correct 8397 ms 199064 KB Output is correct
55 Correct 8441 ms 198968 KB Output is correct
56 Correct 7939 ms 199068 KB Output is correct
57 Correct 8573 ms 199052 KB Output is correct
58 Correct 8167 ms 199052 KB Output is correct
59 Correct 7310 ms 184652 KB Output is correct
60 Correct 7335 ms 184656 KB Output is correct
61 Correct 6597 ms 184596 KB Output is correct
62 Correct 6523 ms 177864 KB Output is correct
63 Correct 6496 ms 177896 KB Output is correct
64 Correct 6429 ms 177936 KB Output is correct
65 Correct 6092 ms 171152 KB Output is correct
66 Correct 5869 ms 171148 KB Output is correct
67 Correct 5948 ms 171176 KB Output is correct