HDU 6351 Naive Operations(线段树)
题目:
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)
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⌋
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.
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
#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(线段树)的更多相关文章
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU 6315 Naive Operations(线段树区间整除区间)
Problem DescriptionIn a galaxy far, far away, there are two integer sequence a and b of length n.b i ...
- HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树
hdu 6315 题意:对于一个数列a,初始为0,每个a[ i ]对应一个b[i],只有在这个数字上加了b[i]次后,a[i]才会+1. 有q次操作,一种是个区间加1,一种是查询a的区间和. 思路:线 ...
- 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, ...
- HDU 6315 Naive Operations(线段树+复杂度均摊)
发现每次区间加只能加1,最多全局加\(n\)次,这样的话,最后的答案是调和级数为\(nlogn\),我们每当答案加1的时候就单点加,最多加\(nlogn\)次,复杂度可以得当保证. 然后问题就是怎么判 ...
- 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,另一种操 ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- HDU6315 Naive Operations(线段树 复杂度分析)
题意 题目链接 Sol 这题关键是注意到题目中的\(b\)是个排列 那么最终的答案最多是\(nlogn\)(调和级数) 设\(d_i\)表示\(i\)号节点还需要加\(d_i\)次才能产生\(1\)的 ...
- 2018HDU多校二 -F 题 Naive Operations(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6315 In a galaxy far, far away, there are two integer ...
随机推荐
- mac中svn服务器的搭建以及如何在eclipse中使用
mac自带了svn客户端和服务端功能. 1.查看svn版本 svnserve --version yintingtingdeMacBook-Pro:~ yintingting$ svnserve -- ...
- 微信小程序奇奇怪怪的语法
这... <view class="body"> <view class="nav bc_white"> <view class= ...
- Java URLDecoder 和 URLEncoder 对中文进行编码和解码
URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串: ...
- SambaJava API
做一个 backup package net.jnas; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...
- <转载> FreeNAS的安装和简单配置 http://freenas.cn/?p=342
前些日子在公司搭了一个模拟生产环境的平台.由于是测试环境,资源有限只能使用虚拟机实现,所以存储这块就想到了使用FreeNAS.很早以前玩儿过几次,当时是生产环境需要上存储设备,经过对比还是选择的更可靠 ...
- Mybatis十( mybatis其他使用)
1.批量执行 public void addUser(User user); <insert id="addUser" parameterType="model.U ...
- win8换win7的操作方法
详细参考UEFI与 Legacy BIOS两种启动模式详解 BIOS的两种引导模式 win8更换win7的方法的两个步骤: (1).设置BIOS支持Legacy启动,具体目标就是设置secur ...
- RabbitMq(2) 简单消息队列
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client </ar ...
- shell脚本判断执行用户
在脚本中,判断执行者是否为root. 判断方法1, #!/bin/bash if [ `whoami` != "root" ];then echo " only root ...
- Block 语法
Block,代码块,^符号是block的语法标记. 比如说,一个block的参数列表是一个UIView,返回值是个CGFloat,block名称是testBlock 可以定义为 CGFloat (^ ...