QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661112 | #5541. Substring Sort | temmie | WA | 2310ms | 25508kb | C++17 | 4.2kb | 2024-10-20 14:45:15 | 2024-10-20 14:45:16 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define fastio ios::sync_with_stdio(0), cin.tie(0);
using namespace std;
#ifdef LOCAL
#define cout cout << "\033[0;32m"
#define cerr cerr << "\033[0;31m"
#define endl "\n" << "\033[0m"
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt")
#define endl "\n"
#endif
const int MAX_N = 5e5+10;
const int INF = 2e18;
mt19937 seed(time(NULL));
inline int rnd(int l, int r){ return uniform_int_distribution(l, r)(seed); }
int A = rnd(100, 1e9+7);
const int B = 1e13+7;
// declare
int n, q;
vector<string> v;
int l, r;
vector<int> P;
struct Treap{
Treap *l, *r;
int pri, val, sz, sum;
char c;
Treap(int _val, int _c){
l = nullptr;
r = nullptr;
pri = rnd(1, 1e9);
val = sum = _val;
sz = 1;
c = _c;
}
} *tr[3], *tr_l[3], *tr_r[3], *tr_mid[3], *tr_tmp, *tr_tmpx[4];
int size(Treap *a){
return a ? a->sz : 0;
}
int sum(Treap *a){
return a ? a->sum : 0;
}
void pull(Treap *t){
t->sz = size(t->l)+size(t->r)+1;
t->sum = (sum(t->l) + sum(t->r) + t->val);
}
Treap *merg(Treap *a, Treap *b){
if (!a || !b) return a ? a : b;
if (a->pri > b->pri){
a->r = merg(a->r, b);
pull(a);
return a;
}else{
b->l = merg(a, b->l);
pull(b);
return b;
}
}
void split(Treap *&t, int k, Treap *&a, Treap *&b){
if (!t) a = b = nullptr;
else if (size(t->l)+1<=k){
a = t;
split(t->r, k-size(t->l)-1, a->r, b);
pull(a);
}else{
b = t;
split(t->l, k, a, b->l);
pull(b);
}
}
ostream & operator << (ostream &os, Treap *t){
if (t==0) return os;
os << t->l;
os << t->c;
os << t->r;
return os;
}
void print(Treap *t){
if (t->l!=0) print(t->l);
cout << t->c;
if (t->r!=0) print(t->r);
}
char treap_get(Treap *&t, int k){
if (size(t->l) + 1 < k) {
return treap_get(t->r, k-size(t->l)-1);
} else if (k == size(t->l)+1) {
return t->c;
} else {
return treap_get(t->l, k);
}
}
int pref(Treap *&t, int k){
if (size(t->l) + 1 < k) {
return pref(t->r, k-size(t->l)-1)+sum(t->l);
} else if (k == size(t->l)+1) {
return t->val;
} else {
return pref(t->l, k);
}
}
inline bool check_swap(int i, int j) {
int ll = 1, rr = tr_mid[i]->sz+1, ans = 0;
while (ll < rr) {
int mid = ll+(rr-ll)/2;
if (pref(tr_mid[i], mid)==pref(tr_mid[j], mid)) {
ans = mid;
ll = mid+1;
}else{
rr = mid;
}
}
if (ans==tr_mid[i]->sz){
return false;
}else{
return treap_get(tr_mid[i], ans+1)>treap_get(tr_mid[j], ans+1);
}
}
inline void srt(int i, int j) {
if (check_swap(i, j)) {
swap(tr_mid[i], tr_mid[j]);
}
}
inline char readchar(){
static char buffer[BUFSIZ], *now = buffer+BUFSIZ, *end = buffer+BUFSIZ;
if (now==end){
if (end<buffer+BUFSIZ)
return EOF;
end = (buffer+fread(buffer, 1, BUFSIZ, stdin));
now = buffer;
}
return *now++;
}
inline int nextint(){
int x = 0, c = readchar(), neg = false;
while (('0'>c || c>'9') && c!='-' && c!=EOF) c = readchar();
if (c=='-') neg = true, c = readchar();
while ('0'<=c && c<='9') x = (x<<3)+(x<<1)+(c^'0'), c = readchar();
if (neg) x = -x;
return x;
}
inline void nextstring(string &s){
char c = readchar();
while (('a'>c || c>'z') && c!=EOF) c = readchar();
while ('a'<=c && c<='z') s+=c, c = readchar();
}
void solve1(){
// input
n = nextint();
q = nextint();
v.resize(3);
for (int i=0 ; i<3 ; i++){
nextstring(v[i]);
}
// process
P.resize(n);
P[0] = 1;
for (int i=1 ; i<n ; i++){
P[i] = P[i-1]*A%B;
}
vector<vector<int>> p(3, vector<int>(n));
for (int i = 0 ; i < 3 ; i++) {
for (int j = 0 ; j < n ; j++) {
p[i][j] = P[j] * v[i][j] % B;
tr[i] = merg(tr[i], new Treap(p[i][j], v[i][j]));
}
}
// queries
for (int cases = 0 ; cases < q; ++cases){
l = nextint();
r = nextint();
l--, r--;
for (int j = 0; j < 3; ++j) {
split(tr[j], l, tr_l[j], tr_tmp);
split(tr_tmp, r - l + 1, tr_mid[j], tr_r[j]);
}
srt(0, 1);
srt(0, 2);
srt(1, 2);
for (int j = 0; j < 3; ++j) {
tr_tmp = merg(tr_l[j], tr_mid[j]);
tr[j] = merg(tr_tmp, tr_r[j]);
}
}
// output
print(tr[0]);
cout << endl;
print(tr[1]);
cout << endl;
print(tr[2]);
cout << endl;
return;
}
signed main(){
fastio;
int t = 1;
while (t--){
solve1();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3656kb
input:
5 2 icpca siaja karta 2 4 1 5
output:
iarta kiaja scpca
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
6 6 aabbcc bcacab cbcaba 1 1 2 2 3 3 4 4 5 5 6 6
output:
aaaaaa bbbbbb cccccc
result:
ok 3 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
3 1 aba aab aac 1 3
output:
aab aac aba
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
1 1 z y x 1 1
output:
x y z
result:
ok 3 lines
Test #5:
score: 0
Accepted
time: 2310ms
memory: 25332kb
input:
100000 100000 llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...
output:
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...
result:
ok 3 lines
Test #6:
score: -100
Wrong Answer
time: 2215ms
memory: 25508kb
input:
100000 100000 ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttottttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt...
output:
ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttotttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttqtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttkttttttttttttttttttttttttttttttttttttttttttttttt...
result:
wrong answer 1st lines differ - expected: 'tttttttttttttttttttttttttttttt...ttttttttttttttttttttttttttttttt', found: 'tttttttttttttttttttttttttttttt...ttttttttttttttttttttttttttttttt'