题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315

学习博客:https://blog.csdn.net/SunMoonVocano/article/details/81207676

Naive Operations

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

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
2018 Multi-University Training Contest 2
Recommend
chendu   |   We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 
题目大意:第一行输入n,p  代表数组a[]和b[]的长度为n ,p代表有p次操作,第二行n个数代表b[1]····b[n] 的值 a[1]···a[n]刚开始为0
接下来p行代表p个操作
add l r  表示区间   [l,r]内a数组每个数加一,
query l r  输出区间lr内 ai/bi的值的和(ai/bi向下取整)
思路:自己并不会做这道题,看了别人题解将近花了一天才做出来,感慨自己还是不熟悉线段树 ,一个小问题卡了两个多小时 ,代码中会说我卡在哪了,真的难受,。。。
好了吗,下面真的说思路:
因为ai/bi是向下取整,所以更新ai的值未必会影响到ai/bi的值 ,那么我们怎么进行区间更新呢?  说实话,想了挺久的,也没有想出来,正常思维下  给一个区间里每一个数加上1除以不同的数,那岂不是
要遍历才知道是否超过1,什么情况下可以不用遍历就知道呢?  可以试着猜想一下,如果我们知道那个区间bi的最小值,那么如果区间内最小值都不能提供一个1,那么肯定其它的也是不行的
重点来了:  与其给ai加上1 不如给bi减去一个1  每一次更新,只要bi大于1  那么肯定是对ai/bi没有影响的,所以只要bi-1就行了
具体看代码:
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
#include<cstring>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int maxm=1000+5;
ll b[maxn<<2];//b数组
ll lazy[maxn<<2];//延迟标记
ll sum[maxn<<2];//记录区间ai/bi的和
ll mi[maxn<<2];//记录b数组区间最小值
ll ans=0;
void Pushup(ll rt)
{sum[rt]=sum[rt<<1]+sum[rt<<1|1];mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);//存区间最小的值
}
void Build(ll l,ll r,ll rt)
{if(l==r){mi[rt]=b[l];//cout<<"rt:"<<rt<<"mi:"<<mi[rt]<<endl;return ;}ll mid=(l+r)>>1;Build(l,mid,rt<<1);Build(mid+1,r,rt<<1|1);Pushup(rt);
}
void Pushdown(ll rt)
{mi[rt<<1]-=lazy[rt];mi[rt<<1|1]-=lazy[rt];lazy[rt<<1]+=lazy[rt];lazy[rt<<1|1]+=lazy[rt];lazy[rt]=0;
}
void Updata(ll l ,ll r,ll rt,ll L,ll R)//(1,n,1,l,r)
{//cout<<"*"<<"l:"<<l<<"r:"<<r<<"L:"<<L<<"R:"<<R<<"mi:"<<mi[rt]<<"rt:"<<rt<<endl;if(L<=l&&r<=R)//这里是重点
    {
//        cout<<"叶子rt:"<<rt<<endl;if(mi[rt]>1)//最小的都大于1,那么整个区间ai/bi肯定是没有影响的,
        {mi[rt]--;lazy[rt]++;return ;}}if(l==r)//到了叶子节点,代表mi[rt]<=1  此时sum值要加1,同时mi[rt]恢复原值
    {sum[rt]++;mi[rt]=b[l];return ;}if(lazy[rt])Pushdown(rt);ll mid=(l+r)>>1;if(L<=mid) Updata(l,mid,rt<<1,L,R);if(R>mid) Updata(mid+1,r,rt<<1|1,L,R);Pushup(rt);
}
void Query(ll l,ll r,ll rt,ll L,ll R)
{if(L<=l&&r<=R){ans+=sum[rt];return ;}ll mid=(l+r)>>1;if(lazy[rt])Pushdown(rt);if(L<=mid) Query(l,mid,rt<<1,L,R);//就是这里卡了几个小时 我把l写成1了  !!!  一直re  找了很久。。。if(R>mid) Query(mid+1,r,rt<<1|1,L,R);}
int main()
{ll n,q;ll l,r;//string s;char s[10];//while(cin>>n>>q)while(scanf("%lld%lld",&n,&q)!=EOF)//cin  cout会超时
    {ans=0;memset(sum,0,sizeof(sum));memset(lazy,0,sizeof(lazy));memset(mi,0,sizeof(mi));for(int i=1;i<=n;i++) scanf("%d",&b[i]);//cin>>b[i];Build(1,n,1);for(int i=1;i<=q;i++){ans=0;scanf("%s%lld%lld",s,&l,&r);//cin>>s>>l>>r;if(s[0]=='a'){Updata(1,n,1,l,r);}else{Query(1,n,1,l,r);printf("%lld\n",ans);//cout<<ans<<endl;
            }}}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/10300967.html

Naive Operations相关推荐

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

    题目: 给出一个长度为n初值为0的数组,以及长度为n的b数组,然后q次操作,add(l,r) 使得区间l-r所有元素+1,或者查询l~r区间a[i]/b[i]的和 题解: 很少情况下能很快写出一道线段 ...

  2. HDU - 6315 Naive Operations(线段树+思维)

    题目链接:点击查看 题目大意:给出一个数列 a 和一个数列 b ,其中数列 a 初始时全部为 0 ,数列 b 初始时是一个 1 ~ n 的排列,接下来共有 m 次操作,每次操作分为两种: add l ...

  3. 多校训练 Naive Operations线段树区间更新

    http://acm.hdu.edu.cn/showproblem.php?pid=6315 维护ai/bi向下取整,怎么维护??a数组+1就是对应的b数组减一,b数组减到0时结果就该加1了 #inc ...

  4. 2018 Multi-University Training Contest 2: 1007. Naive Operations

    题意:给你一个长度为n的b[]数组和长度为n的a[]数组,b[]是1到n的全排列,a[]初始全为0,有两种操作:①add l r:a[]数组从l到r全部+1,②query l r:查询∑a[i]/b[ ...

  5. 【地狱副本】数据结构之线段树Ⅲ——区间最值/赋值/修改/历史值操作(HDU5306,Tyvj 1518,【清华集训2015】V,HDU6315,HDU1828,POJ3162)

    文章目录 Gorgeous Sequence Tyvj 1518 CPU监控 [清华集训2015]V Naive Operations Picture Walking Race Gorgeous Se ...

  6. 数据结构 —— 线段树

    [概述] 线段树是一种二叉搜索树,其存储的是一个区间的信息,每个结点以结构体的形式去存储,每个结构体包含三个元素:区间左端点.区间有端点.该区间要维护的信息(视实际情况而定),其基本思想是分治的思想. ...

  7. 20ZR暑期集训 简单数据结构

    方差 令 DDD 为方差,SSS 为区间和,S2S_2S2​ 为区间平方和. D=1r−l+1∑i=1r(ai−Sr−l+1)2=1r−l+1∑i=1r(ai2−2Sair−l+1+S2(r−l+1) ...

  8. 2021 HZNU Winter Training Day 18

    目录 Solutions A. 星球大战starwar B. Median C. Obtain Two Zeroes D. Naive Operations E. Co-prime F. Lightn ...

  9. 题解报告(CDUT暑期集训——第二场)

    题解报告(CDUT暑期集训--第二场) D - Game HDU - 6312 思路:水题 Alice一直是必胜态 AC代码 #include<stdio.h> #include<i ...

最新文章

  1. 推荐一个非常COOL的开源相册程序!
  2. BZOJ 3218(a + b Problem-二分图套值域线段树)
  3. boost::function_types::result_type用法的测试程序
  4. 18.self关键字.rs
  5. MongoDB via Dotnet Core数据映射详解
  6. 四人帮–代理设计模式
  7. 【Python】pyCryptodome模块实现AES加密、解密
  8. php状态,PHP 状态模式 - 304158的个人空间 - OSCHINA - 中文开源技术交流社区
  9. Ranger-Solr审计日志安装
  10. 有机晶体数据库_面向Journal of Organic Chemistry作者的晶体学信息文件(CIF)工作流程将于10月6日作出调整...
  11. ATF(Arm Trusted Firmware)/TF-A Chapter 05 BL2
  12. 人工智能AI-模型训练思路
  13. TOGAF9.2企业架构师考试小记
  14. 惠普服务器自动装驱动安装,Win10系统不断自动安装HP打印机驱动且报错0x80240017的解决方法...
  15. 物联网设备开发中常说的 AT 指令集是什么?
  16. sublime改成中文简体及菜单变成中文
  17. 为什么10M、20M的宽带只有大约1、2M的下载速度——网速KB/s与Kbps(Kb/s)的区别
  18. IDEA设置版权信息
  19. 计算机软考证书含金量和性价比分析
  20. 堆及堆排序(超超超超超详细讲解~~~~)-----数据结构

热门文章

  1. Java路径问题终于解决方式—可定位全部资源的相对路径寻址
  2. 从程序员角度--解决ipad白苹果问题的最佳办法---纠正网上的错误言论
  3. RMI 异常 no security manager: RMI class loader disabled
  4. 解决vue多个路由共用一个页面的问题
  5. 【白皮书】2020在线教育用户增长闭环白皮书.pdf(附下载链接)
  6. 双赛道20支战队解题思路大公开,线上Poster Session等你来
  7. 腾讯广告算法大赛 | 专家开小灶,独家解析Lookalike那些事儿
  8. java登陆拦截器_SpringBoot拦截器实现登录拦截
  9. 使用python排序_Python排序
  10. 12年外贸婚纱跨境老司机分享独立站推广引流实操干货