Submission #166753

# Submission time Handle Problem Language Result Execution time Memory
166753 2019-12-03T16:39:02 Z wleung_bvg Street Lamps (APIO19_street_lamps) C++14
100 / 100
3084 ms 435980 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;using namespace __gnu_pbds;
template<class C>constexpr int sz(const C&c){return int(c.size());}
using ll=long long;using ld=long double;using uint=unsigned int;using ull=unsigned long long;
using pii=std::pair<int,int>;using pll=std::pair<ll,ll>;using pill=std::pair<int,ll>;using plli=std::pair<ll,int>;using pdd=std::pair<double,double>;using pld=std::pair<ld,ld>;
#if __SIZEOF_INT128__
    using i128=__int128_t;using ui128=__uint128_t;using pi128=std::pair<i128,i128>;
#endif
constexpr const char nl='\n',sp=' ';constexpr const int INT_INF=0x3f3f3f3f;constexpr const ll LL_INF=0x3f3f3f3f3f3f3f3f;
constexpr const double D_INF=std::numeric_limits<double>::infinity();constexpr const ld LD_INF=std::numeric_limits<ld>::infinity();
template<class T>constexpr const T&_min(const T&x,const T&y){return x<y?x:y;}template<class T>constexpr const T&_max(const T&x,const T&y){return x<y?y:x;}
template<class T,class...Ts>constexpr const T&_min(const T&x,const Ts&...xs){return _min(x,_min(xs...));}
template<class T,class...Ts>constexpr const T&_max(const T&x,const Ts&...xs){return _max(x,_max(xs...));}
template<class T,class...Ts>void MIN(T&x,const Ts&...xs){x=_min(x,xs...);}template<class T,class...Ts>void MAX(T&x,const Ts&...xs){x=_max(x,xs...);}
template<class T,class...Args>std::unique_ptr<T>_make_unique(Args&&...args){return std::unique_ptr<T>(new T(std::forward<Args>(args)...));}
template<class T,class...Args>std::shared_ptr<T>_make_shared(Args&&...args){return std::shared_ptr<T>(new T(std::forward<Args>(args)...));}
#define min(...) _min(__VA_ARGS__)
#define max(...) _max(__VA_ARGS__)
#define make_unique _make_unique
#define make_shared _make_shared
std::seed_seq seq{
    (uint64_t)std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count(),
    (uint64_t)__builtin_ia32_rdtsc(),(uint64_t)(uintptr_t)make_unique<char>().get()
};
std::mt19937 rng(seq);std::mt19937_64 rng64(seq);const std::size_t RANDOM=std::uniform_int_distribution<std::size_t>(0,(std::numeric_limits<std::size_t>::max)())(rng64);
template<class T,class H=std::hash<T>>struct rand_hash{
    static uint64_t splitmix64(uint64_t x){x+=0x9e3779b97f4a7c15;x=(x^(x>>30))*0xbf58476d1ce4e5b9;x=(x^(x>>27))*0x94d049bb133111eb;return x^(x>>31);}
    std::size_t operator()(const T&x)const{return splitmix64(H{}(x)+RANDOM);}
};
template<class K,class H=rand_hash<K>,class...Ts>using hashset=__gnu_pbds::gp_hash_table<K,__gnu_pbds::null_type,H,Ts...>;
template<class K,class V,class H=rand_hash<K>,class...Ts>using hashmap=__gnu_pbds::gp_hash_table<K,V,H,Ts...>;
template<class K,class C=std::less<K>,class...Ts>using treeset=__gnu_pbds::tree<K,__gnu_pbds::null_type,C,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update,Ts...>;
template<class K,class V,class C=std::less<K>,class...Ts>using treemap=__gnu_pbds::tree<K,V,C,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update,Ts...>;
template<class K,class H=rand_hash<K>,class...Ts>using uset=std::unordered_set<K,H,Ts...>;
template<class K,class V,class H=rand_hash<K>,class...Ts>using umap=std::unordered_map<K,V,H,Ts...>;
template<class T>using minpq=std::priority_queue<T,std::vector<T>,std::greater<T>>;template<class T>using maxpq=std::priority_queue<T,std::vector<T>,std::less<T>>;
template<class T>using minpairingheap=__gnu_pbds::priority_queue<T,std::greater<T>,__gnu_pbds::pairing_heap_tag>;
template<class T>using maxpairingheap=__gnu_pbds::priority_queue<T,std::less<T>,__gnu_pbds::pairing_heap_tag>;
template<class T1,class T2,class H1=rand_hash<T1>,class H2=rand_hash<T2>>struct pair_hash{
    std::size_t operator()(const std::pair<T1,T2>&p)const{return 31*H1{}(p.first)+H2{}(p.second);}
};
template<class T>struct is_iterator{
    template<class U,typename std::enable_if<!std::is_convertible<U,const char*>::value,int>::type=0>constexpr static auto has_indirection(int)->decltype(*std::declval<U>(),bool()){return true;}
    template<class>constexpr static bool has_indirection(long){return false;}constexpr static bool value=has_indirection<T>(0);
};

#define INTERACTIVE_INPUT 0
constexpr const int _bufferSize=1<<16,_maxNumLength=128;
char _inputBuffer[_bufferSize+1],*_inputPtr=_inputBuffer,_outputBuffer[_bufferSize],_c,_last,_sign,*_tempInputBuf=nullptr,_numBuf[_maxNumLength],_tempOutputBuf[_maxNumLength],_fill=' ';
FILE*_input=stdin,*_output=stdout,*_error=stderr;const char*_delimiter=" ";int _cur,_outputPtr=0,_numPtr=0,_precision=9,_width=0,_tempOutputPtr=0,_cnt;ull _precisionBase=1000000000;
#ifdef _WIN32
    #define getchar_unlocked getchar
    #define fread_unlocked fread
    #define fwrite_unlocked fwrite
#endif
#if INTERACTIVE_INPUT
    char _getchar(){return _last=getchar_unlocked();}
    char _getcharskipr(){while(_getchar()=='\r');return _last;}
#else
    char _peekchar(){return*_inputPtr?*_inputPtr:(_inputBuffer[fread_unlocked(_inputPtr=_inputBuffer,1,_bufferSize,_input)]='\0',*_inputPtr);}
    char _peekcharskipr(){while(_peekchar()=='\r')_last=*_inputPtr++;return _peekchar();}
    char _getchar(){return _last=*_inputPtr?*_inputPtr++:(_inputBuffer[fread_unlocked(_inputPtr=_inputBuffer,1,_bufferSize,_input)]='\0',*_inputPtr++);}
    char _getcharskipr(){while(_getchar()=='\r');return _last;}
    bool hasNext(){return (_last&&_peekcharskipr())||!feof(_input);}bool hasNextToken(){while(hasNext()&&_peekchar()<=' ')_getchar();return hasNext();}
#endif
template<class I>void _readSigned(I&x){while((_c=_getchar())<=' ');_sign=_c=='-';if(_sign)_c=_getchar();if(_c>='0')for(x=_c-'0';(_c=_getchar())>='0';x=x*10+_c-'0');}
template<class UI>void _readUnsigned(UI&x){while((_c=_getchar())<=' ');for(x=_c-'0';(_c=_getchar())>='0';x=x*10+_c-'0');}
template<class F>void _readFloatingPoint(F&x){for(F _div=1.0;(_c=_getchar())>='0';x+=(_c-'0')/(_div*=10));}
void setLength(int x){if(_tempInputBuf)delete[](_tempInputBuf);_tempInputBuf=new char[x+1];}
template<class I>typename std::enable_if<std::is_integral<I>::value&&std::is_signed<I>::value>::type read(I&x){_readSigned(x);if(_sign)x=-x;}
template<class UI>typename std::enable_if<std::is_integral<UI>::value&&std::is_unsigned<UI>::value>::type read(UI&x){_readUnsignedSigned(x);}
#if __SIZEOF_INT128__
    void read(i128&x){_readSigned(x);if(_sign)x=-x;}void read(ui128&x){_readUnsigned(x);}
