hdu5828 Rikka with Sequence
传送门: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的更多相关文章
- HDU5828 Rikka with Sequence 线段树
分析:这个题和bc round 73应该是差不多的题,当时是zimpha巨出的,那个是取phi,这个是开根 吐槽:赛场上写的时候直接维护数值相同的区间,然后1A,结果赛后糖教一组数据给hack了,仰慕 ...
- 2018.07.23 hdu5828 Rikka with Sequence(线段树)
传送门 这道题维护区间加,区间开根,区间求和. 线段树常规操作. 首先回忆两道简单得多的线段树. 第一个:区间覆盖,区间加,区间求和. 第二个:区间开根,区间求和. 这两个是名副其实的常规操作. 但这 ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence
// 判断相同区间(lazy) 多校8 HDU 5828 Rikka with Sequence // 题意:三种操作,1增加值,2开根,3求和 // 思路:这题与HDU 4027 和HDU 5634 ...
- HDU 5828 Rikka with Sequence (线段树)
Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- hdu 5204 Rikka with sequence 智商不够系列
Rikka with sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 5828 Rikka with Sequence(线段树 开根号)
Rikka with Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5828 Rikka with Sequence(线段树区间加开根求和)
Problem DescriptionAs we know, Rikka is poor at math. Yuta is worrying about this situation, so he g ...
随机推荐
- c# CLR无法从 COM 上下文 0x51cd20 转换为 COM 上下文 0x51ce90
调试菜单--->异常---->managed debugging assistants栏下ContextSwitchDeadlock 前面的√去掉
- encode 与 decode
decode 将其它编码的字符串转换成unicode编码,例如:str1.decode("gb2312"),表示将gb2312编码的字符串转换成unicode编码 encode 将 ...
- lintcode-144-交错正负数
144-交错正负数 给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组. 注意事项 不需要保持正整数或者负整数原来的顺序. 样例 给出数组[-1, -2, -3, 4, 5, 6],重新 ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- ZOJ 1539 L-Lot
https://vjudge.net/contest/67836#problem/L Out of N soldiers, standing in one line, it is required t ...
- shell基础练习题讲解
1037774765 克隆 1.创建一个用户redhat,其ID号为1001,基本组为like(组ID为2002),附近租为linux. groupadd -g 2002 likegroupadd l ...
- Visual Stdio 2015打包安装项目的方法(使用Visual Studio Installer)
首先在官网下载VS2015的Visual Studio Installer 1.创建安装项目 里面最左侧的框框有三个文件夹 1.“应用程序文件夹”即"Application Folder&q ...
- shell脚本学习—条件测试和循环语句
条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...
- Square Root of Permutation - CF612E
Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...
- [BZOJ5339] [TJOI2018]教科书般的亵渎
题目链接 BZOJ题面. 洛谷题面. Solution 随便推一推,可以发现瓶颈在求\(\sum_{i=1}^n i^k\),关于这个可以看看拉格朗日插值法. 复杂度\(O(Tm^2)\). #inc ...