codeforces 920F SUM and REPLACE

Posted erro

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 920F SUM and REPLACE相关的知识,希望对你有一定的参考价值。

codeforces 920F

题目描述

\(d(x)\)为正整数\(x\)的因子的数量。给定一个长度为\(n\)的序列\(a\),有两种操作:

  1. \([l,r]\)的数i变为\(d(i)\)
  2. \(\sum_{i=l}^{r}{a_i}\)

思路

首先可以用线段树来维护这个序列,每次暴力修改。因为\(d(1)=1,d(2)=2\),因此当一个区间的最大值小于2时就不需要修改。又因为每次修改只会让值减少,而每个数最多会被修改\(O(log_2n)\)次,因此能够保证复杂度

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXN = 300000 + 10;
const int MAXA = 1000000 + 10;
const char ENDLINE = \n;

int d[MAXA], num[MAXA];
int prime[MAXA / 10], primeCnt;

void sieve()
{
    const int MX = 1000000;
    static bool vis[MAXA];
    d[1] = 1;
    for (int i = 2; i <= MX; ++i) {
        if (!vis[i]) {
            prime[++primeCnt] = i;
            d[i] = 2;
            num[i] = 1;
        }
        for (int j = 1; j <= primeCnt && prime[j] * i <= MX; ++j) {
            vis[prime[j] * i] = true;
            if (i % prime[j] == 0) {
                num[i * prime[j]] = num[i] + 1;
                d[i * prime[j]] = d[i] / (num[i] + 1) * (num[i] + 2);
                break;
            }
            num[i * prime[j]] = 1;
            d[i * prime[j]] = d[i] * 2;
        }
    }

}

struct SegmentTree {
    int L[MAXN << 2], R[MAXN << 2];
    int mx[MAXN << 2];
    ll sum[MAXN << 2];
    void pushup(int o) { mx[o] = max(mx[o<<1], mx[o<<1|1]), sum[o] = sum[o<<1] + sum[o<<1|1]; }
    void update(int l, int r, int o=1)
    {
        if (mx[o] <= 2) 
            return;
        if (L[o] == R[o]) {
            sum[o] = mx[o] = d[sum[o]];
            return;
        }
        int mid = (L[o] + R[o]) >> 1;
        if (l <= mid)
            update(l, r, o<<1);
        if (mid < r)
            update(l, r, o<<1|1);
        pushup(o);
    }
    ll query(int l, int r, int o=1)
    {
        if (L[o] == l && R[o] == r)
            return sum[o];
        int mid = (L[o] + R[o]) >> 1;
        if (r <= mid)
            return query(l, r, o<<1);
        if (mid < l)
            return query(l, r, o<<1|1);
        return query(l, mid, o<<1) + query(mid+1, r, o<<1|1);
    }
    void build(int l, int r, int a[], int o=1)
    {
        L[o] = l, R[o] = r;
        if (l == r) {
            sum[o] = mx[o] = a[l];
            return;
        }
        int mid = (l + r) >> 1;
        build(l, mid, a, o<<1);
        build(mid+1, r, a, o<<1|1);
        pushup(o);
    }
} segt;

int main()
{
    int n, m;
    static int a[MAXN];
    ios::sync_with_stdio(0);
    sieve();
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    segt.build(1, n, a);
    for (int i = 1; i <= m; ++i) {
        int opt, l, r;
        cin >> opt >> l >> r;
        if (opt == 1) {
            segt.update(l, r);
        } else {
            cout << segt.query(l, r) << ENDLINE;
        }
    }
}

以上是关于codeforces 920F SUM and REPLACE的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 920F SUM and REPLACE (线段树)

codeforces CF920F SUM and REPLACE 线段树 线性筛约数

Codefroces 920F SUM and REPLACE???????????????

题解 CF920F SUM and REPLACE

CF920F SUM and REPLACE 题解

Educational Codeforces Round 37 (Rated for Div. 2)F. SUM and REPLACE+线段树