#endif
template<class F>typename std::enable_if<std::is_floating_point<F>::value>::type read(F&x){_readSigned(x);if(_c=='.')_readFloatingPoint(x);if(_sign)x=-x;}
void read(char&x){while((x=_getchar())<=' ');}void read(char*x){_cur=0;do{_c=_getchar();}while(_c<=' ');do{x[_cur++]=_c;}while((_c=_getchar())>' ');x[_cur]='\0';}
void readln(char*x){if(_last=='\r')_getcharskipr();for(_cur=0;(_c=_getcharskipr())!='\n'&&_c;x[_cur++]=_c);x[_cur]='\0';}
void read(std::string&x){if(!_tempInputBuf)assert(0);read(_tempInputBuf);x=std::string(_tempInputBuf,_cur);}
void readln(std::string &x){if(!_tempInputBuf)assert(0);readln(_tempInputBuf);x=std::string(_tempInputBuf,_cur);}
template<class T1,class T2>void read(std::pair<T1,T2>&x){read(x.first);read(x.second);}template<class T>void read(std::complex<T>&x){T _re,_im;read(_re);read(_im);x.real(_re);x.imag(_im);}
template<class T>typename std::enable_if<is_iterator<typename T::iterator>::value>::type read(T&x);template<class T,class...Ts>void read(T&x,Ts&&...xs);
template<class It>typename std::enable_if<is_iterator<It>::value>::type read(It st,It en){for(It _i=st;_i!=en;_i++)read(*_i);}
template<class It,class...Ts>typename std::enable_if<is_iterator<It>::value>::type read(It st,It en,Ts&&...xs){read(st,en);read(std::forward<Ts>(xs)...);}
template<class T>typename std::enable_if<is_iterator<typename T::iterator>::value>::type read(T&x){for(auto&&_i:x)read(_i);}
template<class T,class...Ts>void read(T&x,Ts&&...xs){read(x);read(std::forward<Ts>(xs)...);}
void setInput(FILE*file){*_inputPtr='\0';_input=file;}void setInput(const char*s){*_inputPtr='\0';_input=fopen(s,"r");}void setInput(const std::string&s){*_inputPtr='\0';_input=fopen(s.c_str(),"r");}
int _flushBuf(){fwrite_unlocked(_outputBuffer,1,_outputPtr,_output);return _outputPtr=0;}void flush(){_flushBuf();fflush(_output);}
int _putchar(char x){return _outputBuffer[_outputPtr==_bufferSize?_flushBuf():_outputPtr]=x,_outputPtr++;}
void _writeTempBuf(char x){_tempOutputBuf[_tempOutputPtr++]=x;}void _writeOutput(){for(int _i=0;_i<_tempOutputPtr;_putchar(_tempOutputBuf[_i++]));_tempOutputPtr=0;}
void _fillBuf(int x){for(int _i=0;_i<x;_i++)_putchar(_fill);}void _flushNumBuf(){for(;_numPtr;_writeTempBuf(_numBuf[--_numPtr]));}
void _flushTempBuf(){int _tempLen=_tempOutputPtr;_fillBuf(_width-_tempLen);_writeOutput();_fillBuf(-_width-_tempLen);}
void setPrecision(int x){_precision=x;_precisionBase=1;for(int _i=0;_i<x;_i++,_precisionBase*=10);}void setWidth(int x){_width=x;}void setFill(char x){_fill=x;}
void setDelimiter(const char*x){_delimiter=x;}void setDelimiter(const std::string&x){_delimiter=x.c_str();}
void writeDelimiter(){for(const char*_p=_delimiter;*_p;_putchar(*_p++));}
template<class T>void _writeNum(const T&x,int digits){_cnt=0;for(T _y=x;_y;_y/=10,_cnt++)_numBuf[_numPtr++]='0'+_y%10;for(;_cnt<digits;_cnt++)_numBuf[_numPtr++]='0';_flushNumBuf();}
template<class F>void _writeFloatingPoint(const F&x){ull _I=ull(x);ull _F=(x-_I)*_precisionBase+F(0.5);if(_F>=_precisionBase){_I++;_F=0;}_writeNum(_I,1);_writeTempBuf('.');_writeNum(_F,_precision);}
void write(const bool&x){if(x)_writeTempBuf('1');else _writeTempBuf('0');_flushTempBuf();}void write(const char&x){_writeTempBuf(x);_flushTempBuf();}
void write(const char*x){int _slen=strlen(x);_fillBuf(_width-_slen);for(const char*_p=x;*_p;_putchar(*_p++));_fillBuf(-_width-_slen);}
void write(const std::string&x){_fillBuf(_width-int(x.length()));for(int _i=0;_i<int(x.length());_putchar(x[_i++]));_fillBuf(-_width-int(x.length()));}
template<class I>typename std::enable_if<std::is_integral<I>::value&&std::is_signed<I>::value>::type write(const I&x){
    using UI=typename std::make_unsigned<I>::type;if(x<0){_writeTempBuf('-');_writeNum(UI(-x),1);}else{_writeNum(UI(x),1);}_flushTempBuf();
}
template<class UI>typename std::enable_if<std::is_integral<UI>::value&&std::is_unsigned<UI>::value>::type write(const UI&x){_writeNum(x,1);_flushTempBuf();}
template<class F>typename std::enable_if<std::is_floating_point<F>::value>::type write(const F&x){
    if(std::isnan(x)){write("NaN");}else if(std::isinf(x)){write("Inf");}else if(x<0){_writeTempBuf('-');_writeFloatingPoint(-x);}else{_writeFloatingPoint(x);}_flushTempBuf();
}
#if __SIZEOF_INT128__
    void write(const i128&x){if(x<0){_writeTempBuf('-');_writeNum(ui128(-x),1);}else{_writeNum(ui128(x),1);}_flushTempBuf();}void write(const ui128&x){_writeNum(x,1);_flushTempBuf();}
#endif
template<class T1,class T2>void write(const std::pair<T1,T2>&x){write(x.first);writeDelimiter();write(x.second);}
template<class T>void write(const std::complex<T>&x){write(x.real());writeDelimiter();write(x.imag());}
template<class T>typename std::enable_if<is_iterator<typename T::iterator>::value>::type write(const T&x);template<class T,class...Ts>void write(const T&x,Ts&&...xs);
template<class It>typename std::enable_if<is_iterator<It>::value>::type write(It st,It en){bool _first=1;for(It _i=st;_i!=en;_i++){if(_first)_first=0;else writeDelimiter();write(*_i);}}
template<class It,class...Ts>typename std::enable_if<is_iterator<It>::value>::type write(It st,It en,Ts&&...xs){write(st,en);writeDelimiter();write(std::forward<Ts>(xs)...);}
template<class T>typename std::enable_if<is_iterator<typename T::iterator>::value>::type write(const T&x){bool _first=1;for(auto&&_i:x){if(_first)_first=0;else writeDelimiter();write(_i);}}
template<class T,class...Ts>void write(const T&x,Ts&&...xs){write(x);writeDelimiter();write(std::forward<Ts>(xs)...);}
void writeln(){_putchar('\n');}template<class...Ts>void writeln(Ts&&...xs){write(std::forward<Ts>(xs)...);_putchar('\n');}
class IOManager{public:~IOManager(){flush();if(_tempInputBuf)delete[](_tempInputBuf);}};std::unique_ptr<IOManager>iomanager=make_unique<IOManager>();
void setOutput(FILE*file){flush();_output=file;}void setOutput(const char*s){flush();_output=fopen(s,"w");}void setOutput(const std::string&s){flush();_output=fopen(s.c_str(),"w");}
template<class...Ts>void debug(const Ts&...xs){FILE*_temp=_output;setOutput(_error);write(xs...);setOutput(_temp);}
template<class...Ts>void debugln(const Ts&...xs){FILE*_temp=_output;setOutput(_error);writeln(xs...);setOutput(_temp);}
void setError(FILE*file){flush();_error=file;}void setError(const char*s){flush();_error=fopen(s,"w");}void setError(const std::string&s){flush();_error=fopen(s.c_str(),"w");}

template <class T, const bool ONE_INDEXED, const int ...Args> struct FenwickTree {
    T val;
    void init() { val = 0; }
    void update(T v) { val += v; }
    T rsq() { return val; }
};

template <class T, const bool ONE_INDEXED, const int MAXN, const int ...Ns> struct FenwickTree <T, ONE_INDEXED, MAXN, Ns...> {
    FenwickTree<T, ONE_INDEXED, Ns...> BIT[MAXN];
    void init() { for (int i = 0; i < MAXN; i++) BIT[i].init(); }
    template <class ...Args> void update(int i, Args ...args) { for (i += !ONE_INDEXED; i < MAXN; i += i & -i) BIT[i].update(args...); }
    template <class ...Args> T rsq(int l, int r, Args ...args) {
        T ret = 0;
        for (r += !ONE_INDEXED; r > 0; r -= r & -r) ret += BIT[r].rsq(args...);
        for (l -= ONE_INDEXED; l > 0; l -= l & -l) ret -= BIT[l].rsq(args...);
        return ret;
    }
};

template <const int R, class Value, class CountType, class Comparator = less<Value>> struct RootBuffer {
    Comparator cmp; CountType tot; int n; double SCALE_FACTOR; vector<pair<Value, CountType>> A[R];
    function<bool(const pair<Value, CountType>&, const pair<Value, CountType>&)> pairCmp =
        [&] (const pair<Value, CountType> &a, const pair<Value, CountType> &b) { return cmp(a.first, b.first); };
    RootBuffer(const double SCALE_FACTOR = 1) : tot(0), n(0), SCALE_FACTOR(SCALE_FACTOR) {}
    template <class PairIt> RootBuffer(const PairIt st, const PairIt en, const double SCALE_FACTOR = 1) :
            SCALE_FACTOR(SCALE_FACTOR) {
        assert(is_sorted(st, en, pairCmp)); A[R - 1] = vector<pair<Value, CountType>>(st, en); resizeUnique(A[R - 1]);
        n = int(A[R - 1].size()); tot = 0; for (auto &&p : A[R - 1]) tot += p.second;
    }
    void resizeUnique(vector<pair<Value, CountType>> &v) {
        if (!v.empty()) {
            int j = 0;
            for (int i = 1; i < int(v.size()); i++) {
                if (cmp(v[i].first, v[j].first) || cmp(v[j].first, v[i].first)) {
                    v[++j] = v[i]; v[j].second += v[j - 1].second;
                } else v[j].second += v[i].second;
            }
            v.resize(j + 1);
        }
    }
    void rebuild() {
        double b = pow(n, 1.0 / R), c = b;
        for (int i = 0; i < R - 1; i++, c *= b) {
            if (int(A[i].size()) > SCALE_FACTOR * c) {
                int nxtSz = int(A[i + 1].size());
                if (i == 0) sort(A[i].begin(), A[i].end(), pairCmp);
                else for (int j = int(A[i].size()) - 1; j >= 1; j--) A[i][j].second -= A[i][j - 1].second;
                for (int j = nxtSz - 1; j >= 1; j--) A[i + 1][j].second -= A[i + 1][j - 1].second;
                for (auto &&p : A[i]) A[i + 1].push_back(p);
                A[i].clear(); inplace_merge(A[i + 1].begin(), A[i + 1].begin() + nxtSz, A[i + 1].end(), pairCmp); resizeUnique(A[i + 1]);
            }
        }
    }
    void insert(const pair<Value, CountType> &p) { A[0].push_back(p); tot += p.second; n++; }
    void emplace(const Value &v, const CountType &c) { A[0].emplace_back(v, c); tot += c; n++; }
    CountType aboveInd(const Value &val) {
        rebuild(); CountType ret = 0;
        for (int i = R - 1; i >= 1; i--) {
            int ind = upper_bound(A[i].begin(), A[i].end(), make_pair(val, CountType(0)), pairCmp) - A[i].begin();
            ret += ind == 0 ? 0 : A[i][ind - 1].second;
        }
        for (auto &&p : A[0]) if (!cmp(val, p.first)) ret += p.second;
        return ret;
    }
    CountType ceilingInd(const Value &val) {
        rebuild(); CountType ret = 0;
        for (int i = R - 1; i >= 1; i--) {
            int ind = lower_bound(A[i].begin(), A[i].end(), make_pair(val, CountType(0)), pairCmp) - A[i].begin();
            ret += ind == 0 ? 0 : A[i][ind - 1].second;
        }
        for (auto &&p : A[0]) if (cmp(p.first, val)) ret += p.second;
        return ret;
    }
    CountType floorInd(const Value &val) { return aboveInd(val) - 1; }
    CountType belowInd(const Value &val) { return ceilingInd(val) - 1; }
    bool contains(const Value &val) {
        for (int i = R - 1; i >= 1; i--) if (binary_search(A[i].begin(), A[i].end(), make_pair(val, CountType(0)), pairCmp)) return true;
        rebuild();
        for (int i = R - 1; i >= 1; i--) if (binary_search(A[i].begin(), A[i].end(), make_pair(val, CountType(0)), pairCmp)) return true;
        for (auto &&p : A[0]) if (!cmp(val, p.first) && !cmp(p.first, val)) return true;
        return false;
    }
    CountType count(const Value &val) { return aboveInd(val) - ceilingInd(val); }
    // number of values in the range [lo, hi]
    CountType count(const Value &lo, const Value &hi) { return aboveInd(hi) - ceilingInd(lo); }
    CountType count() const { return tot; } 
    void clear() { tot = 0; for (int i = 0; i < R; i++) A[i].clear(); }
    vector<pair<Value, CountType>> valuesAndCount() const { // sorted
        vector<pair<Value, CountType>> ret;
        for (int i = 0; i < R; i++) {
            int mid = int(ret.size());
            for (auto &&p : A[i]) ret.push_back(p);
            if (i > 0) inplace_merge(ret.begin(), ret.begin() + mid, ret.end(), pairCmp);
        }
        resizeUnique(ret); return ret;
    }
};

