QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864445#9250. Max GCDthangthangWA 1276ms283384kbC++202.4kb2025-01-20 16:43:002025-01-20 16:43:03

Judging History

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

  • [2025-01-20 16:43:03]
  • 评测
  • 测评结果:WA
  • 用时:1276ms
  • 内存:283384kb
  • [2025-01-20 16:43:00]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ii = pair <int, int>;

const int N = 1e6 + 1;
const int S = 400;

int n, q, Max[N];
vector <int> dv[N], pos[N];
int ans[N], lqr[N], rqr[N], ord[N];
int id[N], lbl[S * 5], rbl[S * 5], nbl, used[N];
ii f[N], g[S * 5];

void rebuild(int bl){
    for (int i = lbl[bl]; i <= rbl[bl]; ++ i){
        if (i == lbl[bl]) f[i] = {0, 0};
        else f[i] = f[i - 1];
        int v = 0;
        if (!used[i]) v = rqr[ord[i]];
        f[i] = max(f[i], {v, i});
    }
    for (int sf = bl; sf <= nbl; ++ sf)
        g[sf] = max(g[sf - 1], f[rbl[sf]]);
}

ii get(int p){
    p = Max[p];
    return max(g[id[p] - 1], f[p]);
}

void process(){
    cin >> n >> q;
    for (int i = 1; i < N; ++ i){
        for (int j = i; j < N; j += i)
            dv[j].push_back(i);
    }

    for (int i = 1; i <= n; ++ i){
        int a; cin >> a;
        for (int u : dv[a])
            pos[u].push_back(i);
    }

    for (int i = 1; i <= q; ++ i){
        cin >> lqr[i] >> rqr[i];
        ord[i] = i;
    }

    sort(ord + 1, ord + q + 1, [&](int u, int v){
        return lqr[u] < lqr[v];
    });

    int ptr = 0;
    for (int i = 1; i <= n; ++ i){
        while (ptr < q && i >= lqr[ord[ptr + 1]]) ptr ++;
        Max[i] = ptr;
    }

    nbl = q / S + 1;
    for (int i = 1; i <= q; ++ i){
        id[i] = i / S + 1;
        if (!lbl[id[i]]) lbl[id[i]] = i;
        rbl[id[i]] = i;
    }
    for (int bl = 1; bl <= nbl; ++ bl) rebuild(bl);


    for (int gcd = N - 1; gcd >= 1; -- gcd){
        int num = pos[gcd].size();
        if (num < 3) continue;
        int ptr = 0;
        for (int i = 0; i < num - 2; ++ i){
            while (ptr < num && pos[gcd][i + 1] - pos[gcd][i] > pos[gcd][ptr] - pos[gcd][i + 1]) ptr ++;
            if (ptr == num) break;
            int sl = pos[gcd][i];
            int sr = pos[gcd][ptr];
            while (true){
                ii tmp = get(sl);
                if (tmp.first < sr) break;
                assert(used[tmp.second] == 0);
                ans[ord[tmp.second]] = gcd;
                used[tmp.second] = 1;
                rebuild(id[tmp.second]);
            }
        }
    }

    for (int i = 1; i <= q; ++ i) cout << ans[i] << "\n";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    process();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 531ms
memory: 128092kb

input:

8 8
8 24 4 6 6 7 3 3
1 5
2 6
3 7
5 8
4 8
1 3
2 5
3 8

output:

4
2
3
1
3
4
2
3

result:

ok 8 tokens

Test #2:

score: -100
Wrong Answer
time: 1276ms
memory: 283384kb

input:

150000 100000
982800 554400 665280 982800 997920 720720 786240 831600 997920 982800 786240 982800 942480 831600 887040 665280 831600 960960 786240 982800 786240 942480 665280 831600 942480 665280 982800 960960 960960 997920 720720 960960 960960 665280 982800 665280 982800 942480 786240 997920 554400...

output:

997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920
997920...

result:

wrong answer 5108th words differ - expected: '997920', found: '982800'