HDOJ 5306 Gorgeous Sequence 线段树
http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/
Gorgeous Sequence
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 440 Accepted Submission(s): 87
length n.
We use ai to
denote the i-th
element in this sequence. You should do the following three types of operations to this sequence.
0 x y t:
For every x≤i≤y,
we use min(ai,t) to
replace the original ai's
value.
1 x y:
Print the maximum value of ai that x≤i≤y.
2 x y:
Print the sum of ai that x≤i≤y.
indicating the number of testcases.
The first line contains two integers n and m denoting
the length of the sequence and the number of operations.
The second line contains n separated
integers a1,…,an (∀1≤i≤n,0≤ai<231).
Each of the following m lines
represents one operation (1≤x≤y≤n,0≤t<231).
It is guaranteed that T=100, ∑n≤1000000, ∑m≤1000000.
print one line containing the answer to the corresponding query.
1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5
5
15
3
12HintPlease use efficient IO method
/* ***********************************************
Author :CKboss
Created Time :2015年07月27日 星期一 08时13分39秒
File Name :HDOJ5306.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=1000030; int n,m;
int a[maxn]; struct Node
{
LL ss;
int mx,tag,cv; void toString() {
printf("ss: %lld mx: %d tag: %d cv: %d\n",ss,mx,tag,cv);
} }T[maxn<<2]; #define lrt (rt<<1)
#define rrt (rt<<1|1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 void push_up(int rt)
{
T[rt].ss=T[lrt].ss+T[rrt].ss;
T[rt].mx=max(T[lrt].mx,T[rrt].mx);
T[rt].cv=T[lrt].cv+T[rrt].cv;
} void pnc(int t,int l,int r,int rt)
{
if(T[rt].tag!=0&&T[rt].tag<=t) return ; int all=r-l+1;
if(T[rt].cv!=all)
{
T[rt].ss+=(LL)t*(all-T[rt].cv);
T[rt].tag=T[rt].mx=t;
T[rt].cv=all;
}
} void push_down(int l,int r,int rt)
{
if(T[rt].tag)
{
int m=(l+r)/2;
pnc(T[rt].tag,lson); pnc(T[rt].tag,rson);
}
} /// 清除掉全部大于t的标记
void fix(int t,int l,int r,int rt)
{
if(T[rt].mx<t) return ; if(T[rt].tag>=t) T[rt].tag=0;
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag;
T[rt].cv=T[rt].tag!=0;
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
fix(t,lson); fix(t,rson);
push_up(rt);
}
} void build(int l,int r,int rt)
{
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=a[l];
T[rt].cv=1;
return ;
} T[rt].tag=0;
int m=(l+r)/2;
build(lson); build(rson);
push_up(rt);
} /// 0
void update(int L,int R,int t,int l,int r,int rt)
{
if(T[rt].mx<=t) return ; if(L<=l&&r<=R)
{
fix(t,l,r,rt);
if(l==r)
{
T[rt].ss=T[rt].mx=T[rt].tag=t;
T[rt].cv=1;
}
else push_up(rt); pnc(t,l,r,rt);
}
else
{
push_down(l,r,rt);
int m=(l+r)/2;
if(L<=m) update(L,R,t,lson);
if(R>m) update(L,R,t,rson);
push_up(rt);
}
} /// 1
int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].mx; push_down(l,r,rt); int m=(l+r)/2;
int ret=0; if(L<=m) ret=max(ret,query_max(L,R,lson));
if(R>m) ret=max(ret,query_max(L,R,rson)); return ret;
} /// 2
LL query_sum(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return T[rt].ss; push_down(l,r,rt); int m=(l+r)/2; LL ret=0;
if(L<=m) ret+=query_sum(L,R,lson);
if(R>m) ret+=query_sum(L,R,rson); return ret;
} void show(int l,int r,int rt)
{
printf("rt: %d %d <---> %d\n ",rt,l,r);
T[rt].toString(); if(l==r) return ; int m=(l+r)/2;
show(lson); show(rson);
} /********** fast read **************/ char *ch,buf[40*1024000+5]; void nextInt(int& x)
{
x=0;
for(++ch;*ch<=32;++ch);
for(x=0;'0'<=*ch;ch++) x=x*10+*ch-'0';
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); ch=buf-1;
fread(buf,1,1000*35*1024,stdin); int T_T;
nextInt(T_T); while(T_T--)
{
nextInt(n); nextInt(m); for(int i=1;i<=n;i++) nextInt(a[i]); build(1,n,1); int k,l,r,t;
while(m--)
{
nextInt(k);
if(k==0)
{
nextInt(l); nextInt(r); nextInt(t);
update(l,r,t,1,n,1);
}
else if(k==1)
{
nextInt(l); nextInt(r);
printf("%d\n",query_max(l,r,1,n,1));
}
else if(k==2)
{
nextInt(l); nextInt(r);
//printf("%lld\n",query_sum(l,r,1,n,1));
printf("%I64d\n",query_sum(l,r,1,n,1));
}
}
}
return 0;
}
HDOJ 5306 Gorgeous Sequence 线段树的更多相关文章
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析
Code: #include<algorithm> #include<cstdio> #include<cstring> #define ll long long ...
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- HDU - 5306 Gorgeous Sequence (吉司机线段树)
题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...
- 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Wow! Such Sequence!(线段树4893)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- hdu4893Wow! Such Sequence! (线段树)
Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...
随机推荐
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- Hadoop框架基础(三)
** Hadoop框架基础(三) 上一节我们使用eclipse运行展示了hdfs系统中的某个文件数据,这一节我们简析一下离线计算框架MapReduce,以及通过eclipse来编写关于MapReduc ...
- Linux基础04
** Linux基本操作常用命令(四) ** Linux系统管理命令 1.top:查看系统资源,每隔三秒刷新一次,按q:退出浏览状态 2.free:查看内存信息,-m,以MB单位显示 3.netsta ...
- 替换默认debug.keystore文件
最近在开发过程中需要频繁的为测试的同事签名apk,感觉非常很麻烦,于是就想把Intellij或是Eclipse使用的默认debug.keystore文件替换成发布用(生产环境)的签名文件,这样就可以直 ...
- python实现决策树C4.5算法(在ID3基础上改进)
一.概论 C4.5主要是在ID3的基础上改进,ID3选择(属性)树节点是选择信息增益值最大的属性作为节点.而C4.5引入了新概念"信息增益率",C4.5是选择信息增益率最大的属性作 ...
- Maven(一)之Maven入门
一.Maven简介 Maven可以翻译为“知识的积累”.“专家”.“内行”.作为Apache组织中的一个颇为成功的开源项目,Maven主要服务于基于Java平台的项目构建.依赖管理.和项目信息管理.M ...
- Python3基础笔记---re模块
参考博客: Py西游攻关之模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列 ...
- 设置cookie的方法
设置cookie的方法 1.登录之后后端返回的cookie放在响应的数据里,我们可以取到值, 这样就设置上了一个cookie,然后由于我们需要在三个环境里进行操作,开发环境.测试环境.生产环境.刚刚上 ...
- ES6学习笔记(十八)Class 的继承
1.简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链prototype实现继承,要清晰和方便很多. class Point { } class ColorPoin ...
- 用xmanager6启动Linux上的图形界面程序
1.下载Xmanager6 并自行安装,这里不赘述了 2.打开Xmanager.启动Xstart 3.按提示输入:主机IP,协议,用户名,命令,完成后点击“保存”,接着点击“运行”,运行xmanage ...