QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#71820 | #5218. 字符串 | He_Ren | 100 ✓ | 2947ms | 48884kb | C++23 | 9.5kb | 2023-01-12 01:24:25 | 2023-01-12 01:24:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 5;
const int inf = 0x3f3f3f3f;
mt19937 rnd((unsigned long long)new char ^time(0));
const int mod = 1e9 + 7;
const int base = rnd() % 1000000 + 1000000 + 13579;
ll pwb[MAXN], sumpwb[MAXN];
struct Hash {
int h, len;
Hash(void) {}
explicit Hash(int x): h(x), len(1) {}
Hash(int _h, int _len): h(_h), len(_len) {}
};
Hash operator + (const Hash &p, const Hash &q) {
return Hash(((ll)p.h * pwb[q.len] + q.h) % mod, p.len + q.len);
}
Hash shift(const Hash &p, int k) {
return Hash((p.h + (ll)(k + mod) * sumpwb[p.len - 1]) % mod, p.len);
}
inline bool operator == (const Hash &p, const Hash &q) {
return p.h == q.h && p.len == q.len;
}
struct Segment_Tree_Hash {
Hash h[MAXN << 2];
int tag[MAXN << 2];
#define ls(u) ((u)<<1)
#define rs(u) ((u)<<1|1)
#define lson(u) ls(u),l,mid
#define rson(u) rs(u),mid+1,r
void push_up(int u) {
h[u] = h[ls(u)] + h[rs(u)];
}
void upd(int u, int k) {
h[u] = shift(h[u], k);
tag[u] += k;
}
void push_down(int u) {
if (tag[u]) {
upd(ls(u), tag[u]);
upd(rs(u), tag[u]);
tag[u] = 0;
}
}
void build(int u, int l, int r, int a[]) {
if (l == r) {
h[u] = Hash(a[l]);
return;
}
int mid = (l + r) >> 1;
build(lson(u), a);
build(rson(u), a);
push_up(u);
}
Hash query(int u, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)
return h[u];
push_down(u);
int mid = (l + r) >> 1;
if (ql <= mid && mid < qr)
return query(lson(u), ql, qr) + query(rson(u), ql, qr);
else
return ql <= mid ? query(lson(u), ql, qr) : query(rson(u), ql, qr);
}
void add(int u, int l, int r, int ql, int qr, int k) {
if (ql <= l && r <= qr) {
upd(u, k);
return;
}
push_down(u);
int mid = (l + r) >> 1;
if (ql <= mid)
add(lson(u), ql, qr, k);
if (mid < qr)
add(rson(u), ql, qr, k);
push_up(u);
}
int n;
int orih[MAXN];
bool hasupd;
void build(int _n, int a[]) {
n = _n;
build(1, 1, n, a);
hasupd = 0;
for (int i = 1; i <= n; ++i)
orih[i] = ((ll)orih[i - 1] * base + a[i]) % mod;
}
Hash query(int ql, int qr) {
if (!hasupd)
return Hash((orih[qr] - (ll)orih[ql - 1] * pwb[qr - ql + 1] % mod + mod) % mod);
else
return query(1, 1, n, ql, qr);
}
void add(int ql, int qr, int k) {
hasupd = 1;
add(1, 1, n, ql, qr, k);
}
int cmp(int l1, int r1, int l2, int r2) {
if (l1 > r1 && l2 > r2)
return 0;
if (l1 > r1)
return -2;
if (l2 > r2)
return 2;
int lim = min(r1 - l1 + 1, r2 - l2 + 1);
int vl = 0, vr = lim;
while (vl < vr) {
int mid = (vl + vr + 1) >> 1;
if (query(l1, l1 + mid - 1) == query(l2, l2 + mid - 1))
vl = mid;
else
vr = mid - 1;
}
if (vl == lim)
return r1 - l1 == r2 - l2 ? 0 : r1 - l1 < r2 - l2 ? -2 : 2;
else
return query(l1 + vl, l1 + vl).h < query(l2 + vl, l2 + vl).h ? -1 : 1;
}
int cmp(pii p, pii q) {
return cmp(p.first, p.second, q.first, q.second);
}
} hshtree;
struct Data {
int x, y, k, to;
Data(void) {}
Data(int _x, int _y, int _k, int _to): x(_x), y(_y), k(x == y ? 0 : _k), to(_to) {}
Data(int l, int r): x(l), y(l), k(0), to(r) {}
Data ext(int nxt) const {
if (x == y)
return Data(x, y, k, nxt);
int t = hshtree.cmp(x, nxt, x + k, nxt);
if (t == 2)
return Data(x, y, k, nxt);
if (t < 0)
return Data(x, nxt);
else
return Data(y, nxt);
}
pii segX(void) const {
return {x, to};
}
pii segY(void) const {
return {y, to};
}
};
struct Lis {
vector<Data> vec;
Lis(void) {}
Lis(const vector<Data> &_vec): vec(_vec) {}
Lis(const Data &dat): vec(1, dat) {}
};
ostream &operator << (ostream &os, const Data &p) {
return os << p.x << " " << p.y << " " << p.k << " " << p.to;
}
ostream &operator << (ostream &os, const Lis &p) {
for (auto t : p.vec)
os << t << endl;
return os;
}
Lis operator + (const Lis &p, const Lis &q) {
int nxt = q.vec[0].to;
vector<Data> res = q.vec;
for (const Data &t : p.vec) {
Data cur = t.ext(nxt);
while (res.size() && hshtree.cmp(cur.segY(), res.back().segY()) <= 0)
res.pop_back();
if (res.size()) {
if (hshtree.cmp(cur.segY(), res.back().segX()) == 1)
continue;
if (res.back().k != 0) {
Data oth = res.back();
res.pop_back();
int l = 0, r = (oth.y - oth.x) / oth.k;
while (l < r) {
int mid = (l + r) >> 1;
if (hshtree.cmp(cur.segY(), {oth.x + oth.k * l, oth.to}) == 2)
r = mid;
else
l = mid + 1;
}
oth.x += l * oth.k;
if (oth.x == oth.y)
oth.k = 0;
res.push_back(oth);
}
}
res.push_back(cur);
}
vector<Data> res2;
for (Data y : res) {
if (!res2.size()) {
res2.push_back(y);
continue;
}
Data x = res2.back();
bool ok = 1;
if (x.k && y.k && x.k != y.k)
ok = 0;
if (x.k && x.k != x.x - y.y)
ok = 0;
if (y.k && y.k != x.x - y.y)
ok = 0;
if (ok) {
res2.pop_back();
res2.push_back(Data(y.x, x.y, x.x - y.y, y.to));
} else {
res2.push_back(y);
}
}
return res2;
}
struct Segment_Tree_list {
Lis tree[MAXN << 2];
inline void push_up(int u) {
tree[u] = tree[ls(u)] + tree[rs(u)];
}
void build(int u, int l, int r) {
if (l == r) {
tree[u] = Data(l, l);
return;
}
int mid = (l + r) >> 1;
build(lson(u));
build(rson(u));
push_up(u);
}
void update(int u, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)
return;
int mid = (l + r) >> 1;
if (ql <= mid)
update(lson(u), ql, qr);
if (mid < qr)
update(rson(u), ql, qr);
push_up(u);
}
Lis query(int u, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
// cout << "tree [" << l << ", " << r << "]:\n" << tree[u] << endl;
return tree[u];
}
int mid = (l + r) >> 1;
if (ql <= mid && mid < qr) {
auto res = query(lson(u), ql, qr) + query(rson(u), ql, qr);
// cout << "cur [" << max(l,ql) << ", " << min(r,qr) << "]:\n" << res << endl;
return res;
} else
return ql <= mid ? query(lson(u), ql, qr) : query(rson(u), ql, qr);
}
} tree;
int n, Q;
int a[MAXN];
namespace Subtask1 {
const int MAXN = 3e2 + 5;
int lcp[MAXN][MAXN];
void buildlcp(void) {
for (int i = n; i >= 1; --i)
for (int j = n; j >= 1; --j)
lcp[i][j] = a[i] == a[j] ? lcp[i + 1][j + 1] + 1 : 0;
}
void solve(void) {
buildlcp();
bool tag = 1;
while (Q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r);
if (type == 1) {
int k;
scanf("%d", &k);
for (int i = l; i <= r; ++i)
a[i] += k;
tag = 0;
} else {
if (!tag)
buildlcp(), tag = 1;
int mn = l;
for (int i = l + 1; i <= r; ++i) {
int len = min(r - i + 1, lcp[mn][i]);
if (len == r - i + 1)
mn = i;
else if (a[mn + len] > a[i + len])
mn = i;
}
printf("%d\n", mn);
}
}
}
}
namespace Subtask2 {
void solve(void) {
pwb[0] = 1;
for (int i = 1; i < MAXN; ++i)
pwb[i] = (ll)pwb[i - 1] * base % mod;
sumpwb[0] = pwb[0];
for (int i = 1; i < MAXN; ++i)
sumpwb[i] = (sumpwb[i - 1] + pwb[i]) % mod;
const int CCC = 1e8 + 3e7 + 1;
for (int i = 1; i <= n; ++i)
a[i] += CCC;
hshtree.build(n, a);
tree.build(1, 1, n);
while (Q--) {
int type, l, r;
scanf("%d%d%d", &type, &l, &r);
if (type == 1) {
int k;
scanf("%d", &k);
hshtree.add(l, r, k);
tree.update(1, 1, n, l, r);
} else {
auto res = tree.query(1, 1, n, l, r);
printf("%d\n", res.vec[0].y);
}
}
}
}
int main(void) {
scanf("%d%d", &n, &Q);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
if (n <= 300)
Subtask1::solve();
else
Subtask2::solve();
return 0;
}
詳細信息
Test #1:
score: 10
Accepted
time: 6ms
memory: 24632kb
input:
300 300 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
37 65 65 49 89 81 13 97 73 65 35 9 59 37 77 89 91 99 29 85 77 67 81 55 99 17 5 21 13 77 17 99 69 55 97 73 83 97 25 25 53 55 97 57 99 17 33 57 75 69 77 59 9 73 35 95 89 61 9 61 61 131 125 182 154 167 199 187 190 155 156 152 132 139 182 170 139 136 169 195 192 151 177 186 136 153 186 132 148 193 173 1...
result:
ok 193 numbers
Test #2:
score: 10
Accepted
time: 433ms
memory: 31360kb
input:
20000 10000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 ...
output:
7829 9265 1953 6573 6057 663 3617 9361 3083 7179 7529 5457 9521 8069 2039 9745 6387 6565 4511 5481 5521 9349 9953 8233 5883 5841 1541 7913 8993 8945 7985 8701 5781 6349 9325 5507 5341 4767 4051 8305 2465 7809 5105 8101 5763 7355 5765 3537 2817 5355 5715 7585 4005 3429 2769 7001 7801 4095 3949 7455 5...
result:
ok 7497 numbers
Test #3:
score: 10
Accepted
time: 443ms
memory: 31644kb
input:
20000 10000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 ...
output:
8745 5593 6297 5197 9985 2545 9793 5633 5747 9259 5951 8393 8873 8513 9649 3703 6767 9183 9761 4641 6037 6241 4805 7897 9187 9441 4245 9987 1043 7701 9457 8921 3595 9871 8151 9773 1505 4877 7867 4437 6049 4317 7837 3059 8215 9217 5817 8537 8121 4949 1239 8249 7373 7897 8717 2103 7537 9459 3703 1089 ...
result:
ok 7507 numbers
Test #4:
score: 10
Accepted
time: 418ms
memory: 45208kb
input:
200000 30000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5...
output:
48105 56009 65135 23351 57329 44865 39825 26603 50825 58161 39213 36027 67485 59153 62733 46217 26541 69441 59297 24953 46825 40449 33027 44585 29297 52013 57301 43421 34095 43993 68513 44507 62125 52845 63935 52955 43891 62525 40405 57121 22233 42597 63313 50653 26645 6217 59451 59741 49239 24745 3...
result:
ok 30000 numbers
Test #5:
score: 10
Accepted
time: 413ms
memory: 45108kb
input:
200000 30000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5...
output:
62579 47205 49609 20253 59597 43011 61023 69777 16785 67707 68283 36559 11307 58705 29869 43875 61201 60545 51221 67185 49079 55145 14867 34921 44037 18129 22237 65677 51073 51841 39025 63349 20299 45379 8613 26241 20357 50541 48795 35673 66325 48177 55545 17087 33673 60823 50055 53603 60089 66925 2...
result:
ok 30000 numbers
Test #6:
score: 10
Accepted
time: 2461ms
memory: 47436kb
input:
200000 30000 1 0 0 1 0 1 0 0 1 1 0 0 0 0 0 1 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 1 1 1 1 0 0 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 1 1 1 1 1 1...
output:
75409 192357 134617 31333 28555 41015 111347 148163 180788 8834 176883 191492 65563 121357 176883 97659 97659 176883 163250 97659 50612 97659 176161 97659 97659 176161 176161 97659 97659 186590 50612 148252 50612 140945 97659 97659 97659 165173 97659 55518 97659 97659 97659 176161 106551 90620 16270...
result:
ok 15021 numbers
Test #7:
score: 10
Accepted
time: 2627ms
memory: 47264kb
input:
200000 30000 0 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 1 1 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 0 0...
output:
22076 155846 154485 90310 155846 154954 155846 28024 155846 137994 127166 147549 139039 133000 147549 147549 178752 133000 29506 124189 145505 147549 38460 127166 147549 9716 147549 154740 147549 67464 26504 26504 67464 147942 9716 147549 133000 119620 67464 133000 147549 186188 9716 9716 147549 674...
result:
ok 15006 numbers
Test #8:
score: 10
Accepted
time: 2895ms
memory: 47192kb
input:
200000 30000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5...
output:
24349 13095 53797 69079 61467 66041 13313 64421 49077 36859 25585 34465 47905 23953 40841 63801 30141 53879 64033 23593 41265 21683 55853 31913 56497 59649 50577 66357 62329 35889 58361 29701 19637 61665 6487 54425 51049 27033 69489 59073 38015 62573 12577 24113 42433 69485 35591 66337 33619 48623 2...
result:
ok 22326 numbers
Test #9:
score: 10
Accepted
time: 2947ms
memory: 48884kb
input:
200000 30000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5...
output:
45565 37639 65091 57863 52945 57691 49545 56447 28895 50211 66561 51027 37489 54597 17553 26317 29241 32779 52073 66257 39585 68895 55939 50071 35357 65889 43997 20043 58881 58783 56221 66643 55897 58193 42729 1049 58847 30617 59243 32519 67073 46279 51121 63297 30759 47419 55137 15121 65851 57097 6...
result:
ok 22335 numbers
Test #10:
score: 10
Accepted
time: 2795ms
memory: 47564kb
input:
200000 30000 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 8 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5...
output:
10401 44761 54013 39499 52447 69307 38947 51371 38201 37689 42159 48527 68951 49849 34945 68207 59707 59387 37873 46705 46561 51469 29111 39835 63611 37979 54021 60555 55041 54545 24877 60057 39833 50983 30057 33001 38185 66587 63697 52661 47945 40319 53985 13131 37685 48863 55353 64967 42801 32537 ...
result:
ok 22286 numbers
Extra Test:
score: 0
Extra Test Passed