题目链接: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. 巧用 git rebase 将某一部分 commit 复制到另一个分支

    一.为什么需要将一个 commit 复制到其他分支上去呢? 在我们的实际开发的过程中,我们的项目中会存在多个分支. 在某些情况下,可能需要将某一个分支上的 commit 复制到另一个分支上去.   二 ...

  2. CodeForces 402D Upgrading Array (数学+DP)

    题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数), 求 sum( ...

  3. TinkerPop中的遍历:图的遍历步骤(1/3)

    图遍历步骤(Graph Traversal Steps) 在最一般的层次上,Traversal<S,E>实现了Iterator,S代表起点,E代表结束.遍历由四个主要组成部分组成: Ste ...

  4. 处理器(也就是执行流程图上面的controller)执行逻辑(十)

    处理器继承自:AbstractController,MultiActionController   一.AbstractController 如上图,可以分析出AbstractController用的 ...

  5. springcloud 通过后端去下载和预览文件,要重设跨域允许

    @RequestMapping("/download") public void downloadNet(String uri, boolean isOnLine, HttpSer ...

  6. SqlBulkCopy(批量复制)使用方法 && SqlDataAdapter Update

    SqlBulkCopy提供了一种将数据复制到Sql Server数据库表中高性能的方法.SqlBulkCopy 包含一个方法 WriteToServer,它用来从数据的源复制数据到数据的目的地. Wr ...

  7. iTween插件使用

    itween插件 itween是一个动画库,作者创建它的目的就是最小的投入实现最大的产出.用它可以轻松实现各种动画,晃动,旋转,移动.褪色.上色.控制音频等. iTween原理: itween的核心是 ...

  8. js对象的深浅拷贝

    JS数据类型可以分为(ES5,暂时不考虑ES6): 简单数据类型:Number.String.undefined.boolean 复杂数据类型:Object.Array 简单的数据类型,往往是赋值操作 ...

  9. 【Linux】-Ubuntu下配置JDK1.8

    前言 这次实在是不想写前言了,好吧,那咱就不写了. 内容 怀着复杂的心情来整理这个小小的操作,其实我的内心是拒绝的,因为太简单了,但是我却花费了很长的时间,有效时间花费了将近两个小时去整理这个小玩意儿 ...

  10. uoj#448. 【集训队作业2018】人类的本质(Min_25筛+拉格朗日插值)

    题面 传送门 题解 肝了整整一天--膜拜yww和cx巨巨--(虽然它们的题解里我就没看懂几个字) 请备好草稿纸和笔,这种题目就是需要耐心推倒 题目所求是这么一个东西 \[ \begin{aligned ...