QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#692058#9250. Max GCDYipChipWA 2ms5648kbC++232.4kb2024-10-31 13:43:132024-10-31 13:43:14

Judging History

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

  • [2024-10-31 13:43:14]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5648kb
  • [2024-10-31 13:43:13]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, q, len;
int a[N], w[N], sum[N];
vector<int> factor[N];
struct Node {
    int op, l, r, d, id;
};
vector<Node> segment;

bool cmp1(Node x, Node y)
{
    if (x.l == y.l)
    {
        if (x.op == y.op) return x.id < y.id;
        return x.op < y.op;
    }
    return x.l < y.l;
}

bool cmp2(Node x, Node y)
{
    if (x.op == y.op) return x.id < y.id;
    return x.op < y.op;
}

int get(int x)
{
    return (x - 1) / len;
}

int query(int l, int r)
{
    int res = 0;
    if (get(l) == get(r))
        for (int i = l; i <= r; i ++ )
            res = max(res, w[i]);
    else
    {
        int i = l, j = r;
        while (get(i) == get(l)) res = max(res, w[i]), i ++ ;
        while (get(j) == get(r)) res = max(res, w[j]), j -- ;
        for (i = get(i); i <= get(j); i ++ ) res = max(res, sum[i]);
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    cin >> n >> q;
    int maxx = 0;
    for (int i = 1; i <= n; i ++ ) cin >> a[i], maxx = max(maxx, a[i]);
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j * j <= a[i]; j ++ )
            if (a[i] % j == 0)
                if (j * j != a[i]) factor[j].push_back(i), factor[a[i] / j].push_back(i);
                else factor[j].push_back(i);
    for (int i = 1; i <= q; i ++ )
    {
        int l, r;
        cin >> l >> r;
        segment.push_back({0, l, r, 0, i});
    }
    for (int i = 1; i <= maxx; i ++ )
    {
        int last = factor[i].size();
        for (int l = last - 3, r = last - 1; l >= 0; r -- )
        {
            while (l >= 0 && factor[i][r] - factor[i][l + 1] < factor[i][l + 1] - factor[i][l]) l -- ;
            if (l < 0) break;
            segment.push_back({1, factor[i][l], factor[i][r], i, 0});
        }
    }
    len = sqrt(n);
    sort(segment.begin(), segment.end(), cmp1);
    for (int i = segment.size() - 1; i >= 0; i -- )
    {
        if (segment[i].op)
        {
            sum[get(segment[i].r)] = max(sum[get(segment[i].r)], segment[i].d);
            w[segment[i].r] = max(w[segment[i].r], segment[i].d);
        }
        else segment[i].d = query(segment[i].l, segment[i].r);
    }
    for (int i = 0; i < segment.size(); i ++ )
        if (!segment[i].op)
            cout << segment[i].d << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 5648kb

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
4
2
2
3
3
3
1

result:

wrong answer 2nd words differ - expected: '2', found: '4'