Untitled diff

Creato Il diff non scade mai
1 rimozione
371 linee
1 aggiunta
371 linee
/*
/*
Compete against Yourself.
Compete against Yourself.
Author - Aryan (@aryanc403)
Author - Aryan (@aryanc403)
*/
*/
/*
/*
Credits -
Credits -
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/ (namespace atcoder)
Atcoder library - https://atcoder.github.io/ac-library/production/document_en/ (namespace atcoder)
Github source code of library - https://github.com/atcoder/ac-library/tree/master/atcoder
Github source code of library - https://github.com/atcoder/ac-library/tree/master/atcoder
https://codeforces.com/contest/4/submission/150120627
https://codeforces.com/contest/4/submission/150120627
*/
*/


#include <algorithm>
#include <algorithm>
#include <cassert>
#include <cassert>
#include <functional>
#include <functional>
#include <vector>
#include <vector>




#ifdef _MSC_VER
#ifdef _MSC_VER
#include <intrin.h>
#include <intrin.h>
#endif
#endif


#if __cplusplus >= 202002L
#if __cplusplus >= 202002L
#include <bit>
#include <bit>
#endif
#endif


namespace atcoder {
namespace atcoder {


namespace internal {
namespace internal {


#if __cplusplus >= 202002L
#if __cplusplus >= 202002L


using std::bit_ceil;
using std::bit_ceil;


#else
#else


unsigned int bit_ceil(unsigned int n) {
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
while (x < (unsigned int)(n)) x *= 2;
return x;
return x;
}
}


#endif
#endif


int countr_zero(unsigned int n) {
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
#ifdef _MSC_VER
unsigned long index;
unsigned long index;
_BitScanForward(&index, n);
_BitScanForward(&index, n);
return index;
return index;
#else
#else
return __builtin_ctz(n);
return __builtin_ctz(n);
#endif
#endif
}
}


constexpr int countr_zero_constexpr(unsigned int n) {
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
int x = 0;
while (!(n & (1 << x))) x++;
while (!(n & (1 << x))) x++;
return x;
return x;
}
}


} // namespace internal
} // namespace internal


} // namespace atcoder
} // namespace atcoder




namespace atcoder {
namespace atcoder {


#if __cplusplus >= 201703L
#if __cplusplus >= 201703L


template <class S, auto op, auto e> struct segtree {
template <class S, auto op, auto e> struct segtree {
static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
static_assert(std::is_convertible_v<decltype(op), std::function<S(S, S)>>,
"op must work as S(S, S)");
"op must work as S(S, S)");
static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
static_assert(std::is_convertible_v<decltype(e), std::function<S()>>,
"e must work as S()");
"e must work as S()");


#else
#else


template <class S, S (*op)(S, S), S (*e)()> struct segtree {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {


#endif
#endif


public:
public:
segtree() : segtree(0) {}
segtree() : segtree(0) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(int n) : segtree(std::vector<S>(n, e())) {}
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
explicit segtree(const std::vector<S>& v) : _n(int(v.size())) {
size = (int)internal::bit_ceil((unsigned int)(_n));
size = (int)internal::bit_ceil((unsigned int)(_n));
log = internal::countr_zero((unsigned int)size);
log = internal::countr_zero((unsigned int)size);
d = std::vector<S>(2 * size, e());
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
for (int i = size - 1; i >= 1; i--) {
update(i);
update(i);
}
}
}
}


void set(int p, S x) {
void set(int p, S x) {
assert(0 <= p && p < _n);
assert(0 <= p && p < _n);
p += size;
p += size;
d[p] = x;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
for (int i = 1; i <= log; i++) update(p >> i);
}
}