Submission #143023

#TimeUsernameProblemLanguageResultExecution timeMemory
143023popovicirobertMatching (CEOI11_mat)C++14
63 / 100
617 ms65536 KiB
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long /*const int MOD = ; inline int lgput(int a, int b) { int ans = 1; while(b > 0) { if(b & 1) ans = (1LL * ans * a) % MOD; b >>= 1; a = (1LL * a * a) % MOD; } return ans; } inline void mod(int &x) { if(x >= MOD) x -= MOD; } inline void add(int &x, int y) { x += y; mod(x); } inline void sub(int &x, int y) { x += MOD - y; mod(x); } inline void mul(int &x, int y) { x = (1LL * x * y) % MOD; } inline int inv(int x) { return lgput(x, MOD - 2); }*/ /*int fact[], invfact[]; inline void prec(int n) { fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } invfact[n] = lgput(fact[n], MOD - 2); for(int i = n - 1; i >= 0; i--) { invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD; } } inline int comb(int n, int k) { if(n < k) return 0; return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD; }*/ using namespace std; const int MAXN = (int) 1e6; const int B = 1e9 + 7; ull pw[MAXN + 1]; pair <int, int> h[MAXN + 1]; int nrm[MAXN + 1], n; struct Node { ull hsh; int cnt; }aint[2 * MAXN + 1]; inline Node refresh(Node &a, Node &b) { return {a.hsh * pw[b.cnt] + b.hsh, a.cnt + b.cnt}; } void add(int nod, int left, int right, int pos, int val) { if(left == right) { aint[nod] = {val, 1}; return ; } int mid = (left + right) / 2; if(pos <= mid) add(2 * nod, left, mid, pos, val); else add(2 * nod + 1, mid + 1, right, pos, val); aint[nod] = refresh(aint[2 * nod], aint[2 * nod + 1]); } void del(int nod, int left, int right, int pos) { if(left == right) { aint[nod] = {0, 0}; return ; } int mid = (left + right) / 2; if(pos <= mid) del(2 * nod, left, mid, pos); else del(2 * nod + 1, mid + 1, right, pos); aint[nod] = refresh(aint[2 * nod], aint[2 * nod + 1]); } int main() { #if 0 ifstream cin("A.in"); ofstream cout("A.out"); #endif int i, m; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> m >> n; pw[0] = 1; for(i = 1; i <= n; i++) { pw[i] = pw[i - 1] * B; } ull hsh = 0; for(i = 1; i <= m; i++) { int x; cin >> x; hsh += x * pw[m - i]; } for(i = 1; i <= n; i++) { cin >> h[i].first; h[i].second = i; } sort(h + 1, h + n + 1); for(i = 1; i <= n; i++) { nrm[h[i].second] = i; } for(i = 1; i < m; i++) { add(1, 1, n, nrm[i], i); } ull sum = 0; for(i = 0; i < m; i++) { sum += pw[i]; } vector <int> sol; for(i = 1; i <= n - m + 1; i++) { add(1, 1, n, nrm[i + m - 1], i + m - 1); if(aint[1].hsh - sum * (i - 1) == hsh) { sol.push_back(i); } del(1, 1, n, nrm[i]); } cout << sol.size() << "\n"; for(auto it : sol) { cout << it << " "; } return 0; }

Compilation message (stderr)

mat.cpp: In function 'void add(int, int, int, int, int)':
mat.cpp:81:28: warning: narrowing conversion of 'val' from 'int' to 'long long unsigned int' inside { } [-Wnarrowing]
         aint[nod] = {val, 1};
                            ^
#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...