一、题目

二、题目链接

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

三、题意

  给定$N$个范围在$[1, 1e6)$的数字和$M$个操作。操作有两种类型:

  $1$ $l$ $r$:更新区间$[l$, $r]$的数字ai为d[ai]。其中d[i]表示数字i的因子的个数。如:d[1] = 1, d[2] = 2, d[3] = 2, d[4] = 3。

  $2$ $l$ $r$:查询区间$[l, r]$的数字和并输出。

四、思路

  典型的数据结构题。很明显这是线段树的菜。

  对于线段树的每个节点,维护所“管辖”区间的和值和最大值。

  更新时,如果当前区间的最大值$maxv<=2$,那么,不需要往下更新下去了。因为$d[1] = 1$, $d[2] = 2$。否则,继续更新下去。这样并不会超时,因为对于一个范围在$[1, 1e6)$的数字而言,反复做$i = d[i]$这样的操作,次数并不会太多。

  另外,更新完子区间后,要做上推合并操作。这是线段树很常见的操作。

五、源代码

  

#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
;

template <class T> inline void read(T &x) {
    int t;
    bool flag = false;
    ')) ;
    ';
     + t - ';
    if(flag) x = -x;
}

typedef struct {
    LL maxv, sum;
} Node;
Node data[MAXN * ];
 << ], n, q;
void init() {
    ;
    ; i <= N; ++i) {
        for(int j = i; j <= N; j += i)
            d[j]++;
    }
    memset(data, , sizeof(data));
}

void pushup(int root){
    data[root].sum = data[root << ].sum + data[root <<  | ].sum;
    data[root].maxv = max(data[root << ].maxv, data[root <<  | ].maxv);
}

, , int r = n) {
    if(l > x || r < x)return;
    if(l == r && l == x) {
        data[root].sum = data[root].maxv = LL(val);
        return;
    }
    ;
    , l, mid);
     | , mid + , r);
    pushup(root);
}

, , int r = n) {
    if(l > r || l > ur || r < ul)return;
    if(ul <= l && ur >= r && data[root].maxv <= 2LL)return;
    if(l == r) {
        data[root].sum = data[root].maxv = LL(d[data[root].sum]);
        return;
    }
    ;
    , l, mid);
     | , mid + , r);
    pushup(root);
}

LL query(, , int r = n) {
    if(l > r || l > qr || r < ql)return 0LL;
    if(ql <= l && qr >= r)return data[root].sum;
    ;
    , l, mid) + query(ql, qr, root <<  | , mid + , r);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Finput.txt", "r", stdin);
#endif // ONLINE_JUDGE
    init();
    int tmp, type, l, r;
    scanf("%d%d", &n, &q);
    ; i <= n; ++i){
        read(tmp);
        build(i, tmp);
    }
    while(q--){
        read(type), read(l), read(r);
        )update(l, r);
        else printf("%lld\n", query(l, r));
    }
    ;
}

Educational Codeforces Round 37-F.SUM and REPLACE题解的更多相关文章

  1. 【Educational Codeforces Round 37 F】SUM and REPLACE

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 那个D函数它的下降速度是很快的. 也就是说到最后他会很快的变成2或者1 而D(2)==2,D(1)=1 也就是说,几次操作过后很多数 ...

  2. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  3. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  4. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  5. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  6. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  7. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  8. Educational Codeforces Round 37 A B C D E F

    A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...

  9. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. Mongo配置基础

    数据库也是一种服务,数据库的本质也是一个文件,所以说我们把文件存入text和存入数据库的本质是一样的,只是数据库的格式化的删除和添加. 分为四部分, mongo的启动详解 导入导出,运行时备份 Fsy ...

  2. Linux下,EPM11.1.1.3 configurator 不能启动AdminServer

    需要测试环境, 安装EPM11.1.1.3 到 CentOS 5.6 在运行configurator(/app/hyperion/common/config/9.5.0.0 时, 卡在[Startin ...

  3. git 使用和安装

    http://www.git-scm.com/download/ http://www.git-scm.com/download/win http://www.git-scm.com/download ...

  4. 初识webservice 服务

    1.获取电话号码归属地查询 首先访问: http://www.webxml.com.cn/zh_cn/web_services.aspx ①新建一个MyEclipse项目(WebService) ht ...

  5. mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column

    1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...

  6. 99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. 【hive】count() count(if) count(distinct if) sum(if)的区别

    表名: user_active_day (用户日活表) 表内容: user_id(用户id)   user_is_new(是否新用户 1:新增用户 0:老用户) location_city(用户所在地 ...

  8. EPANET头文件解读系列4——EPANET2.H

    该头文件的功能与系列3中的TOOLKIT.H类似,而且内容也几乎一致,所以也就不再详细介绍.

  9. CF86D

    题解: 莫队分块 分块大小为sqrt(n) 代码: #include<bits/stdc++.h> using namespace std; ; typedef long long ll; ...

  10. Docker ENTRYPOINT

    entrypoint: 在启动镜像的时候会执行这个命令下的脚本,在docker run 和docker start情况下都会触发. 好比这个脚本是对某一个文件追加数据,每次start的时候都会追加,文 ...