Educational Codeforces Round 37-F.SUM and REPLACE (segment tree, linear sieve convergence function)

F. SUM and REPLACE

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).

You are given an array a of n integers. You have to process two types of queries:

REPLACE l r — for every replace ai with D(ai);
SUM l r — calculate .
Print the answer for each SUM query.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.

Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).

There is at least one SUM query.

Output
For each SUM query print the answer to it.

Example
inputCopy
7 6
6 4 1 10 3 2 4
2 1 7
2 4 5
1 3 5
2 4 4
1 5 7
2 1 7
outputCopy
30
13
4
22

https://codeforces.com/contest/920/problem/F

Meaning of the questions:

Give you an array comprising a number of n, m and operations

Operation 1: The number of l ~ each R & lt \ (A [I] \) becomes \ (d (a [i] ) \)

Where $ d (x) $ is the number of divisor function.

Operation 2: l ~ r of seeking a [i] and the sum.

Ideas:

$ D (x) $ submultiple number of functions can be pre-treated with a linear screen.

And because \ (d (2) = 2 \) and \ (d (1) = 1 \) operating one pair of a [i] is equal to 1 or 2 had no effect.

Then we can all be in a range of 1 or 2 does not update.

Meanwhile \ (d (x) \) convergence function, 1e6 in the range of no more than five times change will converge to 1 or 2.

So update can solve the violence,

At the same time with a segment tree maintenance can be.

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
//  d(n)表示n的约数个数和
// prime[i]表示第i个质数
//num[i]表示i的最小质因子出现次数
int sshu[maxn];
int N = maxn;
int num[maxn];
int d[maxn];
bool no[maxn];
int tot;
void prepare()
{
    d[1] = 1; num[1] = 1;
    for (int i = 2; i < N; i++) {
        if (!no[i]) {
            sshu[++tot] = i;
            d[i] = 2; num[i] = 1;
        }
        for (int j = 1; j <= tot && sshu[j]*i < N; j++) {
            int v = sshu[j] * i;
            no[v] = 1;
            if (i % sshu[j] == 0) {
                num[v] = num[i] + 1;
                d[v] = d[i] / num[v] * (num[v] + 1);
                break;
            }
            d[v] = d[i] << 1; num[v] = 1;
        }
    }
    //for (int i=1;i<=10;i++) printf("%d\n",d[i]);
}
int a[maxn];
struct node {
    int l, r;
    int laze;
    bool isall;
    ll num;
} segment_tree[maxn << 2];

void pushup(int rt)
{
    segment_tree[rt].num = segment_tree[rt << 1].num + segment_tree[rt << 1 | 1].num;
    segment_tree[rt].isall = segment_tree[rt << 1].isall & segment_tree[rt << 1 | 1].isall;
}
void build(int rt, int l, int r)
{
    segment_tree[rt].l = l;
    segment_tree[rt].r = r;
    if (l == r) {
        segment_tree[rt].num =a[l];
        if (segment_tree[rt].num == 1 || segment_tree[rt].num == 2) {
            segment_tree[rt].isall = 1;
        }
        return ;
    }
    int mid = (l + r) >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    pushup(rt);
}

void update(int rt, int l, int r)
{
    if (l <= segment_tree[rt].l && r >= segment_tree[rt].r && segment_tree[rt].isall) {
        return;
    }
    if (segment_tree[rt].l == segment_tree[rt].r) {
        segment_tree[rt].num = d[segment_tree[rt].num];
        if (segment_tree[rt].num == 1 || segment_tree[rt].num == 2) {
            segment_tree[rt].isall = 1;
        }
        return ;
    } else {
        int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
        if (mid >= l) {
            update(rt << 1, l, r);
        }
        if (mid < r) {
            update(rt << 1 | 1, l, r);
        }
        pushup(rt);
    }
}
ll query(int rt, int l, int r)
{
    if (segment_tree[rt].l >= l && segment_tree[rt].r <= r) {
        ll res = 0ll;
        res += segment_tree[rt].num;
        return res;
    }
    int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
    ll res = 0ll;
    if (mid >= l) {
        res += query(rt << 1, l, r);
    }
    if (mid < r) {
        res += query(rt << 1 | 1, l, r);
    }
    return res;

}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    prepare();
    int n, m;
    du2(n, m);
    repd(i, 1, n) {
        du1(a[i]);
    }
    build(1, 1, n);
    repd(i, 1, m) {
        int op; int l, r;
        du3(op, l, r);
        if (op == 1) {
            update(1, l, r);
        } else {
            printf("%lld\n", query(1, l, r));
        }
    }
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}


 

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11617207.html