Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 2114    Accepted Submission(s): 915

Problem Description
In a galaxy far, far away, there are two integer sequence a and b of length n.
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⌋
 
Input
There are multiple test cases, please read till the end of input file.
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.
 
Output
Output the answer for each 'query', each one line.
 
Sample Input
5 12
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
 
Sample Output
1
1
2
4
4
6
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 
 
题意:有个初始值为0的数组a,和一个数组b,每次可以对a数组所有数进行一次加一的操作,问每次查询时 a(l)/b(l) + a(l+1)/b(l+1) + ... + a(r)/b(r) 的和
分析:考虑我们要求的是[a1/b1] + [a2/b2] + ... + [an/bn]的和,看单独的项a1/b1,因为a1是从零开始的,所以只有当我们加的次数等于b1时(每次只加一),整体的和才会加一
a1每次加一一直加到b1相当于b1每次减一一直减到0(当bi的值减到一再重新赋值为bi这样可以一直进行加减操作),因为bi最开始的值是大于0的,所以我只需要用一个线段树来维护每次的最小值和区间和就行
#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 线段树变形的更多相关文章

  1. HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

    hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...

  2. HDU 6315 Naive Operations(线段树区间整除区间)

    Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...

  3. 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, ...

  4. HDU 6315 Naive Operations(线段树+复杂度均摊)

    发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...

  5. 2018 Multi-University Training Contest 2 杭电多校第二场

    开始逐渐习惯被多校虐orz  菜是原罪 1004  Game    (hdoj 6312) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 虽然披着 ...

  6. [2019杭电多校第二场][hdu6602]Longest Subarray(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K. 大致的分析一 ...

  7. hdu6312 2018杭电多校第二场 1004 D Game 博弈

    Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. [2019杭电多校第二场][hdu6601]Keen On Everything But Triangle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6601 题意是说用给定区间内的数字组成周长最大的三角形. 大致做法就是求区间第1大,第2大和第3大然后判 ...

  9. [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]也满足 ...

随机推荐

  1. codeforces 340 A. The Wall

    水水的一道题,只需要找xy的最小公倍数,然后找a b区间有多少个可以被xy的最小公倍数整除的数,就是答案. //============================================ ...

  2. JavaFX 选择文件 导入Excel文件并解析

    FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...

  3. 使用RedisMQ 做一次分布式改造

    引言 熟悉TPL Dataflow博文的朋友可能记得这是个单体程序,使用TPL Dataflow 处理工作流任务, 在使用Docker部署的过程中, 有一个问题一直无法回避: 在单体程序部署的瞬间会有 ...

  4. 在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL

    在 树莓派(Raspberry PI) 中使用 Docker 运行 MySQL 本文主要利用 biarms 提供的 Dockerfile 进行安装. 笔者最新发现! MySQL 5.7 Docker ...

  5. Rikka with Game[技巧]----2019 杭电多校第九场:1005

      Rikka with Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Othe ...

  6. iOS学习——iOS 宏(define)与常量(const)的正确使用

    概述 在iOS开发中,经常用到宏定义,或用const修饰一些数据类型,经常有开发者不知怎么正确使用,导致项目中乱用宏与const修饰.你能区分下面的吗?知道什么时候用吗? #define HSCode ...

  7. c++的构造和析构

    //文件名ss.h 1 #pragma once class ss { private: char*p;//利用指针来为p申请对内存 float height; ; char sex; public: ...

  8. JavaScript浮点数运算的精度问题

    之前在做浮点数计算时,偶然发现计算结果有误差,度娘了解了下,补充整理了下. 误差是什么样子的呢?举例 console.log(0.1+0.2); // 0.30000000000000004 事实上在 ...

  9. c++采集windows操作系统名称

    WINAPI windows通过c++获取操作系统主要分两种: 1. windows是8.1版本以下版本:获取操作系统可以通过windows提供的api中GetVersionEx函数来获取 2. wi ...

  10. 自己搭建传统ocr识别项目学习

    大批生成文集训练集: https://www.cnblogs.com/skyfsm/p/8436820.html 基于深度学习的文字识别(3755个汉字) http://www.cnblogs.com ...