题目:

http://acm.hdu.edu.cn/showproblem.php?pid=6315

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)

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
 
题意:给定一个初始数组b和一个初始值全部为0的数组a,每次操作可以在给定的区间(l,r)内让a[i](l=<i<=r)加一,或者查询区间区间(l,r)中a[i]/b[i](l=<i<=r)(取整)的和。
思路:
用线段树存放a数组,做好最小更新标记,达到则向下更新
代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=3e6+;
typedef long long LL;
typedef unsigned long long ULL;
//typedef pair<LL,LL> P;
const LL mod=1e9+;
using namespace std;
struct node{
LL l,r,sum,g,mi;
LL lazy;
LL mid(){
return (l+r)>>;
}
}a[maxn];
int b[maxn];
void build(int l,int r,int num){
a[num].l=l;
a[num].r=r;
a[num].lazy=;
if(l==r){
a[num].sum=;
a[num].g=;
a[num].mi=b[l];
}
else{
build(l,a[num].mid(),num<<);
build(a[num].mid()+,r,(num<<)|);
a[num].g=a[num<<].g+a[(num<<)|].g;
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
}
}
void as(int d)
{
if(a[d].lazy)
{
a[(d<<)].lazy+=a[d].lazy;
a[(d<<|)].lazy+=a[d].lazy;
a[(d<<)].mi-=a[d].lazy;
a[(d<<|)].mi-=a[d].lazy;
a[d].lazy=;
}
}
LL Find(int l,int r,int num){
if(a[num].l==l&&a[num].r==r){
return a[num].g;
}
if(l>a[num].mid()){
return Find(l,r,(num<<)|);
}
else if(r<=a[num].mid()){
return Find(l,r,num<<);
}
else{
return Find(l,a[num].mid(),num<<)+Find(a[num].mid()+,r,(num<<)|);
}
}
void add(int l,int r,int num,LL x){
if(a[num].l==l&&a[num].r==r||x==){
a[num].lazy+=x;
a[num].mi-=x;
if(a[num].mi>){
return ;
}
else if(l!=r){
as(num);
add(l,a[num].mid(),num<<,);
add(a[num].mid()+,r,(num<<)|,);
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
return;
}
}
if(l==r&&a[num].l==l&&a[num].r==r)
{
if(a[num].mi<=)
{
a[num].mi=a[num].lazy=;
a[num].mi=b[l];
a[num].g++;
}
return;
}
as(num);
if(l>a[num].mid()){
add(l,r,(num<<)|,x);
}
else if(r<=a[num].mid()){
add(l,r,num<<,x);
}
else {
add(l,a[num].mid(),num<<,x);
add(a[num].mid()+,r,(num<<)|,x);
}
a[num].mi=min(a[num<<].mi,a[(num<<)|].mi);
a[num].g=a[num<<].g+a[(num<<)|].g;
//cout<<'a'<<a[num].l<<' '<<a[num].r<<' '<<a[num].g<<endl;
}
int main(){
fio;
int n,m;
string op;
int x,y;
while(cin>>n>>m){
for(int i=;i<=n;i++){
cin>>b[i];
}
build(,n,);
while(m--){
cin>>op>>x>>y;
if(op[]=='a'){
add(x,y,,);
}
else if(op[]=='q'){ add(x,y,,);
cout<<Find(x,y,)<<endl;
}
}
}
}

HDU 6351 Naive Operations(线段树)的更多相关文章

  1. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  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-DuoXiao第二场hdu 6315 Naive Operations 线段树

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

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

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

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

  6. HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)

    http://acm.hdu.edu.cn/showproblem.php?pid=6315 题意 a数组初始全为0,b数组为1-n的一个排列.q次操作,一种操作add给a[l...r]加1,另一种操 ...

  7. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  8. HDU6315 Naive Operations(线段树 复杂度分析)

    题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...

  9. 2018HDU多校二 -F 题 Naive Operations(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...

随机推荐

  1. parameterized之unittest参数化

    unittest没有想testNG那么方便,可以进行参数化,但是有一个第三方库可是实现参数化 安装 pip install parameterized 该库可以在python的所有单元测试框架中使用 ...

  2. scala快速一览

    println("hello world"); val x = +; println(x); //val 不允许再次赋值 //x = 3; //变量var var xx = x; ...

  3. python学习笔记_week24

    note 内容回顾: Model - 数据库操作 on_delete Query_set select_related 跨表数据一次性拿过来,不增加sql查询次数.帮助跨表,后面参数只能加连表字段 f ...

  4. 【4-1】js函数、事件、补充知识

    一.函数操作 (一)字符串操作: (1)变量名.toLowerCase():--转小写     toUpperCase():----转大写 (2)变量名.substring(索引,截取到位数);--- ...

  5. Mybatis学习4——核心文件sqlMapperConfig.xml属性

    1.外部文件jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis ...

  6. elk收集windows日志

    参考网站:https://www.secpulse.com/archives/55636.html https://blog.csdn.net/qq_38094271/article/details/ ...

  7. 4. mysql 1449 : The user specified as a definer ('test'@'%') does not exist 解决方法

    权限问题,授权 给 root  所有sql 权限 mysql> grant all privileges on *.* to test@"%" identified by & ...

  8. Flex学习笔记-使用MXML和一个AS事件监听器监听事件

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  9. CentOS6.9 网络设置

    一.临时设置IP地址 ifconfig eth0 192.168.42.119 broadcast 192.168.42.129 netmask 255.255.255.0 二.上述方法只能临时生效, ...

  10. docker修改镜像名称

    [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pujh/centos tomcat-centos 70f ...