Gorgeous Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2150    Accepted Submission(s): 594

Problem Description
There is a sequence a of 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.

 
Input
The first line of the input is a single integer T, 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.

 
Output
For every operation of type 1 or 2, print one line containing the answer to the corresponding query.
 
Sample Input
1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5
 
Sample Output
5
15
3
12

Hint

Please use efficient IO method

 
Author
XJZX
 
Source
 

一份代码交了13遍。从TLE->WA->TLE->……QAQ

  1. #include<cstdio>
  2. #include<iostream>
  3. #define lc k<<1
  4. #define rc k<<1|1
  5. #define EF if(ch==EOF) return x;
  6. using namespace std;
  7. typedef long long ll;
  8. inline int read(){
  9. int x=,f=;char ch=getchar();
  10. while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
  11. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  12. return x*f;
  13. }
  14. const int N=1e6+;
  15. const int M=N<<;
  16. int n,m,a[N];
  17. ll sum[M];int mx[M],se[M],mc[M];
  18. inline void updata(int k){
  19. sum[k]=sum[lc]+sum[rc];
  20. mx[k]=max(mx[lc],mx[rc]);
  21. se[k]=max(se[lc],se[rc]);mc[k]=;
  22. if(mx[lc]!=mx[rc]) se[k]=max(se[k],min(mx[lc],mx[rc]));
  23. if(mx[k]==mx[lc]) mc[k]+=mc[lc];
  24. if(mx[k]==mx[rc]) mc[k]+=mc[rc];
  25. }
  26. inline void dec_tag(int k,int v){
  27. if(v>=mx[k]) return ;
  28. sum[k]+=1LL*(v-mx[k])*mc[k];mx[k]=v;
  29. }
  30. inline void pushdown(int k){
  31. dec_tag(lc,mx[k]);
  32. dec_tag(rc,mx[k]);
  33. }
  34. void build(int k,int l,int r){
  35. if(l==r){
  36. sum[k]=mx[k]=a[l];mc[k]=;se[k]=-;
  37. return ;
  38. }
  39. int mid=l+r>>;
  40. build(lc,l,mid);
  41. build(rc,mid+,r);
  42. updata(k);
  43. }
  44. void change(int k,int l,int r,int x,int y,int v){
  45. if(v>=mx[k]) return ;
  46. if(l==x&&r==y&&v>se[k]){
  47. dec_tag(k,v);return ;
  48. }
  49. pushdown(k);
  50. int mid=l+r>>;
  51. if(y<=mid) change(lc,l,mid,x,y,v);
  52. else if(x>mid) change(rc,mid+,r,x,y,v);
  53. else change(lc,l,mid,x,mid,v),change(rc,mid+,r,mid+,y,v);
  54. updata(k);
  55. }
  56. int query_max(int k,int l,int r,int x,int y){
  57. if(l==x&&r==y) return mx[k];
  58. pushdown(k);
  59. int mid=l+r>>;
  60. if(y<=mid) return query_max(lc,l,mid,x,y);
  61. else if(x>mid) return query_max(rc,mid+,r,x,y);
  62. else return max(query_max(lc,l,mid,x,mid),query_max(rc,mid+,r,mid+,y));
  63. }
  64. ll query_sum(int k,int l,int r,int x,int y){
  65. if(l==x&&r==y) return sum[k];
  66. pushdown(k);
  67. int mid=l+r>>;
  68. if(y<=mid) return query_sum(lc,l,mid,x,y);
  69. else if(x>mid) return query_sum(rc,mid+,r,x,y);
  70. else return query_sum(lc,l,mid,x,mid)+query_sum(rc,mid+,r,mid+,y);
  71. }
  72. inline void work(){
  73. n=read();m=read();
  74. for(int i=;i<=n;i++) a[i]=read();
  75. build(,,n);
  76. for(int i=,opt,x,y,z;i<=m;i++){
  77. opt=read();x=read();y=read();
  78. if(opt==) z=read(),change(,,n,x,y,z);
  79. if(opt==) printf("%d\n",query_max(,,n,x,y));
  80. if(opt==) printf("%lld\n",query_sum(,,n,x,y));
  81. }
  82. }
  83. int main(){
  84. for(int T=read();T--;) work();
  85. return ;
  86. }

HDU 5306 Gorgeous Sequence[线段树区间最值操作]的更多相关文章

  1. 【hdu5306】Gorgeous Sequence 线段树区间最值操作

    题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...

  2. 【bzoj4355】Play with sequence 线段树区间最值操作

    题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...

  3. HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析

    Code: #include<algorithm> #include<cstdio> #include<cstring> #define ll long long ...

  4. 【bzoj4695】最假女选手 线段树区间最值操作

    题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...

  5. HDOJ 5306 Gorgeous Sequence 线段树

    http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/ Gorgeous Sequence Time Limit: 6 ...

  6. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  7. 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 ...

  8. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  9. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

随机推荐

  1. PostgreSQL远端访问

    PostgreSQL默认的理念是运行在本地地址且不允许外部访问的. 如果想通过Navicat for postgreSql这种优秀的第三方软件访问需要做出如下修改: 一.启动在外部可访问的地址上 编辑 ...

  2. linux 使用FIO测试磁盘iops 方法详解

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎, 包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...

  3. Hp && Dell服务器硬件监控

    HP 安装HP工具: yum install hpssacli 1 查看控制器状态 raid卡型号等hpssacli ctrl all show status 2 查看硬盘类型.大小 raid级别.状 ...

  4. Linux之系统管理员笔记

    1.查看最近一次启动时间 who -b system boot -- : 2.who命令实现带有“表头”的查询结果 who -H NAME LINE TIME COMMENT root pts/ -- ...

  5. ubuntu和pypi换源

    ubuntu用apt-get下载的源是可以更换的.之前一直是打开软件中心在编辑里找源,找到后系统会自动备份原来的源并换源.奇怪却搜不到自己学校的源=.= 想换源还有一个原因,之前在update的时候会 ...

  6. lucene4.7学习总结 (zhuan)

    http://blog.csdn.NET/mdcmy/article/details/38167955?utm_source=tuicool&utm_medium=referral ***** ...

  7. jdom 读取

    读取XML文档 读取文档,首先需要一个xml的解析器,它可以自动的解析出各个元素,并且把子元素作为自己的孩子节点,方便操作. 主要使用的函数: SAXBuilder.build("xxx.x ...

  8. 基于html5和jquery的篮球跳动游戏

    今天给大家分享一款基于html5和jquery的篮球跳动游戏.这款实例和之前分享的HTML5重力感应小球冲撞动画类似.用鼠标拖动篮球,篮球在页面上跳动,可在演示中看下效果.效果图如下: 在线预览    ...

  9. rt serial 的编写

    /* * Copyright (C) 2005-2007 Jan Kiszka <jan.kiszka@web.de>. * * Xenomai is free software; you ...

  10. RabbitMQ之HelloWorld【译】

    简介 RabbitMQ是一个消息代理,主要的想法很简单:它接收并转发消息.你可以把它当做一个邮局,当你发送邮件到邮筒,你相信邮差先生最终会将邮件投递给收件人.RabbitMQ在这个比喻里,是一个邮筒, ...