QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#691814#9250. Max GCDYipChipRE 2ms5676kbC++232.3kb2024-10-31 13:06:532024-10-31 13:06:53

Judging History

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

  • [2024-10-31 13:06:53]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:5676kb
  • [2024-10-31 13:06:53]
  • 提交

answer

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

bool cmp1(Node x, Node y)
{
    if (x.l == y.l) 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[ ++ cnt] = {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[ ++ cnt] = {1, factor[i][l], factor[i][r], i, 0};
        }
    }
    len = sqrt(cnt);
    sort(segment + 1, segment + cnt + 1, cmp1);
    for (int i = cnt; i; 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);
    }
    sort(segment + 1, segment + cnt + 1, cmp2);
    for (int i = 1; i <= q; i ++ ) cout << segment[i].d << "\n";
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 5676kb

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
Runtime Error

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:


result: