QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#607647#9250. Max GCDpropane#TL 4ms26664kbC++203.5kb2024-10-03 15:40:562024-10-03 15:40:57

Judging History

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

  • [2024-10-03 15:40:57]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:26664kb
  • [2024-10-03 15:40:56]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
using namespace std;
using LL = long long;

#include<iostream>
#include<cstring>
#include<vector>
#include<numeric>
#include<functional>
using namespace std;

struct Info {
    int mx = 0;
};

Info operator+(const Info &a, const Info &b){
    return {max(a.mx, b.mx)};
}

template<class Info>
struct SegmentTree{
    int n;
    vector<Info> info;

    SegmentTree() {}

    SegmentTree(int n, Info _init = Info()){
        init(vector<Info>(n, _init));
    }

    SegmentTree(const vector<Info> &_init){
        init(_init);
    }

    void init(const vector<Info> &_init){
        n = (int)_init.size();
        info.assign((n << 2) + 1, Info());
        function<void(int, int, int)> build = [&](int p, int l, int r){
            if (l == r){
                info[p] = _init[l - 1];
                return;
            }
            int m = (l + r) / 2;
            build(2 * p, l, m);
            build(2 * p + 1, m + 1, r);
            pull(p);
        };
        build(1, 1, n);
    }

    void pull(int p){
        info[p] = info[2 * p] + info[2 * p + 1];
    }

    void modify(int p, int l, int r, int x, const Info &v){
        if (l == r){
            info[p] = info[p] + v;
            return;
        }
        int m = (l + r) / 2;
        if (x <= m){
            modify(2 * p, l, m, x, v);
        } 
        else{
            modify(2 * p + 1, m + 1, r, x, v);
        }
        pull(p);
    }

    void modify(int p, const Info &v){
        modify(1, 1, n, p, v);
    }

    Info query(int p, int l, int r, int x, int y){
        if (l > y || r < x){
            return Info();
        }
        if (l >= x && r <= y){
            return info[p];
        }
        int m = (l + r) / 2;
        return query(2 * p, l, m, x, y) + query(2 * p + 1, m + 1, r, x, y);
    }

    Info query(int l, int r){
        return query(1, 1, n, l, r);
    }
};

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    const int N = 1e6;
    int n, m;
    cin >> n >> m;
    vector<vector<int> > pos(N + 1);
    for(int i = 1; i <= n; i++){
        int t;
        cin >> t;
        for(int j = 1; j * j <= t; j++){
            if (t % j == 0){
                pos[j].push_back(i);
                if (j * j != t){
                    pos[t / j].push_back(i);
                }
            }
        }
    }
    vector<vector<pair<int, int> > > seg(n + 1);
    for(int i = 1; i <= N; i++){
        if (!pos[i].empty()){
            for(int j = 0; j + 1 < pos[i].size(); j++){
                int a = pos[i][j], b = pos[i][j + 1];
                int k = lower_bound(pos[i].begin(), pos[i].end(), 2 * b - a) - pos[i].begin();
                if (k != pos[i].size()) seg[a].push_back({pos[i][k], i});
            }
        }
    }
    vector<vector<pair<int, int> > > query(n + 1);
    vector<int> ans(m);
    for(int i = 0; i < m; i++){
        int l, r;
        cin >> l >> r;
        query[l].push_back({r, i});
    }
    SegmentTree<Info> st(n);
    for(int i = n; i >= 1; i--){
        for(auto [r, val] : seg[i]){
            st.modify(r, {val});
        }
        for(auto [r, id] : query[i]){
            ans[id] = st.query(i, r).mx;
        }
    }
    for(auto x : ans) cout << x << '\n';

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 26664kb

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: