一、题目

二、题目链接

  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. 管道 && 消息队列 && 共享内存

    http://blog.csdn.net/piaoairy219/article/details/17333691 1. 管道 管道的优点是不需要加锁. 缺点是默认缓冲区太小,只有4K. 一个管道只适 ...

  2. 201621123010《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口.面向接口编程 interface.implements has-a 关系 Comparable.Comparator 1. ...

  3. win10下安装MySQL5.7.20

    1. 下载Mysql官方:http://www.mysql.com→downloads→选社区版本MySQL Community Edition(GPL)→点击Community(GPL)Downlo ...

  4. 通过iframe 实现upload file无刷新

    <html>    <head> </head> <body> <form encType="multipart/form-data&q ...

  5. word 使用中 上标符号的实现

    1.   首先在word 中打下一段话  如:   啦啦啦啦啦啦啦啦  然后加入你需要的上标   如   [2] 2.    选中你需要的上标,然后右击 3.   点击字体选项 出现下图: 4.  在 ...

  6. 利用U盘大白菜软件来重装win7系统

    个人装win7系统用了两个U盘,一个做启动盘(FAT32格式),另外一个当做系统盘(NTFS格式). 首先在电脑里面下载一个大白菜软件,并且安装好,打开软件,插上U盘,检测到了该U盘即可一键制作启动盘 ...

  7. Python3.x datetime模块

    1.时间间隔(timedelta) 指定时间长度之间计算差值 #!/usr/bin/env python __author__ = 'realtiger' """ @ve ...

  8. Java编程之Map中分拣思想。

    题目:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数 旨意:map的分拣思想. 每一个key的包装类,存放出现的次数 /** * 作为包装类,用来存放英文单词,和该英文单词出现的次数 * ...

  9. mac终端下修改MySQL的编码格式--找不到my-default.cnf及my.cnf

    首先请确认正确安装好MySQL. 1- 先配置环境变量path 1.1 打开终端,输入: cd ~ 会进入~文件夹, 1.2 然后输入:touch .bash_profile 回车执行后, 1.3 再 ...

  10. python 之 collections

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 基本介绍 我们都知道,Python拥有一些内置的数 ...