传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5828

【题解】

考虑bzoj3211 花神游历各国,只是多了区间加操作。

考虑上题写法,区间全为1打标记。考虑推广到这题:如果一个区间max开根和min开根相同,区间覆盖标记。

巧的是,这样复杂度是错的!

e.g:

$n = 10^5, m = 10^5$

$a[] = \{1, 2, 1, 2, ... , 1, 2\}$

$operation = \{ "1~1~n~2", "2~1~n", "1~1~n~2", "2~1~n", ... \}$

然后发现没有可以合并的,每次都要暴力做,复杂度就错了。

考虑对于区间的$max-min \leq 1$的情况维护:

当$max=min$,显然直接做即可。

当$max=min+1$,如果$\sqrt{max} = \sqrt{min}$,那么变成区间覆盖;否则$\sqrt{max} = \sqrt{min} + 1$,变成区间加法。

都是线段树基本操作,所以可以做。

下面证明复杂度为什么是对的:

时间复杂度$O(nlog^2n)$。

# include <math.h>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h> # ifdef WIN32
# define LLFORMAT "%I64d"
# else
# define LLFORMAT "%lld"
# endif using namespace std; typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 1e5 + ;
const int mod = 1e9+; inline int getint() {
int x = ; char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = (x<<) + (x<<) + ch - '', ch = getchar();
return x;
} int n, a[N]; const int SN = + ;
struct SMT {
ll mx[SN], mi[SN], s[SN], tag[SN], cov[SN];
# define ls (x<<)
# define rs (x<<|)
inline void up(int x) {
mx[x] = max(mx[ls], mx[rs]);
mi[x] = min(mi[ls], mi[rs]);
s[x] = s[ls] + s[rs];
}
inline void pushtag(int x, int l, int r, ll tg) {
mx[x] += tg, mi[x] += tg;
s[x] += tg * (r-l+); tag[x] += tg;
}
inline void pushcov(int x, int l, int r, ll cv) {
mx[x] = cv, mi[x] = cv;
s[x] = cv * (r-l+); cov[x] = cv; tag[x] = ;
}
inline void down(int x, int l, int r) {
register int mid = l+r>>;
if(cov[x]) {
pushcov(ls, l, mid, cov[x]);
pushcov(rs, mid+, r, cov[x]);
cov[x] = ;
}
if(tag[x]) {
pushtag(ls, l, mid, tag[x]);
pushtag(rs, mid+, r, tag[x]);
tag[x] = ;
}
}
inline void build(int x, int l, int r) {
tag[x] = cov[x] = ;
if(l == r) {
mx[x] = mi[x] = s[x] = a[l];
return ;
}
int mid = l+r>>;
build(ls, l, mid); build(rs, mid+, r);
up(x);
}
inline void edt(int x, int l, int r, int L, int R, int d) {
if(L <= l && r <= R) {
pushtag(x, l, r, d);
return ;
}
down(x, l, r);
int mid = l+r>>;
if(L <= mid) edt(ls, l, mid, L, R, d);
if(R > mid) edt(rs, mid+, r, L, R, d);
up(x);
}
inline void doit(int x, int l, int r) {
if(mx[x] == mi[x]) {
register ll t = mx[x];
pushtag(x, l, r, ll(sqrt(t)) - t);
return ;
}
if(mx[x] == mi[x] + ) {
register ll pmx = ll(sqrt(mx[x])), pmi = ll(sqrt(mi[x]));
if(pmx == pmi) pushcov(x, l, r, pmx);
else pushtag(x, l, r, pmx - mx[x]); // mx[x] = mi[x] + 1
return ;
}
down(x, l, r);
int mid = l+r>>;
doit(ls, l, mid); doit(rs, mid+, r);
up(x);
} inline void edt(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) {
doit(x, l, r);
return ;
}
down(x, l, r);
int mid = l+r>>;
if(L <= mid) edt(ls, l, mid, L, R);
if(R > mid) edt(rs, mid+, r, L, R);
up(x);
} inline ll sum(int x, int l, int r, int L, int R) {
if(L <= l && r <= R) return s[x];
down(x, l, r);
int mid = l+r>>; ll ret = ;
if(L <= mid) ret += sum(ls, l, mid, L, R);
if(R > mid) ret += sum(rs, mid+, r, L, R);
return ret;
}
}T; inline void sol() {
n = getint(); register int Q = getint(), op, l, r, x;
for (int i=; i<=n; ++i) a[i] = getint();
T.build(, , n);
while(Q--) {
op = getint(), l = getint(), r = getint();
if(op == ) {
x = getint();
T.edt(, , n, l, r, x);
} else if(op == ) T.edt(, , n, l, r);
else printf(LLFORMAT "\n", T.sum(, , n, l, r));
}
} int main() {
int T = getint();
while(T--) sol();
return ;
}

hdu5828 Rikka with Sequence的更多相关文章

  1. HDU5828 Rikka with Sequence 线段树

    分析:这个题和bc round 73应该是差不多的题,当时是zimpha巨出的,那个是取phi,这个是开根 吐槽:赛场上写的时候直接维护数值相同的区间,然后1A,结果赛后糖教一组数据给hack了,仰慕 ...

  2. 2018.07.23 hdu5828 Rikka with Sequence(线段树)

    传送门 这道题维护区间加,区间开根,区间求和. 线段树常规操作. 首先回忆两道简单得多的线段树. 第一个:区间覆盖,区间加,区间求和. 第二个:区间开根,区间求和. 这两个是名副其实的常规操作. 但这 ...

  3. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  4. 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence

    // 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...

  5. HDU 5828 Rikka with Sequence (线段树)

    Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  6. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  7. hdu 5204 Rikka with sequence 智商不够系列

    Rikka with sequence Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  8. HDU 5828 Rikka with Sequence(线段树 开根号)

    Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  9. HDU 5828 Rikka with Sequence(线段树区间加开根求和)

    Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...

随机推荐

  1. DAY5敏捷冲刺

    站立式会议 工作安排 (1)服务器配置 对单词学习的记录储存 (2)数据库配置 单词学习记录+用户信息 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录

  2. matlab的二维卷积操作(转)

    MATLAB的conv2函数实现步骤(conv2(A,B)): 其中,矩阵A和B的尺寸分别为ma*na即mb*nb ① 对矩阵A补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都 ...

  3. Jedis源码解析——Jedis和BinaryJedis

    1.基本信息 先来看看他们的类定义: public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands ...

  4. 【linux】- nohup 和 &

    &的意思是在后台运行, 什么意思呢? 意思是说,当你在执行 ./a.out & 的时候,即使你用ctrl C,那么a.out照样运行(因为对SIGINT信号免疫).但是要注意,如果你直 ...

  5. 【PHP】- PHPStorm+XDebug进行调试图文教程

    转载:https://www.cnblogs.com/LWMLWM/p/8251905.html   这篇文章主要为大家详细介绍了PHPStorm+XDebug进行调试图文教程,内容很丰富,具有一定的 ...

  6. 客户端 new socket时候 就像服务端发起连接了

    客户端 new socket时候  就像服务端发起连接了

  7. BZOJ 1791 岛屿(环套树+单调队列DP)

    题目实际上是求环套树森林中每个环套树的直径. 对于环套树的直径,可以先找到这个环套树上面的环.然后把环上的每一点都到达的外向树上的最远距离作为这个点的权值. 那么直径一定就是从环上的某个点开始,某个点 ...

  8. 【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  9. Django 2.0 学习(11):Django setuptools

    应用打包 当前状态的Python包与各种工具有点儿混乱,本结我们将学习使用setuptools来构建应用包.该工具是强烈推荐使用的打包工具,之后我们也会使用pip去安装和卸载它. Python打包指的 ...

  10. hadoop 编码实现文件传输、查看等基本文件控制

    hadoop集群搭建参考:https://www.cnblogs.com/asker009/p/9126354.html 1.创建一个maven工程,添加依赖 <?xml version=&qu ...