QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#259395#7748. Karshilov's Matching Problem IIucup-team902WA 50ms14204kbC++203.0kb2023-11-20 21:32:022023-11-20 21:32:02

Judging History

你现在查看的是最新测评结果

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-20 21:32:02]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:14204kb
  • [2023-11-20 21:32:02]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define gc c=getchar()
#define r(x) read(x)
#define ll long long

template<typename T>
inline void read(T &x){
    x=0;T k=1;char gc;
    while(!isdigit(c)){if(c=='-')k=-1;gc;}
    while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}

const int N = 1.5e5 + 7;
const int p = 1e9 + 7;
const int base = 137;

inline int sub(int a, int b){
    return a - b < 0 ? a - b + p : a - b;
}

int tr[N << 2];
int len[N];
#define ls (rt << 1)
#define rs (ls |  1)

void build(int rt, int l, int r){
    if(l == r){
        tr[rt] = l + len[l] - 1;
        // cerr << l << " " << tr[rt] << endl;
        return ;
    }
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
    tr[rt] = max(tr[ls], tr[rs]);
}

int query(int rt, int l, int r, int x, int y, int v){
    if(tr[rt] <= v) return -1;
    if(l == r) return l;
    int mid = (l + r) >> 1;
    if(x <= l && r <= y){
        if(tr[ls] >= v) return query(ls, l, mid, x, y, v);
        else return query(rs, mid + 1, r, x, y, v);
    }
    if(y <= mid) return query(ls, l, mid, x, y, v);
    if(x > mid) return query(rs, mid + 1, r, x, y, v);
    int t = query(ls, l, mid, x, y, v);
    return t == -1 ? query(rs, mid + 1, r, x, y, v) : t;
}

ll f[N];
ll g[N];
ll sumf[N];
ll sumg[N];

char s[N];
char t[N];
int w[N];
int Next[N];

int hashs[N];
int hasht[N];
int pw[N];

int main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    int n, m; r(n), r(m);
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    for(int i = 1; i <= n; ++i){
        r(w[i]);
    }
    pw[0] = 1;
    for(int i = 1; i <= n; ++i){
        hashs[i] = ((ll)hashs[i - 1] * base + s[i]) % p;
        hasht[i] = ((ll)hasht[i - 1] * base + t[i]) % p;
        pw[i] = (ll)pw[i - 1] * base % p;
    }
    for(int i = 1; i <= n; ++i){
        int l = i, r = n, ans = i - 1;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(hashs[mid - i + 1] == sub(hasht[mid], (ll)hasht[i - 1] * pw[mid - i + 1] % p)){
                l = mid + 1;
                ans = mid;
            }
            else{
                r = mid - 1;
            }
        }
        len[i] = ans - i + 1;
    }
    build(1, 1, n);
    for(int i = 2, j = 0; i <= n; ++i){
        while(s[j + 1] != s[i] && j) j = Next[j];
        if(s[j + 1] == s[i]) ++j;
        Next[i] = j;
    }
    for(int i = 1; i <= n; ++i){
        f[i] = f[i - 1] + w[i];
        g[i] = w[i] + g[Next[i]];
    }
    for(int i = 1; i <= n; ++i){
        sumf[i] = sumf[i - 1] + f[len[i]];
        sumg[i] = sumg[i - 1] + g[i];
    }
    for(int i = 1; i <= m; ++i){
        int l, r; r(l), r(r);
        int pos = query(1, 1, n, l, r, r);
        if(pos == -1){
            printf("%lld\n", sumf[r] - sumf[l - 1]);
        }
        else{
            printf("%lld\n", sumg[r - pos + 1] + sumf[pos - 1] - sumf[l - 1]);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9904kb

input:

8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8

output:

1
3
3
16
38

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 7688kb

input:

15 4
heheheheehhejie
heheheheheheheh
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9
2 3
4 8
2 6
1 15

output:

3
13
13
174

result:

ok 4 lines

Test #3:

score: -100
Wrong Answer
time: 50ms
memory: 14204kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

108147037823514
221878585246974
455345186563306
440394952018130
148235792557910
88695249853257
351159618462315
58850354020997
65870321895331
270158033672354
197732558443069
298193894693053
239511187032650
28141545900698
408621726753017
268053430937402
32523094694475
104840668657244
44074926058619
78...

result:

wrong answer 3rd lines differ - expected: '455339807727642', found: '455345186563306'