QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#555505#9250. Max GCDucup-team3924TL 319ms112752kbC++233.6kb2024-09-10 01:18:082024-09-10 01:18:09

Judging History

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

  • [2024-09-10 01:18:09]
  • 评测
  • 测评结果:TL
  • 用时:319ms
  • 内存:112752kb
  • [2024-09-10 01:18:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops, avx2")
const int MAX_E = 1000000;
struct Fenwick {
    std::vector<int> T;

    Fenwick(int n): T(1 + n) {}

    static inline int lsb(int x) { return x & (-x); }

    void update(int pos, int val) {
        while (pos < T.size() && val > T[pos]) {
            T[pos] = val;
            pos += lsb(pos);
        }
    }

    int query(int pos) {
        int res = 0;
        while (pos > 0) {
            res = std::max(res, T[pos]);
            pos -= lsb(pos);
        }
        return res;
    }
};

// const int BUFF = 1 << 14;
// char buff[BUFF];
// int curs = BUFF - 1;

// char getchr() {
//     curs++;
//     if (curs == BUFF) {
//         fread(buff, 1, BUFF, stdin);
//         curs = 0;
//     }
//     return buff[curs];
//  }

// int getnr() {
//     int nr = 0;
//     char chr = getchr();
//     while (!isdigit(chr)) chr = getchr();
    
//     while (isdigit(chr)) {
//         nr = nr * 10 + chr - '0';
//         chr = getchr();
//     }

//     return nr;
// }

int cnt[1 + MAX_E] = {0};
vector<int> divs[1 + MAX_E], pos[1 + MAX_E];
int input[150001] = {0};
vector<std::pair<int, int>> to_left[150001];
vector<std::pair<int, int>> queries[150001];
unordered_set<int> in_set[150001];

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
    int n, q;

    cin >> n >> q;

    for (int d = 1; d <= MAX_E; d++) {
        for (int i = d; i <= MAX_E; i += d)
            cnt[i]++;
    }

    for (int i = 0; i <= MAX_E; i++)
        divs[i].reserve(cnt[i]);

    for (int d = 1; d <= MAX_E; d++)
        for (int i = d; i <= MAX_E; i += d)
            divs[i].push_back(d);

    for (int i = 0; i <= MAX_E; i++)
        cnt[i] = 0;

    std::pair<int, int> largest = {0, 0};


    for (int i = 1; i <= n; i++) {
        cin >> input[i];
        for (auto it: divs[input[i]])
            cnt[it]++;
    }

    for (int i = 1; i <= MAX_E; i++)
        pos[i].reserve(cnt[i]);

    for (int i = 1; i <= n; i++) {
        for (auto it: divs[input[i]])
            pos[it].push_back(i);
    }

    for (int i = 1; i <= MAX_E; i++)
        largest = std::max(largest, std::make_pair((int)divs[i].size(), i));

    int top_aux = 0;
    for (int d = MAX_E; d >= 1; d--) {
        int last_p = 1;
        for (int i = 1; i < pos[d].size(); i++) {
            int first = pos[d][i - 1];
            int l = i, r = pos[d].size();
            while (r - l > 1) {
                int mid = (l + r) / 2;
                if (pos[d][i] - pos[d][i - 1] <= pos[d][mid] - pos[d][i])
                    r = mid;
                else
                    l = mid;
            }
            last_p = r;
            if (last_p < pos[d].size()) {
                int last = pos[d][last_p];
                if (!in_set[first].count(last)) {
                    to_left[last].push_back({d, first});
                    in_set[first].insert(last);
                }
            }
        }
    }
    std::vector<int> res(q);
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        queries[r].push_back({i, l});
    }

    Fenwick T(n);

    int last_update = 0;
    for (int i = 1; i <= n; i++) {
        for (auto it: to_left[i]) {
            T.update(n + 1 - it.second, it.first);
        }
        for (auto it: queries[i]) {
            int id = it.first;
            int pos = it.second;

            res[id] = T.query(n + 1 - pos);
        }
    }

    for (int i : res)
        std::cout << i << "\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 319ms
memory: 112752kb

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: