比赛 EYOI与SBOI开学欢乐赛14th 评测结果 AWWWTEEEEE
题目名称 逆元数列 最终得分 10
用户昵称 lihaoze 运行时间 2.341 s
代码语言 C++ 内存使用 6.42 MiB
提交时间 2022-10-24 20:08:24
显示代码纯文本
#include "bits/stdc++.h"

using i64 = long long;

struct Seg {
    int l, r;
    i64 sum;
};

const int N = 1e5 + 10;
int n, m;
i64 a[N];
Seg tree[N];
#define l(x) tree[x].l
#define r(x) tree[x].r
#define sum(x) tree[x].sum

i64 P;

i64 power(i64 a, i64 b, i64 p) {
    i64 ret = 1;
    for (; b; b /= 2, a = a * a % p) {
        if (b % 2) ret = ret * a % p;
    }
    return ret % p;
}

i64 inv(i64 a, i64 p) {
    return power(a, p - 2, p);
}

void build(int p, int l, int r) {
    l(p) = l, r(p) = r;
    if (l == r) {
        return sum(p) = a[l], void();
    }
    int mid = l + r >> 1;
    build(p * 2, l, mid),
    build(p * 2 + 1, mid + 1, r);
    sum(p) = sum(p * 2) + sum(p * 2 + 1); 
}

void change(int p, int l, int r) {
    if (l(p) == r(p)) {
        sum(p) = inv(sum(p), P);
        return;
    }
    int mid = (l(p) + r(p)) / 2;
    if (l <= mid) change(p * 2, l, r);
    if (r > mid) change(p * 2 + 1, mid + 1, r);
    sum(p) = sum(p * 2) + sum(p * 2 + 1);
}

i64 ask(int p, int l, int r) {
    if (l <= l(p) && r >= r(p)) return sum(p);
    int mid = (l(p) + r(p)) / 2;
    i64 res = 0;
    if (l <= mid) res += ask(p * 2, l, r);
    if (r > mid) res += ask(p * 2 + 1, l, r);
    return res;
}

int main() {
    freopen("invarray.in", "r", stdin); 
    freopen("invarray.out", "w", stdout);
    std::cin >> n >> m >> P;
    for (int i = 1; i <= n; ++ i)
        std::cin >> a[i];
    build(1, 1, n);
    for (int i = 1; i <= m; ++ i) {
        int op, l, r;
        std::cin >> op >> l >> r;
        if (op == 1) {
            change(1, l, r);
        } else {
            std::cout << ask(1, l, r) << '\n';
        }
    }
    return 0;
}