Submission #1151882

#TimeUsernameProblemLanguageResultExecution timeMemory
1151882SangMatching (CEOI11_mat)C++20
100 / 100
688 ms58316 KiB
#include<bits/stdc++.h>
using namespace std;

template <typename T>
class Modular {
 public:
   using Type = typename decay<decltype(T::value)>::type;

   constexpr Modular() : value() {}
   template <typename U>
   Modular(const U& x) {
      value = normalize(x);
   }

   template <typename U>
   static Type normalize(const U& x) {
      Type v;
      if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
      else v = static_cast<Type>(x % mod());
      if (v < 0) v += mod();
      return v;
   }

   const Type& operator()() const { return value; }
   template <typename U>
   explicit operator U() const { return static_cast<U>(value); }
   constexpr static Type mod() { return T::value; }

   Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
   Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
   template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
   template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
   Modular& operator++() { return *this += 1; }
   Modular& operator--() { return *this -= 1; }
   Modular operator++(int) { Modular result(*this); *this += 1; return result; }
   Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
   Modular operator-() const { return Modular(-value); }

   template <typename U = T>
   typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
      value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
      return *this;
   }
   template <typename U = T>
   typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
      long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod());
      value = normalize(value * rhs.value - q * mod());
      return *this;
   }
   template <typename U = T>
   typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
      value = normalize(value * rhs.value);
      return *this;
   }

   Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }

   friend const Type& abs(const Modular& x) { return x.value; }

   template <typename U>
   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);

   template <typename U>
   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);

   template <typename V, typename U>
   friend V& operator>>(V& stream, Modular<U>& number);

private:
   Type value;
};

template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }

template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }

template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }

template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }

template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }

template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
   Modular<T> x = a, res = 1;
   U p = b;
   while (p > 0) {
      if (p & 1) res *= x;
      x *= x;
      p >>= 1;
   }
   return res;
}

template <typename T>
bool IsZero(const Modular<T>& number) {
   return number() == 0;
}

template <typename T>
string to_string(const Modular<T>& number) {
   return to_string(number());
}

template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
   return stream << number();
}

template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
   typename common_type<typename Modular<T>::Type, long long>::type x;
   stream >> x;
   number.value = Modular<T>::normalize(x);
   return stream;
}

constexpr int mod = 1e9 + 2277;
using mint = Modular<std::integral_constant<decay<decltype(mod)>::type, mod>>;

#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
#define task "kbsiudthw"

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;

const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;

mint base = (int)101 + 3;

int n, m, a[N], h[N], pos[N];
mint pw[N];

struct Node{
   mint v;
   int cnt;
    Node(mint v = 0, int cnt = 0) : v(v), cnt(cnt) {};
} T[4*N];

Node combine(Node A, Node B){
    Node ans;
    ans.v = (A.v * pw[B.cnt] + B.v);
    ans.cnt = (A.cnt + B.cnt); 
    return ans;
}

void build(int id, int l, int r){
   if (l == r) {
      pos[l] = id;
      return;
   }
   int m = (l + r)/2;
   build(id*2, l, m);
   build(id*2+1, m+1, r);
}

void update(int id, Node v){
   T[id] = v;
   id /= 2;
   while (id){
      T[id] = combine(T[id*2], T[id*2+1]);
      id /= 2;
   }
}


signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> n >> m;
    FOR (i, 1, n) {
        cin >> a[i];
    }
    vi vals;
    FOR (i, 1, m) cin >> h[i], vals.pb(h[i]);
    sort(ALL(vals));
    FOR (i, 1, m) h[i] =  upper_bound(ALL(vals), h[i]) - vals.begin();
    pw[0] = 1;
    FOR (i, 1, N - 1) pw[i] = pw[i-1] * base;
    mint d = 0;
    FOR (i, 0, n - 1) d = (d + pw[i]); // 1111111...11111
    mint hsh = 0;
    FOR (i, 1, n) hsh = (hsh*base + a[i]);
    build(1, 1, m);
    FOR (i, 1, n) update(pos[h[i]], Node(i, 1));
    vi ans;
    if (T[1].v == hsh) ans.pb(1); 
    mint cur = d;
    FOR (i, 2, m - n + 1){
        update(pos[h[i-1]], Node());
        update(pos[h[i+n-1]], Node(i+n-1, 1));
        if (T[1].v - cur  == hsh) ans.pb(i);
        cur = (cur + d);
    }
    cout << ans.size() << '\n';
    for (int &x : ans) cout << x << ' ';

    return 0;
}

Compilation message (stderr)

mat.cpp: In function 'int main()':
mat.cpp:194:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  194 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mat.cpp:195:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  195 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...