一、题目

二、题目链接

  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. 2018-2019-2 网络对抗技术 20165332 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165332 Exp2 后门原理与实践 - 实验内容 任务一:使用netcat获取主机操作Shell,cron启动 任务二:使用socat获取主机操作Shel ...

  2. docker 安装mysql mongo

    Docker安装Mysql 1.拉取镜像 docker pull mysql/mysql-server 2.运行mysql docker run -d -p : --name [Name] [Imag ...

  3. 解决xshell乱码问题

    如下图,xshell在执行命令时显示乱码 解决办法: 文件—属性—终端,将编码改成Unicode即可 参考文章 https://blog.csdn.net/yueloveme/article/deta ...

  4. HDU - 5917 水题

    题意:n个点m条边,找点集个数,点集满足有任意三个点成环,或者三个点互不相连 题解:暴力复杂度O(n^5/120*O(ok))==O(能过) //#pragma comment(linker, &qu ...

  5. UVA-810 A Dicey Problem (BFS)

    题目大意:滚骰子游戏,骰子的上面的点数跟方格中的数相同时或格子中的数是-1时能把格子滚过去,找一条从起点滚到起点的路径. 题目大意:简单BFS,状态转移时细心一些即可. 代码如下; # include ...

  6. oracle表的统计信息完全正确,执行计划无故改变。原厂人员如是回复

    就像在电话里提到的那样,Oracle内部的优化器是根据一系列的内部算法基于表上的统计信息来产生执行计划的.对于特别复杂的SQL语句,Oracle的优化器有一定几率不能得到最优的执行计划(因为机器代码实 ...

  7. C#删除图片问题

    public Image GetImage(string path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess. ...

  8. kali linux 2018.2 mysql密码修改后无效,外部无法连接问题。

    kali linux 2018.2 mysql密码修改后无效,外部无法连接问题 Kali Linux 2018.2 默认MySQL数据库是mariadb,可能和MySQL有些细微的变化,只需要做如下处 ...

  9. hdu 3262 09 宁波 现场 C - Seat taking up is tough 暴力 难度:0

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  10. SoftMax多分类器原理及代码理解

    关于多分类 我们常见的逻辑回归.SVM等常用于解决二分类问题,对于多分类问题,比如识别手写数字,它就需要10个分类,同样也可以用逻辑回归或SVM,只是需要多个二分类来组成多分类,但这里讨论另外一种方式 ...