QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#744225#9543. Good PartitionsLonelyperML 22ms12004kbC++202.6kb2024-11-13 21:13:362024-11-13 21:13:37

Judging History

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

  • [2024-11-13 21:13:37]
  • 评测
  • 测评结果:ML
  • 用时:22ms
  • 内存:12004kb
  • [2024-11-13 21:13:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, M = 1e6 + 10;

int n, q;
int g[N], a[N], tree[N << 2];
int h[M];

LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}

void init2(){
    for(int i = 1; i < M; i ++){
        for(int j = i; j < M; j += i){
            h[j] ++;
        }
    }
}

void init(){
    for(int i = 0; i <= n ; i ++) g[i] = 0;
    for(int i = 0; i <= 4 * n; i ++) tree[i] = 0;
}

void push_up(int p) {
    tree[p] = gcd(tree[p << 1], tree[p << 1 | 1]);
}

void build(int p, int pl, int pr) {
    if (pl == pr) {
        tree[p] = g[pl];
        return;
    }
    int mid = (pl + pr) >> 1;
    build(p << 1, pl, mid);
    build(p << 1 | 1, mid + 1, pr);
    push_up(p);
}

int query(int l, int r, int p, int pl, int pr) {
    if (l <= pl && pr <= r) return tree[p];
    int mid = (pl + pr) >> 1;
    LL res = 0;
    if (l <= mid) res = gcd(res, query(l, r, p << 1, pl, mid));
    if (r > mid) res = gcd(res, query(l, r, p << 1 | 1, mid + 1, pr));
    return res;
}

void update(int pos) {
    if (pos > 1) {
        if (a[pos - 1] > a[pos]) g[pos - 1] = pos - 1;
        else g[pos - 1] = 0;
    }
    if (pos < n) {
        if (a[pos] > a[pos + 1]) g[pos] = pos;
        else g[pos] = 0;
    }
}

void update_tree(int pos, int p, int pl, int pr) {
    if (pl == pr) {
        tree[p] = g[pl];
        return;
    }
    int mid = (pl + pr) >> 1;
    if (pos <= mid) update_tree(pos, p << 1, pl, mid);
    else update_tree(pos, p << 1 | 1, mid + 1, pr);
    push_up(p);
}

void solve() {
    cin >> n >> q;
    init();
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i + 1]) g[i] = i;
        else g[i] = 0;
    }

    build(1, 1, n - 1);
    // cout << tree[1] << ' ';
    int x = query(1, n - 1, 1, 1, n - 1);
    cout << h[x] << endl;
    // cout << h[1] << ' ' << h[2] << ' ' << h[4] << endl;
    while (q--) {
        int pos, x;
        cin >> pos >> x;
        a[pos] = x;
        if (pos > 1) {
            update(pos - 1);
            update_tree(pos - 1, 1, 1, n - 1);
        }
        if (pos < n) {
            update(pos);
            update_tree(pos, 1, 1, n - 1);
        }
        // for(int i = 1; i <= n; i ++) cout << g[i] << ' ';
        // cout << endl;
        // cout << tree[1] << ' ';
        x = query(1, n - 1, 1, 1, n - 1);
        cout << h[x] << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    init2();
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 22ms
memory: 12004kb

input:

1
5 2
4 3 2 6 1
2 5
3 5

output:

1
2
3

result:

ok 3 lines

Test #2:

score: -100
Memory Limit Exceeded

input:

1
1 1
2000000000
1 1999999999

output:


result: