QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#594331#9250. Max GCDgaofengWA 2469ms572724kbC++233.9kb2024-09-27 21:31:272024-09-27 21:31:27

Judging History

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

  • [2024-09-27 21:31:27]
  • 评测
  • 测评结果:WA
  • 用时:2469ms
  • 内存:572724kb
  • [2024-09-27 21:31:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF  = 0x3f3f3f3f;
const ll INFll  = 0x3f3f3f3f3f3f3f3f;
#define endl "\n" 
#define x first
#define y second

vector<vector<int>>adj(N);

int a[N];

struct Node{
    int l, r, w;
}query[N];
int ans[N];

struct gfseg { // 区间最值
    int a[N], _n;
    struct Node {
        int l, r;
        int max, min;
    }tr[N];

    void pushup(int u) {
        tr[u].max = max(tr[u << 1].max, tr[u << 1 | 1].max);
    }

    void build(int u, int l, int r) {
        // cout << u << " " << l << " " << r << endl;
        if(l == r) {
            tr[u] = {l, r, a[l], a[r]};
            // cout << l << " " << a[l] << endl;
            return;
        }
        int mid = l + r >> 1;
        tr[u] = {l, r, -INF, INF};
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }

    void init(vector<int> _a) {
        _n = _a.size();
        for(int i = 1; i <= _n; i ++) {
            a[i] = _a[i - 1];
        } 
        build(1, 1, _n);
    }

    void modify(int u, int x, int y) {
        if(tr[u].l == tr[u].r && tr[u].l == x) {
            tr[u].max = max(tr[u].max, y);
            return;
        }
        int mid = tr[u].l + tr[u].r >> 1;
        if(x <= mid) modify(u << 1, x, y);
        if(x >  mid) modify(u << 1 | 1, x, y);
        pushup(u);
    }


    int querymax(int u, int l, int r) {
        if(l <= tr[u].l && tr[u].r <= r) {
            return tr[u].max;
        }

        int res = -INF;
        int mid = tr[u].l + tr[u].r >> 1;
        if(l <= mid) res = max(querymax(u << 1, l,  r), res);
        if(r >  mid) res = max(querymax(u << 1 | 1, l,  r), res);
        return res;
    }

    void debug() {
        for(int i = 1; i <= _n; i ++)
        cout << querymax(1, i, i) << " "; cout <<  endl;
    }
}tr;



void solve()
{
    
    int n, q; cin >> n >> q;
    for(int i = 1; i <= n; i ++) {
        cin >> a[i];
        // a[i] = rand();
        int x = a[i];
        for(int j = 1; j * j <= x; j ++) {
            if(x % j == 0) {
                adj[j].push_back(i);
                if(j * j != x) 
                    adj[x/j].push_back(i);
            }
        }
    }
    vector<Node> no;
    for(int i = 1; i < N; i ++) {
        // for(auto id : adj[i]) cout << id << " "; cout << endl;
        for(int j = 0; j + 2 < adj[i].size(); j ++) {
            if(adj[i][j + 1] - adj[i][j] <= adj[i][j + 2] - adj[i][j + 1]) 
                no.push_back({adj[i][j], adj[i][j + 2], i});
        }
    }

    // cout << no.size() << endl;
    // for(auto [l, r, w]:no) cout << l << " " << r << " " << w << endl;

    vector<int>T; for(int i = 1; i <= n + 10; i ++) T.push_back(0); tr.init(T);

    for(int i = 0; i < q; i ++) {
        cin >> query[i].l >> query[i].r; query[i].w = i;
    }

    sort(query , query + q, [&](Node a, Node b){
        return a.l > b.l;
    });

    sort(no.begin(), no.end(), [&](Node a, Node b){
        return a.l > b.l;
    });

    int i = 0, j = 0;
 

    for(int L = n; L >= 0; L --) {
        // cout << L  << ":\n";
        while(i < no.size() && no[i].l >= L) {
            // cout << no[i].l << " " << no[i].r << " " << no[i].w << endl;
            tr.modify(1, no[i].r, no[i].w);
            i ++;
        }

        while(j < q && query[j].l >= L) {
            ans[query[j].w] = tr.querymax(1,1,query[j].r);
            j ++;
        }
    }
    // tr.debug();
    // cout << i << " " << j << endl;

    for(int i = 0; i < q; i ++) cout << ans[i] << endl;


}


signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cout << setprecision(11) << fixed;
    int t;t=1;
    //cin>>t;
    for(int i=1;i<=t;i++){
        //printf("Case %d: ",i);
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 34672kb

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: 0
Accepted
time: 2469ms
memory: 572724kb

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:

ok 100000 tokens

Test #3:

score: -100
Wrong Answer
time: 498ms
memory: 76300kb

input:

150000 100000
716844 340255 836453 422971 389959 56166 837543 724949 336855 860372 579302 812222 849774 845846 999555 136871 624002 100905 529143 187215 582397 95964 363772 534762 258007 132867 753342 300681 770692 654005 397230 267857 21953 347450 248776 397101 768172 868404 612257 885884 270063 45...

output:

962054
997805
890994
997805
302526
997805
997805
977481
977481
962054
997805
977481
187969
962054
389779
753782
385422
267290
299894
219398
73821
400069
997805
722583
58309
962054
977481
997805
963049
997805
413562
994191
997805
426490
997805
962054
962054
753782
484075
997805
994191
299879
761650
7...

result:

wrong answer 3037th words differ - expected: '11283', found: '7183'