QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#637720#9250. Max GCDucup-team4479#TL 0ms11736kbC++232.5kb2024-10-13 13:54:582024-10-13 13:54:59

Judging History

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

  • [2024-10-13 13:54:59]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:11736kb
  • [2024-10-13 13:54:58]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=150005;
const int M=1000005;
int n,q;
int a[N];
int lst[M];
map<int,int>nxt[N];
vector<pair<int,int>>seg[N];
vector<pair<int,int>>query[N];
int ans[N];
struct Segment_Tree
{
    #define ls i*2
    #define rs i*2+1
    struct Node
    {
        int l,r;
        int mx;
    }tree[N*4];
    void push_up(int i)
    {
        tree[i].mx=max(tree[ls].mx,tree[rs].mx);
        return;
    }
    void build(int i,int l,int r)
    {
        tree[i].l=l,tree[i].r=r;
        if(l==r)
        {
            tree[i].mx=1;
            return;
        }
        int mid=(l+r)>>1;
        build(ls,l,mid);
        build(rs,mid+1,r);
        push_up(i);
        return;
    }
    void modify(int i,int u,int v)
    {
        if(tree[i].l==tree[i].r)
        {
            tree[i].mx=max(tree[i].mx,v);
            return;
        }
        if(u<=tree[ls].r) modify(ls,u,v);
        else modify(rs,u,v);
        push_up(i);
        return;
    }
    int query(int i,int ql,int qr)
    {
        if(ql<=tree[i].l&&tree[i].r<=qr) return tree[i].mx;
        int res=0;
        if(ql<=tree[ls].r) res=max(res,query(ls,ql,qr));
        if(qr>=tree[rs].l) res=max(res,query(rs,ql,qr));
        return res;
    }
}T;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    cin>>n>>q;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=n;i>=1;i--)
    {
        nxt[i]=nxt[i+1];
        for(int j=1;j*j<=a[i];j++)
            if(a[i]%j==0) nxt[i][j]=nxt[i][a[i]/j]=i;
    }
    for(int j=1;j<=n;j++)
    {
        auto calc=[&](int g)
        {
            if(g==1) return;
            int i=lst[g];
            if(i<1) return;
            if(!nxt[j+j-i].count(g)) return;
            int k=nxt[j+j-i][g];
            seg[k].emplace_back(i,g);
            return;
        };
        for(int g=1;g*g<=a[j];g++)
            if(a[j]%g==0)
            {
                calc(g);
                if(g*g!=a[j]) calc(a[j]/g);
            }
        for(int g=1;g*g<=a[j];g++)
            if(a[j]%g==0) lst[g]=lst[a[j]/g]=j;
    }
    for(int i=1;i<=q;i++)
    {
        int l,r;
        cin>>l>>r;
        query[r].emplace_back(l,i);
    }
    T.build(1,1,n);
    for(int r=1;r<=n;r++)
    {
        for(auto &[i,g]:seg[r])
            T.modify(1,i,g);
        for(auto &[l,id]:query[r])
            ans[id]=T.query(1,l,r);
    }
    for(int i=1;i<=q;i++)
        cout<<ans[i]<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

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: