codeforces 920F 线段树

F. SUM and REPLACE
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 123 and 6).

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

  1. REPLACE l r — for every  replace ai with D(ai);
  2. 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 a1a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.

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

There is at least one SUM query.

Output

For each SUM query print the answer to it.

Example
input
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
output
30
13
4
22

题意:给你一个序列,有两个操作,一个是求区间l-r的和,另一个是对区间l-r的元素修改值,x=d(x),d(x)为x的因子个数。

题解:线段树,预先处理1e6的数的因子个数,然后就是线段树的操作了,注意用mx表示区间x的最大值,如果是小于等于2的直接return,没有必要继续往下走了,还有就是区间和用long long,否则会爆int。。。wa了一下午的血的教训。。

AC代码:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 7;

int a[maxn];
int cnt[maxn];
struct node
{
    int l,r;
    long long sum,mx;
}tree[maxn<<2];

void init()
{
    for(int i=1;i<maxn;i++)
        for(int j=i;j<maxn;j+=i) cnt[j]++;
}

void push_up(int pos)
{
    tree[pos].sum=tree[pos<<1].sum+tree[pos<<1|1].sum;
    tree[pos].mx=max(tree[pos<<1].mx,tree[pos<<1|1].mx);
}

void build(int pos,int l,int r)
{
    tree[pos].l=l; tree[pos].r=r;
    tree[pos].sum=tree[pos].mx=0;
    if(l==r)
    {
        tree[pos].sum=a[l];
        tree[pos].mx=a[l];
    }
    else
    {
        int mid=(l+r)>>1;
        build(pos<<1,l,mid);
        build(pos<<1|1,mid+1,r);
        push_up(pos);
    }
}

void update(int pos,int l,int r)
{
    if(tree[pos].mx<=2) return;
    int L=tree[pos].l,R=tree[pos].r;
    if(L==R)
    {
        tree[pos].sum=tree[pos].mx=cnt[tree[pos].mx];
        return;
    }
    int mid=(L+R)>>1;
    if(mid>=l) update(pos<<1,l,r);
    if(mid<r) update(pos<<1|1,l,r);
    push_up(pos);
}

long long query(int pos,int l,int r)
{
    int L=tree[pos].l,R=tree[pos].r;
    if(l<=L&&R<=r)
        return tree[pos].sum;
    if(L==R)
        return tree[pos].sum;
    int mid=(L+R)>>1;
    long long ans=0;
    if(mid>=l) ans+=query(pos<<1,l,r);
    if(mid<r) ans+=query(pos<<1|1,l,r);
    return ans;
}

int main()
{
    init();
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,1,n);
    while(q--)
    {
        int op,l,r;
        scanf("%d%d%d",&op,&l,&r);
        if(op==1)
            update(1,l,r);
        else
        {
            long long ans=query(1,l,r);
            printf("%I64d\n",ans);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值