제출 #1284286

#제출 시각아이디문제언어결과실행 시간메모리
1284286dex111222333444555Matching (CEOI11_mat)C++20
100 / 100
590 ms50700 KiB
#include <bits/stdc++.h> #define all(v) begin(v), end(v) using namespace std; const int MAXN = 1e6 + 6, base = 1e6 + 4837, mod = 1e9 + 7; void add(int &x, const int &y){ x += y; if (x >= mod) x -= mod; } void sub(int &x, const int &y){ x -= y; if (x < 0) x += mod; } int mul(const int &a, const int &b){ return 1LL * a * 1LL * b % mod; } int lenA, lenB, valA[MAXN], valB[MAXN], powB[MAXN], hashA, id[MAXN], up; vector<int> compress; struct segmentTree{ struct Node{ int sum, cnt; Node(int _sum = 0, int _cnt = 0): sum(_sum), cnt(_cnt) {}; }; int n; vector<Node> node; segmentTree(int _n = 0): n(_n){ node.assign(n << 2 | 1, Node()); } Node combine(const Node &left, const Node &right){ if (left.cnt == 0) return right; if (right.cnt == 0 ) return left; Node mid; mid.cnt = left.cnt + right.cnt; mid.sum = mul(left.sum, powB[right.cnt]); add(mid.sum, right.sum); return mid; } void update(int id, int l, int r, int pos, int x, int p = 0){ if (l == r){ node[id] = Node(x, p); return; } int m = (l + r)>> 1; if (pos <= m) update(id << 1, l, m, pos, x, p); else update(id << 1 | 1, m + 1, r, pos, x, p); node[id] = combine(node[id << 1], node[id << 1 | 1]); } void update(int pos, int x, int p = 0){ update(1, 1, n, pos, x, p); } int getSum(int l, int r){ return node[1].sum; } } hashB; int getPos(int x){ return lower_bound(all(compress), x) - begin(compress) + 1; } void init(){ sort(all(compress)); for(int i = 1; i <= lenB; i++) valB[i] = getPos(valB[i]); hashB = segmentTree(lenB); powB[0] = 1; for(int i = 1; i <= lenA; i++){ powB[i] = 1LL * powB[i - 1] * 1LL * base % mod; hashA = mul(hashA, base); up = mul(up, base); add(hashA, valA[i]); add(up, 1); } } void input(){ cin >> lenA >> lenB; for(int i = 1; i <= lenA; i++) cin >> valA[i]; for(int i = 1; i <= lenB; i++) cin >> valB[i], compress.push_back(valB[i]); } void dex(){ init(); vector<int> res; for(int i = 1; i <= lenB; i++){ hashB.update(valB[i], i, 1); if (i >= lenA){ if (hashA == hashB.getSum(1, lenB)) res.push_back(i - lenA + 1); hashB.update(valB[i - lenA + 1], 0); add(hashA, up); } } cout << res.size() << '\n'; for(int x: res) cout << x << ' '; cout << '\n'; cerr << res.size() << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("mohinhgia.inp", "r")){ freopen("mohinhgia.inp", "r", stdin); freopen("mohinhgia.out", "w", stdout); } input(); dex(); cerr << 1.0 *clock() / CLOCKS_PER_SEC << ".s\n"; }

컴파일 시 표준 에러 (stderr) 메시지

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