template <class T, class IndexType, const int MAXN, const bool ONE_INDEXED, class Tree = RootBuffer<3, IndexType, T>> struct SemiSparseFenwickTree2DSqrt {
    Tree BIT[MAXN];
    void init(const double SCALE_FACTOR = 1) { for (int i = 0; i < MAXN; i++) BIT[i] = Tree(SCALE_FACTOR); }
    void clear() { for (int i = 0; i < MAXN; i++) BIT[i].clear(); }
    void update(int x, IndexType y, T v) { for (x += !ONE_INDEXED; x < MAXN; x += x & -x) BIT[x].emplace(y, v); }
    T rsq(int x, IndexType y) { T ret = 0; for (x += !ONE_INDEXED; x > 0; x -= x & -x) ret += BIT[x].aboveInd(y); return ret; }
    T rsq(int x, IndexType y1, IndexType y2) { T ret = 0; for (x += !ONE_INDEXED; x > 0; x -= x & -x) ret += BIT[x].count(y1, y2); return ret; }
    T rsq(int x1, IndexType y1, int x2, IndexType y2) { return rsq(x2, y1, y2) - rsq(x1 - 1, y1, y2); }
};

template <class T, class F> T getFirst(T lo, T hi, F f) {
    while (lo < hi) {
        T mid = lo + (hi - lo) / 2;
        if (f(mid)) hi = mid;
        else lo = mid + 1;
    }
    return lo;
}

template <class T, class F> T getLast(T lo, T hi, F f) {
    hi--;
    while (lo <= hi) {
        T mid = lo + (hi - lo) / 2;
        if (f(mid)) lo = mid + 1;
        else hi = mid - 1;
    }
    return hi;
}

const int MAXN = 3e5 + 5;

int N, Q;
bool cur[MAXN];
FenwickTree<ll, 1, MAXN> ft1;
SemiSparseFenwickTree2DSqrt<ll, int, MAXN, 1> ft2;

void update(int l1, int l2, int r1, int r2, ll v) {
    ft2.update(l1, r1, v);
    ft2.update(l1, r2 + 1, -v);
    ft2.update(l2 + 1, r1, -v);
    ft2.update(l2 + 1, r2 + 1, v);
}

int main() {
    // setInput("in.txt");
    // setOutput("out.txt");
    // setError("err.txt");
    setLength(MAXN);
    ft1.init();
    ft2.init(1);
    string S;
    read(N, Q, S);
    for (int i = 1; i <= N; i++) {
        cur[i] = S[i - 1] == '1';
        if (cur[i]) ft1.update(i, 1);
    }
    for (int qi = 1; qi <= Q; qi++) {
        read(S);
        if (S == "toggle") {
            int i;
            read(i);
            ll mul = cur[i] ? 1 : -1;
            if (!cur[i]) ft1.update(i, 1);
            int l1 = getFirst(1, i, [&] (int x) {
                return ft1.rsq(x, i) == i - x + 1;
            });
            int r2 = getLast(i, N + 1, [&] (int x) {
                return ft1.rsq(i, x) == x - i + 1;
            });
            if (cur[i]) ft1.update(i, -1);
            cur[i] = !cur[i];
            update(l1, i, i, r2, mul * qi);
        } else {
            int l, r;
            read(l, r);
            --r;
            ll ans = ft2.rsq(l, r);
            if (ft1.rsq(l, r) == r - l + 1) ans += qi;
            writeln(ans);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 56 ms 42744 KB Output is correct
2 Correct 53 ms 42616 KB Output is correct
3 Correct 53 ms 42744 KB Output is correct
4 Correct 54 ms 42616 KB Output is correct
5 Correct 53 ms 42744 KB Output is correct
6 Correct 53 ms 42616 KB Output is correct
7 Correct 54 ms 42700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 853 ms 158292 KB Output is correct
2 Correct 945 ms 145064 KB Output is correct
3 Correct 1560 ms 114212 KB Output is correct
4 Correct 2800 ms 132004 KB Output is correct
5 Correct 2488 ms 117584 KB Output is correct
6 Correct 2409 ms 368184 KB Output is correct
7 Correct 437 ms 50236 KB Output is correct
8 Correct 614 ms 51704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 43768 KB Output is correct
2 Correct 53 ms 43384 KB Output is correct
3 Correct 53 ms 43072 KB Output is correct
4 Correct 50 ms 42620 KB Output is correct
5 Correct 2822 ms 250400 KB Output is correct
6 Correct 3084 ms 161552 KB Output is correct
7 Correct 2253 ms 117320 KB Output is correct
8 Correct 461 ms 51548 KB Output is correct
9 Correct 168 ms 46664 KB Output is correct
10 Correct 189 ms 46732 KB Output is correct
11 Correct 187 ms 47164 KB Output is correct
12 Correct 468 ms 50252 KB Output is correct
13 Correct 478 ms 51692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 42748 KB Output is correct
2 Correct 59 ms 43256 KB Output is correct
3 Correct 54 ms 43768 KB Output is correct
4 Correct 53 ms 43904 KB Output is correct
5 Correct 712 ms 50052 KB Output is correct
6 Correct 1792 ms 208500 KB Output is correct
7 Correct 2385 ms 357608 KB Output is correct
8 Correct 2631 ms 429496 KB Output is correct
9 Correct 1223 ms 366940 KB Output is correct
10 Correct 1500 ms 433712 KB Output is correct
11 Correct 1253 ms 367484 KB Output is correct
12 Correct 1539 ms 434344 KB Output is correct
13 Correct 1247 ms 366760 KB Output is correct
14 Correct 1517 ms 435980 KB Output is correct
15 Correct 435 ms 45016 KB Output is correct
16 Correct 448 ms 46676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 42744 KB Output is correct
2 Correct 53 ms 42616 KB Output is correct
3 Correct 53 ms 42744 KB Output is correct
4 Correct 54 ms 42616 KB Output is correct
5 Correct 53 ms 42744 KB Output is correct
6 Correct 53 ms 42616 KB Output is correct
7 Correct 54 ms 42700 KB Output is correct
8 Correct 853 ms 158292 KB Output is correct
9 Correct 945 ms 145064 KB Output is correct
10 Correct 1560 ms 114212 KB Output is correct
11 Correct 2800 ms 132004 KB Output is correct
12 Correct 2488 ms 117584 KB Output is correct
13 Correct 2409 ms 368184 KB Output is correct
14 Correct 437 ms 50236 KB Output is correct
15 Correct 614 ms 51704 KB Output is correct
16 Correct 52 ms 43768 KB Output is correct
17 Correct 53 ms 43384 KB Output is correct
18 Correct 53 ms 43072 KB Output is correct
19 Correct 50 ms 42620 KB Output is correct
20 Correct 2822 ms 250400 KB Output is correct
21 Correct 3084 ms 161552 KB Output is correct
22 Correct 2253 ms 117320 KB Output is correct
23 Correct 461 ms 51548 KB Output is correct
24 Correct 168 ms 46664 KB Output is correct
25 Correct 189 ms 46732 KB Output is correct
26 Correct 187 ms 47164 KB Output is correct
27 Correct 468 ms 50252 KB Output is correct
28 Correct 478 ms 51692 KB Output is correct
29 Correct 50 ms 42748 KB Output is correct
30 Correct 59 ms 43256 KB Output is correct
31 Correct 54 ms 43768 KB Output is correct
32 Correct 53 ms 43904 KB Output is correct
33 Correct 712 ms 50052 KB Output is correct
34 Correct 1792 ms 208500 KB Output is correct
35 Correct 2385 ms 357608 KB Output is correct
36 Correct 2631 ms 429496 KB Output is correct
37 Correct 1223 ms 366940 KB Output is correct
38 Correct 1500 ms 433712 KB Output is correct
39 Correct 1253 ms 367484 KB Output is correct
40 Correct 1539 ms 434344 KB Output is correct
41 Correct 1247 ms 366760 KB Output is correct
42 Correct 1517 ms 435980 KB Output is correct
43 Correct 435 ms 45016 KB Output is correct
44 Correct 448 ms 46676 KB Output is correct
45 Correct 449 ms 142652 KB Output is correct
46 Correct 523 ms 122040 KB Output is correct
47 Correct 1413 ms 104532 KB Output is correct
48 Correct 2562 ms 139680 KB Output is correct
49 Correct 192 ms 46908 KB Output is correct
50 Correct 196 ms 47096 KB Output is correct
51 Correct 198 ms 47096 KB Output is correct
52 Correct 186 ms 47356 KB Output is correct
53 Correct 179 ms 47452 KB Output is correct
54 Correct 174 ms 47480 KB Output is correct