Submission #430469

# Submission time Handle Problem Language Result Execution time Memory
430469 2021-06-16T14:15:07 Z vulpes2 Bubble Sort 2 (JOI18_bubblesort2) C++17
38 / 100
152 ms 4700 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<int> 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<int,int> segtree(initial, [](int x, int y){ return max(x,y); }, -modd-modd/10,
      [](int a, int b){ return a+b; }, [](int 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);
        if (counttree[A[X[j]]]==0) {
            segtree.SetVal(A[X[j]], A[X[j]]+1, -modd/10);
        }
        counttree.SetVal(V[j], counttree[V[j]]+1);
        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;
}


std::vector<int> countScanscorrect(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]; }
 
	int Q=X.size();
	std::vector<int> answer(Q);
	for (int j=0;j<Q;j++) {
        A[X[j]] = V[j];
        int ans = 0;
        vector<int> a_copy(A.begin(), A.end());
        sort(a_copy.begin(), a_copy.end());
        forr(i,0,A.size()) {
            int k = upper_bound(a_copy.begin(), a_copy.end(), A[i]) - a_copy.begin();
            --k;
            ans = max(ans, i-k);
        }
		answer[j]=ans;
	}
	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 function 'std::vector<int> countScanscorrect(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:285:9: note: in expansion of macro 'forr'
  285 |         forr(i,0,A.size()) {
      |         ^~~~
bubblesort2.cpp: In instantiation of 'void LazySegmentTree<T, S>::Initialize(const std::vector<_Tp>&) [with T = int; S = 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 = int; S = int]'
bubblesort2.cpp:232:89:   required from here
bubblesort2.cpp:159:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 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 940 KB Output is correct
7 Correct 10 ms 972 KB Output is correct
8 Correct 10 ms 972 KB Output is correct
9 Correct 10 ms 972 KB Output is correct
10 Correct 10 ms 844 KB Output is correct
11 Correct 10 ms 844 KB Output is correct
12 Correct 11 ms 844 KB Output is correct
13 Correct 10 ms 844 KB Output is correct
14 Correct 10 ms 844 KB Output is correct
15 Correct 10 ms 844 KB Output is correct
16 Correct 12 ms 844 KB Output is correct
17 Correct 11 ms 844 KB Output is correct
18 Correct 10 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 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 940 KB Output is correct
7 Correct 10 ms 972 KB Output is correct
8 Correct 10 ms 972 KB Output is correct
9 Correct 10 ms 972 KB Output is correct
10 Correct 10 ms 844 KB Output is correct
11 Correct 10 ms 844 KB Output is correct
12 Correct 11 ms 844 KB Output is correct
13 Correct 10 ms 844 KB Output is correct
14 Correct 10 ms 844 KB Output is correct
15 Correct 10 ms 844 KB Output is correct
16 Correct 12 ms 844 KB Output is correct
17 Correct 11 ms 844 KB Output is correct
18 Correct 10 ms 844 KB Output is correct
19 Correct 42 ms 2852 KB Output is correct
20 Correct 47 ms 3020 KB Output is correct
21 Correct 48 ms 3228 KB Output is correct
22 Correct 46 ms 3088 KB Output is correct
23 Correct 49 ms 2900 KB Output is correct
24 Correct 46 ms 2872 KB Output is correct
25 Correct 47 ms 2796 KB Output is correct
26 Correct 44 ms 2784 KB Output is correct
27 Correct 45 ms 2636 KB Output is correct
28 Correct 43 ms 2612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 1868 KB Output is correct
2 Correct 78 ms 3044 KB Output is correct
3 Incorrect 152 ms 4700 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 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 940 KB Output is correct
7 Correct 10 ms 972 KB Output is correct
8 Correct 10 ms 972 KB Output is correct
9 Correct 10 ms 972 KB Output is correct
10 Correct 10 ms 844 KB Output is correct
11 Correct 10 ms 844 KB Output is correct
12 Correct 11 ms 844 KB Output is correct
13 Correct 10 ms 844 KB Output is correct
14 Correct 10 ms 844 KB Output is correct
15 Correct 10 ms 844 KB Output is correct
16 Correct 12 ms 844 KB Output is correct
17 Correct 11 ms 844 KB Output is correct
18 Correct 10 ms 844 KB Output is correct
19 Correct 42 ms 2852 KB Output is correct
20 Correct 47 ms 3020 KB Output is correct
21 Correct 48 ms 3228 KB Output is correct
22 Correct 46 ms 3088 KB Output is correct
23 Correct 49 ms 2900 KB Output is correct
24 Correct 46 ms 2872 KB Output is correct
25 Correct 47 ms 2796 KB Output is correct
26 Correct 44 ms 2784 KB Output is correct
27 Correct 45 ms 2636 KB Output is correct
28 Correct 43 ms 2612 KB Output is correct
29 Correct 21 ms 1868 KB Output is correct
30 Correct 78 ms 3044 KB Output is correct
31 Incorrect 152 ms 4700 KB Output isn't correct
32 Halted 0 ms 0 KB -