QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#599590 | #9425. Generated String | ucup-team3099# | WA | 4ms | 4008kb | C++23 | 12.8kb | 2024-09-29 06:55:58 | 2024-09-29 06:55:59 |
Judging History
answer
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <algorithm>
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
class SuffixArray {
public:
template<class T>
std::vector<int> buildSuffix(const T &array) {
int n = (int) array.size();
std::vector<int> sa(n);
for(int i = 0; i < n; i++) {
sa[i] = i;
}
std::sort(sa.begin(), sa.end(), [&](int a, int b) { return array[a] < array[b]; });
int cur = 0;
std::vector<int> inv(n);
std::vector<int> nxt(n);
inv[sa[0]] = 0;
for(int i = 1; i < n; i++) {
inv[sa[i]] = (array[sa[i - 1]] != array[sa[i]] ? ++cur : cur);
}
cur++;
for(int k = 0; cur < n; k++) {
cur = 0;
auxSort(sa, inv, 1 << k);
for(int l = 0, r = 0; l < n; l = r, cur++) {
while(r < n && getPair(inv, sa[l], 1 << k) == getPair(inv, sa[r], 1 << k)) {
nxt[sa[r++]] = cur;
}
}
nxt.swap(inv);
}
return sa;
}
template<class T>
std::vector<int> buildLCP(const std::vector<int> &sa, const T &array) {
int n = (int) sa.size();
std::vector<int> inv(n);
for(int i = 0; i < n; i++) {
inv[sa[i]] = i;
}
std::vector<int> lcp(n, 0);
for(int i = 0, k = 0; i < n; i++) {
if(inv[i] + 1 == n) {
k = 0;
continue;
}
int j = sa[inv[i] + 1];
while(i + k < n && j + k < n && array[i + k] == array[j + k]) {
k++;
}
lcp[inv[i]] = k;
if(k > 0) {
k--;
}
}
return lcp;
}
private:
void auxSort(std::vector<int> &sa, const std::vector<int> &inv, int offset) {
// O(nlogn) step, O(nlog^2n) total
std::sort(sa.begin(), sa.end(), [&](int a, int b) { return getPair(inv, a, offset) < getPair(inv, b, offset); });
// O(n) step, O(nlogn) total -- TO DO --
}
std::pair<int, int> getPair(const std::vector<int> &inv, int pos, int offset) {
return std::pair<int, int>(inv[pos], pos + offset < (int) inv.size() ? inv[pos + offset] : -1);
}
};
template<class T>
struct Minimizer {
T operator() (T a, T b) { return std::min(a, b); }
};
template <class T, class Merger = Minimizer<T>>
class SparseTable {
public:
void init(const std::vector<T> &a) {
int e = 0;
int n = (int) a.size();
while((1 << e) / 2 < n) {
e++;
}
table.assign(e, std::vector<T>(n));
get.assign(n + 1, -1);
for(int i = 0; i < n; i++) {
table[0][i] = a[i];
get[i+1] = get[(i+1)/2] + 1;
}
for(int i = 0; i + 1 < e; i++) {
for(int j = 0; j + (1 << i) < n; j++) {
table[i+1][j] = merge(table[i][j], table[i][j + (1 << i)]);
}
}
}
T qry(int l, int r) {
if(l > r) std::swap(l, r);
if(l == r) return 1e9;
int e = get[r - l];
return merge(table[e][l], table[e][r - (1 << e)]);
}
int getPos(int x) {
while(x >= (int) get.size()) get.push_back(get[(int) get.size() / 2] + 1);
return get[x];
}
private:
std::vector<std::vector<T>> table;
std::vector<int> get;
Merger merge;
};
std::string str;
std::vector<int> sa, inv, lcp;
SparseTable<int> table;
long long lim = 1e18;
struct Info {
std::vector<std::pair<int, int>> ranges;
int id = -1;
void read() {
int n;
std::cin >> n;
ranges.resize(n);
for(int i = 0; i < n; i++) {
std::cin >> ranges[i].first >> ranges[i].second;
ranges[i].first--;
}
}
void reverse() {
for(auto &[l, r] : ranges) {
l = (int) str.size() - l;
r = (int) str.size() - r;
std::swap(l, r);
}
std::reverse(ranges.begin(), ranges.end());
}
void init(const std::vector<std::pair<int, int>> &_ranges) {
ranges = _ranges;
}
std::string buildNaive() const {
std::string ans;
for(auto [l, r] : ranges) {
ans += str.substr(l, r-l);
}
while((int) ans.size() > lim) {
ans.pop_back();
}
return ans;
}
bool operator < (const Info &o) const {
return buildNaive() < o.buildNaive();
std::pair<int, int> mine = ranges[0], other = o.ranges[0];
long long common = 0;
for(int i = 1, j = 1; str[mine.first] == str[other.first]; ) {
int use = std::min({mine.second - mine.first, other.second - other.first, table.qry(inv[mine.first], inv[other.first])});
mine.first += use;
other.first += use;
common += use;
if(common >= lim) {
return false;
}
if(mine.first == mine.second && i < (int) ranges.size()) {
mine = ranges[i++];
}
if(other.first == other.second && j < (int) o.ranges.size()) {
other = o.ranges[j++];
}
if(mine.first == mine.second) {
if(other.first == other.second) {
return false;
} else {
return true;
}
} else if(other.first == other.second) {
return false;
}
}
return str[mine.first] < str[other.first];
}
long long size() const {
long long ans = 0;
for(auto [l, r] : ranges) {
ans += r - l;
}
return ans;
}
};
template<class T = int>
struct Bit2D {
public:
Bit2D(std::vector<std::pair<T, T>> pts) {
std::sort(pts.begin(), pts.end());
for(auto a : pts) {
if(ord.empty() || a.first != ord.back()) {
ord.push_back(a.first);
}
}
fw.resize(ord.size() + 1);
coord.resize(fw.size());
for(auto &a : pts) {
std::swap(a.first, a.second);
}
std::sort(pts.begin(), pts.end());
for(auto &a : pts) {
std::swap(a.first, a.second);
for(int on = (int) (std::upper_bound(ord.begin(), ord.end(), a.first) - ord.begin()); on < (int) fw.size(); on += on & -on) {
if(coord[on].empty() || coord[on].back() != a.second) {
coord[on].push_back(a.second);
}
}
}
for(int i = 0; i < (int) fw.size(); i++) {
fw[i].assign(coord[i].size() + 1, 0);
}
}
void upd(T x, T y, T v) {
//std::cout << "updating (" << x << ", " << y << ") with " << v << '\n';
for(int xx = (int) (std::upper_bound(ord.begin(), ord.end(), x) - ord.begin()); xx < (int) fw.size(); xx += xx & -xx) {
for(int yy = (int) (std::upper_bound(coord[xx].begin(), coord[xx].end(), y) - coord[xx].begin()); yy < (int) fw[xx].size(); yy += yy & -yy) {
fw[xx][yy] += v;
}
}
}
T qry(T x, T y) {
T ans = 0;
for(int xx = (int) (std::upper_bound(ord.begin(), ord.end(), x) - ord.begin()); xx > 0; xx -= xx & -xx) {
for(int yy = (int) (std::upper_bound(coord[xx].begin(), coord[xx].end(), y) - coord[xx].begin()); yy > 0; yy -= yy & -yy) {
ans += fw[xx][yy];
}
}
return ans;
}
T qry(T x1, T y1, T x2, T y2) {
return qry(x2, y2) - qry(x2, y1 - 1) - qry(x1 - 1, y2) + qry(x1 - 1, y1 - 1);
}
void upd(T x1, T y1, T x2, T y2, T v) {
upd(x1, y1, v);
upd(x1, y2 + 1, -v);
upd(x2 + 1, y1, -v);
upd(x2 + 1, y2 + 1, v);
}
private:
std::vector<T> ord;
std::vector<std::vector<T>> fw, coord;
};
int main() {
std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
int n, q;
std::cin >> n >> q;
std::cin >> str;
{
auto rev = str;
std::reverse(rev.begin(), rev.end());
str += rev;
}
{
SuffixArray builder;
sa = builder.buildSuffix(str);
lcp = builder.buildLCP(sa, str);
table.init(lcp);
inv.resize(2*n);
for(int i = 0; i < 2*n; i++) {
inv[sa[i]] = i;
}
}
std::vector<Info> info0, info1;
std::vector<char> qries(q);
std::vector<int> back(q, -1);
std::vector<std::pair<int, int>> pts(q, {-1, -1});
std::vector<Info> wut0(q), wut1(q);
for(int i = 0; i < q; i++) {
std::cin >> qries[i];
if(qries[i] == '+') {
Info A;
A.read();
auto B = A;
B.reverse();
//std::cout << A.buildNaive() << ", " << B.buildNaive() << '\n';
A.id = B.id = i;
info0.push_back(A);
info1.push_back(B);
} else if(qries[i] == '-') {
std::cin >> back[i];
} else {
assert(qries[i] == '?');
wut0[i].read();
wut1[i].read();
wut1[i].reverse();
//std::cout << "! " << wut0[i].buildNaive() << ", " << wut1[i].buildNaive() << '\n';
}
}
std::sort(info0.begin(), info0.end());
std::sort(info1.begin(), info1.end());
for(int i = 0; i < (int) info0.size(); i++) {
//std::cout << "(" << info0[i].id << ", " << info1[i].id << ")\n";
pts[info0[i].id].first = i;
pts[info1[i].id].second = i;
}
Bit2D<int> fw(pts);
for(int i = 0; i < q; i++) {
if(qries[i] == '+') {
fw.upd(pts[i].first, pts[i].second, 1);
} else if(qries[i] == '-') {
fw.upd(pts[back[i]].first, pts[back[i]].second, -1);
} else {
assert(qries[i] == '?');
lim = wut0[i].size();
int x0 = (int) (std::lower_bound(info0.begin(), info0.end(), wut0[i]) - info0.begin());
int x1 = (int) (std::upper_bound(info0.begin(), info0.end(), wut0[i]) - info0.begin());
int y0 = (int) (std::lower_bound(info1.begin(), info1.end(), wut1[i]) - info1.begin());
int y1 = (int) (std::upper_bound(info1.begin(), info1.end(), wut1[i]) - info1.begin());
//std::cout << "[" << x0 << ", " << x1 << "] [" << y0 << ", " << y1 << "]\n";
std::cout << fw.qry(x0, y0, x1, y1) << '\n';
}
}
}
/*
NEVER FORGET TO:
Look at the problem's constraints before coding.
How to cheese cf:
Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
If it isn't the answer, have more faith or change to another bound god by looking for a better bound.
Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.
You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
Don't think about them being right or not, cf is a battle of intuition only.
Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.
Think about 2x2 cases for matrix problems and hope that everything works for the general case.
Find a necessary condition and trust it to be sufficient. They're basically the same thing.
Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.
For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C
Consider doing problems in reverse order of queries/updates
For combinatorics problems, consider symmetry
For problems that are similar to past problems, think about the differences betweem it and the current problem.
Sometimes the difference makes no difference. Sometimes it does.
General strategy (MUST DO):
Try to solve the problem with more restricted constraints.
About testing:
Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.
This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
8 7 abcaabbc + 3 1 3 2 4 3 8 + 2 1 4 1 8 + 1 2 4 ? 1 5 6 1 7 8 - 3 + 1 2 5 ? 1 2 3 1 5 5
output:
2 1
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 4008kb
input:
5 2000 bbaab + 1 3 5 + 2 5 5 3 5 - 2 ? 1 1 3 3 3 3 4 5 2 5 - 1 ? 3 1 1 2 4 1 5 1 3 4 ? 1 1 2 2 2 4 4 4 ? 1 1 5 1 5 5 + 3 1 2 1 5 1 4 ? 2 1 5 1 3 2 1 2 5 5 ? 1 3 4 1 4 5 - 9 ? 1 1 4 1 2 3 + 2 1 5 1 2 + 1 1 4 - 15 - 14 ? 2 2 5 2 5 1 3 4 + 1 2 3 - 19 + 3 1 4 1 5 4 5 - 21 + 1 2 5 ? 3 1 5 5 5 1 5 1 3 5 ?...
output:
0 0 0 0 0 1 0 0 0 3 0 0 3 0 0 0 2 0 2 0 0 0 0 0 0 0 0 0 0 3 8 3 0 16 0 0 0 0 2 0 0 1 0 0 0 0 0 0 17 1 9 0 6 12 9 1 8 0 1 2 0 0 0 15 0 12 0 0 0 0 9 26 12 28 0 0 15 0 10 19 2 0 0 0 5 0 0 0 0 0 0 7 1 0 4 0 21 25 21 0 0 0 10 14 0 0 3 0 14 0 0 4 0 3 42 6 0 17 2 0 24 6 0 0 0 4 0 0 49 25 1 50 0 51 52 29 0 ...
result:
wrong answer 6th lines differ - expected: '0', found: '1'