Submission #1151593

#TimeUsernameProblemLanguageResultExecution timeMemory
1151593CDuongMatching (CEOI11_mat)C++20
63 / 100
1112 ms72864 KiB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define isz(x) (int)x.size()
using namespace std;

template<class data_t, data_t _mod>
struct modular_fixed_base{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>)
    static_assert(IS_UNSIGNED(data_t));
    static_assert(_mod >= 1);
    static constexpr bool VARIATE_MOD_FLAG = false;
    static constexpr data_t mod(){
        return _mod;
    }
    template<class T>
    static vector<modular_fixed_base> precalc_power(T base, int SZ){
        vector<modular_fixed_base> res(SZ + 1, 1);
        for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base;
        return res;
    }    
    static vector<modular_fixed_base> _INV;
    static void precalc_inverse(int SZ){
        if(_INV.empty()) _INV.assign(2, 1);
        for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]);
    }
    // _mod must be a prime
    static modular_fixed_base _primitive_root;
    static modular_fixed_base primitive_root(){
        if(_primitive_root) return _primitive_root;
        if(_mod == 2) return _primitive_root = 1;
        if(_mod == 998244353) return _primitive_root = 3;
        data_t divs[20] = {};
        divs[0] = 2;
        int cnt = 1;
        data_t x = (_mod - 1) / 2;
        while(x % 2 == 0) x /= 2;
        for(auto i = 3; 1LL * i * i <= x; i += 2){
            if(x % i == 0){
                divs[cnt ++] = i;
                while(x % i == 0) x /= i;
            }
        }
        if(x > 1) divs[cnt ++] = x;
        for(auto g = 2; ; ++ g){
            bool ok = true;
            for(auto i = 0; i < cnt; ++ i){
                if(modular_fixed_base(g).power((_mod - 1) / divs[i]) == 1){
                    ok = false;
                    break;
                }
            }
            if(ok) return _primitive_root = g;
        }
    }
    constexpr modular_fixed_base(){ }
    modular_fixed_base(const double &x){ data = _normalize(llround(x)); }
    modular_fixed_base(const long double &x){ data = _normalize(llround(x)); }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base(const T &x){ data = _normalize(x); }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){
        int sign = x >= 0 ? 1 : -1;
        data_t v =  _mod <= sign * x ? sign * x % _mod : sign * x;
        if(sign == -1 && v) v = _mod - v;
        return v;
    }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data; }
    modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; }
    modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); }
    modular_fixed_base &operator++(){ return *this += 1; }
    modular_fixed_base &operator--(){ return *this += _mod - 1; }
    modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; }
    modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; }
    modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); }
    modular_fixed_base &operator*=(const modular_fixed_base &rhs){
        if constexpr(is_same_v<data_t, unsigned int>) data = (unsigned long long)data * rhs.data % _mod;
        else if constexpr(is_same_v<data_t, unsigned long long>){
            long long res = data * rhs.data - _mod * (unsigned long long)(1.L / _mod * data * rhs.data);
            data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod);
        }
        else data = _normalize(data * rhs.data);
        return *this;
    }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
    modular_fixed_base &inplace_power(T e){
        if(e == 0) return *this = 1;
        if(data == 0) return *this = {};
        if(data == 1 || e == 1) return *this;
        if(data == mod() - 1) return e % 2 ? *this : *this = -*this;
        if(e < 0) *this = 1 / *this, e = -e;
        if(e == 1) return *this;
        modular_fixed_base res = 1;
        for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
        return *this = res;
    }
    template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
    modular_fixed_base power(T e) const{
        return modular_fixed_base(*this).inplace_power(e);
    }
    modular_fixed_base &operator/=(const modular_fixed_base &otr){
        make_signed_t<data_t> a = otr.data, m = _mod, u = 0, v = 1;
        if(a < _INV.size()) return *this *= _INV[a];
        while(a){
            make_signed_t<data_t> t = m / a;
            m -= t * a; swap(a, m);
            u -= t * v; swap(u, v);
        }
        assert(m == 1);
        return *this *= u;
    }
#define ARITHMETIC_OP(op, apply_op)\
modular_fixed_base operator op(const modular_fixed_base &x) const{ return modular_fixed_base(*this) apply_op x; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
modular_fixed_base operator op(const T &x) const{ return modular_fixed_base(*this) apply_op modular_fixed_base(x); }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend modular_fixed_base operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x) apply_op y; }
    ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=)
#undef ARITHMETIC_OP
#define COMPARE_OP(op)\
bool operator op(const modular_fixed_base &x) const{ return data op x.data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
bool operator op(const T &x) const{ return data op modular_fixed_base(x).data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend bool operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x).data op y.data; }
    COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
    friend istream &operator>>(istream &in, modular_fixed_base &number){
        long long x;
        in >> x;
        number.data = modular_fixed_base::_normalize(x);
        return in;
    }
//#define _SHOW_FRACTION
    friend ostream &operator<<(ostream &out, const modular_fixed_base &number){
        out << number.data;
    #if defined(LOCAL) && defined(_SHOW_FRACTION)
        cerr << "(";
        for(auto d = 1; ; ++ d){
            if((number * d).data <= 1000000){
                cerr << (number * d).data;
                if(d != 1) cerr << "/" << d;
                break;
            }
            else if((-number * d).data <= 1000000){
                cerr << "-" << (-number * d).data;
                if(d != 1) cerr << "/" << d;
                break;
            }
        }
        cerr << ")";
    #endif
        return out;
    }
    data_t data = 0;
#undef _SHOW_FRACTION
#undef IS_INTEGRAL
#undef IS_UNSIGNED
};
template<class data_t, data_t _mod> vector<modular_fixed_base<data_t, _mod>> modular_fixed_base<data_t, _mod>::_INV;
template<class data_t, data_t _mod> modular_fixed_base<data_t, _mod> modular_fixed_base<data_t, _mod>::_primitive_root;

const unsigned int mod = (119 << 23) + 1; // 998244353
// const unsigned int mod = 1e9 + 7; // 1000000007
// const unsigned int mod = 1e9 + 9; // 1000000009
// const unsigned long long mod = (unsigned long long)1e18 + 9;
using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>;

// Requires modular
template<class modular_t, class len_t, bool ALLOW_BINEXP>
struct hash_base{
#ifdef LOCAL
    #define ASSERT(c) assert(c)
#else
    #define ASSERT(c) 42
#endif
    static modular_t _base, _inv_base;
    template<class T = int>
    static void setup(T base = 0){
        if constexpr(modular_t::VARIATE_MOD_FLAG) modular_t::setup((unsigned long long)1e18 + 9);
        if(!base) base = mt19937(chrono::high_resolution_clock::now().time_since_epoch().count())() % 100'000'000 + 100'000'000;
        _base = base, _inv_base = modular_t(1) / base;
    }
    static vector<modular_t> _power, _inv_power;
    static void setup_power(size_t len){
        if(_power.empty()) _power.push_back(1), _inv_power.push_back(1);
        while((int)_power.size() <= len){
            _power.push_back(_power.back() * _base);
            _inv_power.push_back(_inv_power.back() * _inv_base);
        }
    }
    static modular_t power(len_t e){
        assert(e >= 0);
        if constexpr(ALLOW_BINEXP) return e < (int)_power.size() ? _power[e] : _base.power(e);
        else{
            if((int)_power.size() <= e) setup_power(e);
            return _power[e];
        }
    }
    static modular_t inv_power(len_t e){
        assert(e >= 0);
        if constexpr(ALLOW_BINEXP) return e < (int)_inv_power.size() ? _inv_power[e] : _inv_base.power(e);
        else{
            if((int)_power.size() <= e) setup_power(e);
            return _inv_power[e];
        }
    }
    hash_base(){ ASSERT(_base >= 1); }
    hash_base(const modular_t &x, len_t len): data(x), len(len){ ASSERT(_base >= 1); }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base(T x): data(x), len(1){ ASSERT(_base >= 1); }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base(const vector<T> &s){
        ASSERT(_base >= 1);
        for(auto c: s) *this += hash_base(c);
    }
    hash_base(const string &s){
        ASSERT(_base >= 1);
        for(auto c: s) *this += hash_base(c);
    }
    hash_base &operator=(const hash_base &x){
        data = x.data, len = x.len;
        return *this;
    }
    hash_base &operator+=(const hash_base &x){
        data = power(x.len) * data + x.data;
        len += x.len;
        return *this;
    }
    hash_base operator+(const hash_base &x) const{ return hash_base(*this) += x; }
    hash_base &inplace_append_right(const hash_base &x){ return *this += x; }
    hash_base append_right(const hash_base &x) const{ return hash_base(*this).inplace_append_right(x); }
    hash_base &inplace_append_left(const hash_base &x){
        data += power(len) * x.data;
        len += x.len;
        return *this;
    }
    hash_base append_left(const hash_base &x) const{ return hash_base(*this).inplace_append_left(x); }
    hash_base &inplace_pop_right(const hash_base &x){
        assert(len >= x.len);
        data = inv_power(x.len) * (data - x.data);
        len -= x.len;
        return *this;
    }
    hash_base pop_right(const hash_base &x) const{ return hash_base(*this).inplace_pop_right(x); }
    hash_base &inplace_pop_left(const hash_base &x){
        assert(len >= x.len);
        data -= power(len - x.len) * x.data;
        len -= x.len;
        return *this;
    }
    hash_base pop_left(const hash_base &x) const{ return hash_base(*this).inplace_pop_left(x); }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base &inplace_update(len_t pos, T x){
        assert(0 <= pos && pos < len);
        data += power(len - pos - 1) * x;
        return *this;
    }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base update(len_t pos, T x) const{ return hash_base(*this).inplace_update(pos, x); }
    hash_base &inplace_update(len_t pos, const hash_base &x){
        assert(0 <= pos && pos + x.len <= len);
        data += power(len - pos - x.len) * x.data;
        return *this;
    }
    hash_base update(len_t pos, const hash_base &x) const{ return hash_base(*this).inplace_update(pos, x); }
#define COMPARE_OP(op)\
bool operator op(const hash_base &x) const{ return data op x.data; }
    COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base &operator*=(T x){
        assert(x >= 0);
        if(x == 0) return *this = {};
        if(x == 1) return *this;
        hash_base res{};
        for(auto e = x; e; e >>= 1){
            if(e & 1) res += *this;
            *this += *this;
        }
        return *this = res;
    }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    hash_base operator*(T x) const{ return hash_base(*this) *= x; }
    template<class T, typename enable_if<is_integral_v<T>>::type* = nullptr>
    friend hash_base operator*(T x, const hash_base &h){ return hash_base(h) *= x; }
    friend ostream &operator<<(ostream &out, const hash_base &x){ return out << "{" << x.data << ", " << x.len << "}"; }
    modular_t data = 0;
    len_t len = 0;
#undef ASSERT
};
template<class modular_t, class len_t, bool ALLOW_BINEXP> modular_t hash_base<modular_t, len_t, ALLOW_BINEXP>::_base;
template<class modular_t, class len_t, bool ALLOW_BINEXP> modular_t hash_base<modular_t, len_t, ALLOW_BINEXP>::_inv_base;
template<class modular_t, class len_t, bool ALLOW_BINEXP> vector<modular_t> hash_base<modular_t, len_t, ALLOW_BINEXP>::_power{1};
template<class modular_t, class len_t, bool ALLOW_BINEXP> vector<modular_t> hash_base<modular_t, len_t, ALLOW_BINEXP>::_inv_power{1};

using hash_t = hash_base<modular_fixed_base<unsigned long long, (unsigned long long)1e18 + 9>, int, false>;

template<bool HAS_QUERY, bool HAS_UPDATE, class T, class U, class F1, class F2, class F3>
struct segment_tree_base{
    static_assert(HAS_QUERY || HAS_UPDATE);
#define ifQ if constexpr(HAS_QUERY)
#define ifU if constexpr(HAS_UPDATE)
    int n, size, log;
    vector<T> data;
    vector<U> data_action;
    F1 TT; // monoid operation (always adjacent)
    T T_id; // monoid identity
    F2 UU; // monoid operation (superset, subset)
    U U_id; // monoid identity
    F3 UT; // action of U on T (superset, subset)
    // O(n)
    segment_tree_base(F1 TT, T T_id, F2 UU, U U_id, F3 UT): TT(TT), T_id(T_id), UU(UU), U_id(U_id), UT(UT){ }
    segment_tree_base &operator=(const segment_tree_base &seg){
        n = seg.n;
        size = seg.size;
        log = seg.log;
        data = seg.data;
        data_action = seg.data_action;
    }
    // O(1)
    friend void swap(segment_tree_base &x, segment_tree_base &y){
        swap(x.n, y.n);
        swap(x.size, y.size);
        swap(x.log, y.log);
        swap(x.data, y.data);
        swap(x.data_action, y.data_action);
    }
    // O(n)
    void build(int n){
        assert(n >= 0);
        this->n = n;
        size = 1;
        while(size < n) size <<= 1;
        log = __lg(size);
        ifQ data.assign(size << 1, T_id);
        ifU data_action.assign(HAS_QUERY ? size : size << 1, U_id);
    }
    // O(n)
    void build(int n, T x){
        static_assert(HAS_QUERY);
        assert(n >= 0);
        this->n = n;
        size = 1;
        while(size < n) size <<= 1;
        log = __lg(size);
        data.assign(size << 1, T_id);
        fill(data.begin() + size, data.begin() + size + n, x);
        for(auto i = size - 1; i >= 1; -- i) refresh(i);
        ifU data_action.assign(size, U_id);
    }
    // O(n)
    template<class V>
    void build(const vector<V> &a){
        static_assert(HAS_QUERY);
        n = (int)a.size();
        size = 1;
        while(size < n) size <<= 1;
        log = __lg(size);
        data.assign(size << 1, T_id);
        copy(a.begin(), a.end(), data.begin() + size);
        for(auto i = size - 1; i >= 1; -- i) refresh(i);
        ifU data_action.assign(size, U_id);
    }
    // O(n)
    void build_action(int n){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        assert(n >= 0);
        build(n);
    }
    // O(n)
    void build_action(int n, U f){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        assert(n >= 0);
        this->n = n;
        size = 1;
        while(size < n) size <<= 1;
        log = __lg(size);
        data_action.assign(size << 1, U_id);
        fill(data_action.begin() + size, data_action.begin() + size + n, f);
    }
    // O(n)
    template<class V>
    void build_action(const vector<V> &a){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        n = (int)a.size();
        size = 1;
        while(size < n) size <<= 1;
        log = __lg(size);
        data_action.assign(size << 1, U_id);
        copy(a.begin(), a.end(), data_action.begin() + size);
    }
    // O(1)
    void refresh(int u){
        static_assert(HAS_QUERY);
        data[u] = TT(data[u << 1], data[u << 1 | 1]);
    }
    // O(1)
    void apply(int u, U f){
        static_assert(HAS_UPDATE);
        ifQ data[u] = UT(f, data[u]);
        if(!HAS_QUERY || u < size) data_action[u] = UU(f, data_action[u]);
    }
    // O(1)
    void push(int u){
        static_assert(HAS_UPDATE);
        apply(u << 1, data_action[u]), apply(u << 1 | 1, data_action[u]);
        data_action[u] = U_id;
    }
    // O(log(n)) if HAS_UPDATE, O(1) otherwise.
    T query(int p){
        static_assert(HAS_QUERY);
        assert(0 <= p && p < n);
        p += size;
        ifU for(auto i = log; i >= 1; -- i) push(p >> i);
        return data[p];
    }
    // O(log(n))
    U query_action(int p){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        assert(0 <= p && p < n);
        p += size;
        ifU for(auto i = log; i >= 1; -- i) push(p >> i);
        return data_action[p];
    }
    // O(log(n))
    T query(int ql, int qr){
        static_assert(HAS_QUERY);
        assert(0 <= ql && ql <= qr && qr <= n);
        if(ql == qr) return T_id;
        ql += size, qr += size;
        ifU for(auto i = log; i >= 1; -- i){
            if(ql >> i << i != ql) push(ql >> i);
            if(qr >> i << i != qr) push(qr >> i);
        }
        T res_left = T_id, res_right = T_id;
        for(; ql < qr; ql >>= 1, qr >>= 1){
            if(ql & 1) res_left = TT(res_left, data[ql ++]);
            if(qr & 1) res_right = TT(data[-- qr], res_right);
        }
        return TT(res_left, res_right);
    }
    // O(1)
    T query_all() const{
        static_assert(HAS_QUERY);
        return data[1];
    }
    // O(n)
    vector<T> to_array(){
        static_assert(HAS_QUERY);
        ifU for(auto u = 1; u < size; ++ u) push(u);
        return vector<T>(data.begin() + size, data.begin() + size + n);
    }
    // O(n)
    vector<U> to_array_of_updates(){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        for(auto u = 1; u < size; ++ u) push(u);
        return vector<U>(data_action.begin() + size, data_action.begin() + size + n);
    }
    // O(log(n))
    void set(int p, T x){
        static_assert(HAS_QUERY);
        assert(0 <= p && p < n);
        p += size;
        ifU for(auto i = log; i >= 1; -- i) push(p >> i);
        data[p] = x;
        for(auto i = 1; i <= log; ++ i) refresh(p >> i);
    }
    // O(log(n))
    void set_action(int p, U f){
        static_assert(!HAS_QUERY && HAS_UPDATE);
        assert(0 <= p && p < n);
        p += size;
        for(auto i = log; i >= 1; -- i) push(p >> i);
        data_action[p] = f;
    }
    // O(log(n))
    void update(int p, U f){
        static_assert(HAS_UPDATE);
        assert(0 <= p && p < n);
        p += size;
        for(auto i = log; i >= 1; -- i) push(p >> i);
        ifQ{
            data[p] = UT(f, data[p]);
            for(auto i = 1; i <= log; ++ i) refresh(p >> i);
        }
        else data_action[p] = UU(f, data_action[p]);
    }
    // O(log(n))
    void update(int ql, int qr, U f){
        static_assert(HAS_UPDATE);
        assert(0 <= ql && ql <= qr && qr <= n);
        if(ql == qr) return;
        ql += size, qr += size;
        for(auto i = log; i >= 1; -- i){
            if(ql >> i << i != ql) push(ql >> i);
            if(qr >> i << i != qr) push(qr >> i);
        }
        int _ql = ql, _qr = qr;
        for(; ql < qr; ql >>= 1, qr >>= 1){
            if(ql & 1) apply(ql ++, f);
            if(qr & 1) apply(-- qr, f);
        }
        ql = _ql, qr = _qr;
        ifQ for(auto i = 1; i <= log; ++ i){
            if(ql >> i << i != ql) refresh(ql >> i);
            if(qr >> i << i != qr) refresh(qr >> i);
        }
    }
    void update_beats(int ql, int qr, auto exit_rule, auto enter_rule, auto update_rule){
        static_assert(HAS_QUERY && HAS_UPDATE);
        assert(0 <= ql && ql <= qr && qr <= n);
        if(ql == qr) return;
        ql += size, qr += size;
        for(auto i = log; i >= 1; -- i){
            if(ql >> i << i != ql) push(ql >> i);
            if(qr >> i << i != qr) push(qr >> i);
        }
        auto recurse = [&](auto self, int u)->void{
            if(exit_rule(data[u])) return;
            if(enter_rule(data[u])){
                apply(u, update_rule(data[u]));
                return;
            }
            push(u);
            self(self, u << 1), self(self, u << 1 | 1);
            refresh(u);
        };
        int _ql = ql, _qr = qr;
        for(; ql < qr; ql >>= 1, qr >>= 1){
            if(ql & 1) recurse(recurse, ql ++);
            if(qr & 1) recurse(recurse, -- qr);
        }
        ql = _ql, qr = _qr;
        for(auto i = 1; i <= log; ++ i){
            if(ql >> i << i != ql) refresh(ql >> i);
            if(qr >> i << i != qr) refresh(qr >> i);
        }
    }
    // pred(sum[ql, r)) is T, T, ..., T, F, F, ..., F
    // Returns max r with T
    // O(log(n))
    int max_pref(int ql, auto pred){
        static_assert(HAS_QUERY);
        assert(0 <= ql && ql <= n && pred(T_id));
        if(ql == n) return n;
        ql += size;
        ifU for(auto i = log; i >= 1; -- i) push(ql >> i);
        T sum = T_id;
        do{
            while(~ql & 1) ql >>= 1;
            if(!pred(TT(sum, data[ql]))){
                while(ql < size){
                    ifU push(ql);
                    ql = ql << 1;
                    if(pred(TT(sum, data[ql]))) sum = TT(sum, data[ql ++]);
                }
                return ql - size;
            }
            sum = TT(sum, data[ql]);
            ++ ql;
        }while((ql & -ql) != ql);
        return n;
    }
    // pred(sum[l, qr)) is F, F, ..., F, T, T, ..., T
    // Returns min l with T
    // O(log(n))
    int min_suff(int qr, auto pred){
        static_assert(HAS_QUERY);
        assert(0 <= qr && qr <= n && pred(T_id));
        if(qr == 0) return 0;
        qr += size;
        ifU for(auto i = log; i >= 1; -- i) push(qr - 1 >> i);
        T sum = T_id;
        do{
            -- qr;
            while(qr > 1 && qr & 1) qr >>= 1;
            if(!pred(TT(data[qr], sum))){
                while(qr < size){
                    ifU push(qr);
                    qr = qr << 1 | 1;
                    if(pred(TT(data[qr], sum))) sum = TT(data[qr --], sum);
                }
                return qr + 1 - size;
            }
            sum = TT(data[qr], sum);
        }while((qr & -qr) != qr);
        return 0;
    }
    template<class output_stream>
    friend output_stream &operator<<(output_stream &out, segment_tree_base<HAS_QUERY, HAS_UPDATE, T, U, F1, F2, F3> seg){
        out << "{";
        for(auto i = 0; i < seg.n; ++ i){
            ifQ out << seg.query(i);
            else out << seg.query_action(i);
            if(i != seg.n - 1) out << ", ";
        }
        return out << '}';
    }
#undef ifQ
#undef ifU
};

// Supports query
template<class T, class F>
auto make_Q_segment_tree(F TT, T T_id){
    using U = int;
    auto _UU = [&](U, U)->U{ return U{}; };
    auto _UT = [&](U, T)->T{ return T{}; };
    return segment_tree_base<true, false, T, U, F, decltype(_UU), decltype(_UT)>(TT, T_id, _UU, U{}, _UT);
}
// Supports update
template<class U, class F>
auto make_U_segment_tree(F UU, U U_id){
    using T = int;
    auto _TT = [&](T, T)->T{ return T{}; };
    auto _UT = [&](U, T)->T{ return T{}; };
    return segment_tree_base<false, true, T, U, decltype(_TT), F, decltype(_UT)>(_TT, T{}, UU, U_id, _UT);
}
// Supports query and update
template<class T, class U, class F1, class F2, class F3>
auto make_QU_segment_tree(F1 TT, T T_id, F2 UU, U U_id, F3 UT){
    return segment_tree_base<true, true, T, U, F1, F2, F3>(TT, T_id, UU, U_id, UT);
}

struct node {
    hash_t val;
    int lx = -1, rx = -1;
    node() {}
    node(int val) : lx(val), rx(val) {}
};

void solve() {
    int m, n;
    cin >> m >> n;

    hash_t::setup();

    hash_t sigma;
    vector<int> a(m), b(n);
    for (int i = 0; i < m; ++i) {
        cin >> a[i];
        if (i) sigma.inplace_append_right(a[i] - a[i - 1]);
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }

    vector<int> aidx(n);
    iota(all(aidx), 0);
    sort(all(aidx), [&](int x, int y) {
        return b[x] < b[y];
    });

    vector<int> nidx(n);
    for (int i = 0; i < n; ++i) nidx[aidx[i]] = i;

    auto TT = [&](const auto &lhs, const auto &rhs) {
        if (lhs.rx == -1) return rhs;
        if (rhs.lx == -1) return lhs;
        node res = lhs;
        res.val.inplace_append_right(rhs.lx - lhs.rx);
        res.val.inplace_append_right(rhs.val);
        res.lx = lhs.lx, res.rx = rhs.rx;
        return res;
    };
    auto st = make_Q_segment_tree(TT, node());
    st.build(n);

    vector<int> res;
    for (int i = 0; i < m; ++i) {
        st.set(nidx[i], node(i));
    }
    if (st.query_all().val == sigma) {
        res.emplace_back(0);
    }
    for (int i = m; i < n; ++i) {
        st.set(nidx[i], node(i));
        st.set(nidx[i - m], node());
        if (st.query_all().val == sigma) {
            res.emplace_back(i - m + 1);
        }
    }

    cout << isz(res) << "\n";
    for (auto val : res) {
        cout << val + 1 << " ";
    }
    cout << "\n";
}

signed main() {

#ifndef CDuongg
    if (fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...