이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
struct BIT {
vector<int> tr;
BIT (int sz) : tr(sz + 1, INT_MIN) {}
int p (int k) { return k & -k; }
void update (int k, int val) {
for (; k < tr.size(); k += p(k))
tr[k] = max(tr[k], val);
}
int query (int k, int ans = INT_MIN) {
for (; k; k -= p(k)) ans = max(ans, tr[k]);
return ans;
}
};
const int mn = 2e6 + 6;
int p[mn], s[mn], lb[mn], rb[mn], z[mn];
bool ok (int i, int nxt) {
// check if [i..nxt] is isomorphic to [0..p]
// knowing that [i..nxt-1] is already isomorphic to the prefix
if (max(s[i], s[nxt]) == INT_MAX) return 0;
int p = nxt - i;
if (lb[p] != -1) {
int dist = p - lb[p];
if (nxt - dist < 0 || s[nxt - dist] > s[nxt]) return 0; // s[nxt - dist] must be < s[nxt]
}
if (rb[p] != -1) {
int dist = p - rb[p];
if (nxt - dist < 0 || s[nxt - dist] < s[nxt]) return 0; // s[nxt - dist] must be > s[nxt]
}
return 1;
}
void zFunc (int n) {
int x = 0, y = 0;
for (int i = 1; i < n; i++) {
z[i] = min(0, max(y - i + 1, z[i - x]));
if (i + z[i] > y) {
while (i + z[i] < n && ok(i, i + z[i]))
z[i]++, y++;
x = i;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
int len = n + m + 1;
for (int i = 1; i <= n; i++) {
cin >> p[i];
s[--p[i]] = i;
}
s[n] = INT_MAX;
for (int i = n + 1; i <= n + m; i++) cin >> s[i];
BIT lt(n + 1), rt(n + 1);
for (int i = 0; i < n; i++) {
int lval = lt.query(s[i]), rval = rt.query(n - s[i] + 1);
lb[i] = (lval == INT_MIN ? -1 : p[lval]);
rb[i] = (rval == INT_MIN ? -1 : p[-rval]);
lt.update(s[i], s[i]);
rt.update(n - s[i] + 1, -s[i]);
}
zFunc(len);
vector<int> ans;
for (int i = n + 1; i <= n + m; i++)
if (z[i] == n) ans.push_back(i - n);
cout << ans.size() << "\n";
for (int u : ans) cout << u << " ";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
mat.cpp: In member function 'void BIT::update(int, int)':
mat.cpp:20:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (; k < tr.size(); k += p(k))
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |