Submission #430412

#TimeUsernameProblemLanguageResultExecution timeMemory
430412vulpes2Bubble Sort 2 (JOI18_bubblesort2)C++17
0 / 100
42 ms1452 KiB
//#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); 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]]; } LazySegmentTree<int,int> segtree(initial, [](int x, int y){ return max(x,y); }, -modd, [](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); } 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); if (counttree[V[j]]==1) { int p = segtree[V[j]]; segtree.SetVal(V[j], V[j]+1, -p+X[j]-counttree.GetEvaluation(0, V[j])); } A[X[j]] = V[j]; answer[j] = segtree.GetEvaluation(0, initial.size()); } return answer; }

Compilation message (stderr)

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:223:5: note: in expansion of macro 'forr'
  223 |     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:230: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:231: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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...