题目链接: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
 
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+;
const int maxm=+;
ll b[maxn<<];//b数组
ll lazy[maxn<<];//延迟标记
ll sum[maxn<<];//记录区间ai/bi的和
ll mi[maxn<<];//记录b数组区间最小值
ll ans=;
void Pushup(ll rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
mi[rt]=min(mi[rt<<],mi[rt<<|]);//存区间最小的值
}
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)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
Pushup(rt);
}
void Pushdown(ll rt)
{
mi[rt<<]-=lazy[rt];
mi[rt<<|]-=lazy[rt];
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
lazy[rt]=;
}
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,那么整个区间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)>>; if(L<=mid) Updata(l,mid,rt<<,L,R);
if(R>mid) Updata(mid+,r,rt<<|,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)>>; if(lazy[rt])
Pushdown(rt); if(L<=mid) Query(l,mid,rt<<,L,R);//就是这里卡了几个小时 我把l写成1了 !!! 一直re 找了很久。。。
if(R>mid) Query(mid+,r,rt<<|,L,R); }
int main()
{
ll n,q;
ll l,r;
//string s;
char s[];
//while(cin>>n>>q)
while(scanf("%lld%lld",&n,&q)!=EOF)//cin cout会超时
{
ans=;
memset(sum,,sizeof(sum));
memset(lazy,,sizeof(lazy));
memset(mi,,sizeof(mi));
for(int i=;i<=n;i++) scanf("%d",&b[i]);
//cin>>b[i];
Build(,n,);
for(int i=;i<=q;i++)
{
ans=;
scanf("%s%lld%lld",s,&l,&r);
//cin>>s>>l>>r;
if(s[]=='a')
{
Updata(,n,,l,r); }
else
{
Query(,n,,l,r);
printf("%lld\n",ans);
//cout<<ans<<endl;
}
}
}
return ;
}

Naive Operations的更多相关文章

  1. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

  2. hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)

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

  3. hdu Naive Operations 线段树

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

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

    链接:HDU-6315:Naive Operations 题意: In a galaxy far, far away, there are two integer sequence a and b o ...

  5. HDU 多校对抗 F Naive Operations

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

  6. HDU 6315: Naive Operations

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

  7. HDU6315 Naive Operations(多校第二场1007)(线段树)

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

  8. HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)

    原题地址 Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/ ...

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

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

随机推荐

  1. kaggle Data Leakage

    What is Data Leakage¶ Data leakage is one of the most important issues for a data scientist to under ...

  2. Bugly集成指南

    官网: https://bugly.qq.com/v2/,用QQ扫码登录即可 1.创建应用,获取APPID 2.自动集成 2.1 在Module的build.gradle文件中添加依赖和属性配置: d ...

  3. Spring MVC 简介

  4. 读取txt文件的简易算法

    网友在问,从一个文本文件(txt)读取数据,并做简易算法.网友的原问题大约如下, 网友的问题,虽然说是全部是数字,但没有说明是否只有一行.因此Insus.NET在实现算法时,处理文本文件是否多行,是否 ...

  5. cuda by example

    int offset= x+y*dim   x 线程块内的线程索引 y 线程块索引 dim 线程块的维度   tid = threadIdx.x+blockIdx.x*blockDim.x 计算大于或 ...

  6. JAVA进阶----ThreadPoolExecutor机制(转)

    http://825635381.iteye.com/blog/2184680 ThreadPoolExecutor机制 一.概述 1.ThreadPoolExecutor作为java.util.co ...

  7. 【NOIP 2009】靶形数独

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...

  8. CF580B Kefa and Company 尺取法

    Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa ...

  9. UVa 11292 勇者斗恶龙(The Dragon of Loowater)

    首先先看一下这道题的英文原版... 好吧,没看懂... 大体意思就是: 有一条n个头的恶龙,现在有m个骑士可以雇佣去杀死他,一个能力值为x的勇士可以砍掉直径不超过x的头,而且需要支付x个金币.如何雇佣 ...

  10. C语言把字符串转换为数字

    C当中有一些函数专门用于把字符串形式转换成数值形式. printf()函数和sprintf()函数 -->通过转换说明吧数字从数字形式转换为字符串形式: scanf()函数把输入字符串转换为数值 ...