杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2114 Accepted Submission(s): 915
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5
1
2
4
4
6
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 1000;
const ll mod = 1e9 + 7;
struct node {
ll fg;
ll s; //一段区间总和
ll mv; //记录最小值
}root[4*maxn];
ll b[maxn], n, m, le, ri;
char s[10];
void update( ll r ) {
root[r].mv = min( root[ls].mv, root[rs].mv );
root[r].s = root[ls].s + root[rs].s;
}
void setf( ll r, ll f ) {
root[r].fg += f;
root[r].mv += f;
}
void build( ll r, ll le, ll ri ) {
root[r].fg = 0;
if( le == ri ) {
root[r].mv = b[le]-1;
root[r].s = 0;
} else {
ll mid = ( le + ri ) >> 1;
build( ls, le, mid );
build( rs, mid+1, ri );
update(r);
}
}
void push( ll r ) {
if( root[r].fg ) { //fg为-1进行操作,将左右子树最小值置为-1,同时将左右子树fg标记为-1
setf( ls, root[r].fg );
setf( rs, root[r].fg );
root[r].fg = 0;
}
}
ll query( ll r, ll le, ll ri, ll tl, ll tr ) {
if( tl == le && tr == ri ) {
return root[r].s;
} else {
push(r);
ll mid = ( le + ri ) >> 1;
if( tr <= mid ) {
return query( ls, le, mid, tl, tr );
} else if( tl > mid ) {
return query( rs, mid+1, ri, tl, tr );
} else {
return query( ls, le, mid, tl, mid ) + query( rs, mid+1, ri, mid+1, tr );
}
}
}
void modify( ll r, ll le, ll ri, ll tl, ll tr ) {
if( tl > tr ) {
return;
}
if( tl == le && tr == ri ) {
if( root[r].mv > 0 ) {
root[r].mv --, root[r].fg --;
} else {
if( tl == tr ) {
root[r].mv = b[le]-1;
root[r].s ++;
} else {
push(r);
ll mid = ( le + ri ) >> 1;
if( root[ls].mv == 0 ) {
modify( ls, le, mid, tl, mid );
} else {
setf( ls, -1 );
}
if( root[rs].mv == 0 ) {
modify( rs, mid+1, ri, mid+1, tr );
} else {
setf( rs, -1 );
}
update(r);
}
}
} else {
push(r);
ll mid = ( le + ri ) >> 1;
if( tr <= mid ) {
modify( ls, le, mid, tl, tr );
} else if( tl > mid ) {
modify( rs, mid+1, ri, tl, tr );
} else {
modify( ls, le, mid, tl, mid ), modify( rs, mid+1, ri, mid+1, tr );
}
update(r);
}
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
while( scanf("%lld%lld",&n,&m) != EOF ) {
for( ll i = 1; i <= n; i ++ ) {
scanf("%lld",&b[i]);
}
build(1,1,n);
for( ll i = 1; i <= m; i ++ ) {
scanf("%s%lld%lld",s,&le,&ri);
if( s[0] == 'a' ) {
modify( 1, 1, n, le, ri );
} else {
printf("%lld\n",query(1,1,n,le,ri));
}
}
}
return 0;
}
杭电多校第二场 hdu 6315 Naive Operations 线段树变形的更多相关文章
- HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- HDU 6315 Naive Operations(线段树区间整除区间)
Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...
- HDU - 6315 Naive Operations (线段树+思维) 2018 Multi-University Training Contest 2
题意:数量为N的序列a和b,a初始全为0,b为给定的1-N的排列.有两种操作:1.将a序列区间[L,R]中的数全部+1:2.查询区间[L,R]中的 ∑⌊ai/bi⌋(向下取整) 分析:对于一个位置i, ...
- HDU 6315 Naive Operations(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- 2018 Multi-University Training Contest 2 杭电多校第二场
开始逐渐习惯被多校虐orz 菜是原罪 1004 Game (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...
- [2019杭电多校第二场][hdu6602]Longest Subarray(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K. 大致的分析一 ...
- hdu6312 2018杭电多校第二场 1004 D Game 博弈
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- [2019杭电多校第二场][hdu6601]Keen On Everything But Triangle
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601 题意是说用给定区间内的数字组成周长最大的三角形. 大致做法就是求区间第1大,第2大和第3大然后判 ...
- [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...
随机推荐
- codeforces 340 A. The Wall
水水的一道题,只需要找xy的最小公倍数,然后找a b区间有多少个可以被xy的最小公倍数整除的数,就是答案. //============================================ ...
- JavaFX 选择文件 导入Excel文件并解析
FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...
- 使用RedisMQ 做一次分布式改造
引言 熟悉TPL Dataflow博文的朋友可能记得这是个单体程序,使用TPL Dataflow 处理工作流任务, 在使用Docker部署的过程中, 有一个问题一直无法回避: 在单体程序部署的瞬间会有 ...
- 在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL
在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL 本文主要利用 biarms 提供的 Dockerfile 进行安装. 笔者最新发现! MySQL 5.7 Docker ...
- Rikka with Game[技巧]----2019 杭电多校第九场:1005
Rikka with Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Othe ...
- iOS学习——iOS 宏(define)与常量(const)的正确使用
概述 在iOS开发中,经常用到宏定义,或用const修饰一些数据类型,经常有开发者不知怎么正确使用,导致项目中乱用宏与const修饰.你能区分下面的吗?知道什么时候用吗? #define HSCode ...
- c++的构造和析构
//文件名ss.h 1 #pragma once class ss { private: char*p;//利用指针来为p申请对内存 float height; ; char sex; public: ...
- JavaScript浮点数运算的精度问题
之前在做浮点数计算时,偶然发现计算结果有误差,度娘了解了下,补充整理了下. 误差是什么样子的呢?举例 console.log(0.1+0.2); // 0.30000000000000004 事实上在 ...
- c++采集windows操作系统名称
WINAPI windows通过c++获取操作系统主要分两种: 1. windows是8.1版本以下版本:获取操作系统可以通过windows提供的api中GetVersionEx函数来获取 2. wi ...
- 自己搭建传统ocr识别项目学习
大批生成文集训练集: https://www.cnblogs.com/skyfsm/p/8436820.html 基于深度学习的文字识别(3755个汉字) http://www.cnblogs.com ...