Gorgeous Sequence

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

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  6554 6553 6552 6551 6550 

题意 :

有一个长度为n的序列a。我们用ai来表示这个序列中的第i个元素。您应该对这个序列执行以下三种类型的操作。

0 x y t:对于每一个x≤i≤y,我们用min(ai,t)替换原始ai的值。

1 x y:打印ai的最大值,即x≤i≤y。

2xy:输出x≤i≤y的ai之和。

输入

输入的第一行是一个整数T,表示测试用例的数量。

第一行包含两个整数n和m,表示序列的长度和操作的数量。

第二行包含n分离整数a1,…,一个(∀1≤≤n, 0≤ai < 231)。

以下m行每一行表示一个操作(1≤x≤y≤n,0≤t<231)。

保证T=100,∑n≤1000000,∑m≤1000000。

输出

对于类型1或2的每个操作,打印一行包含相应查询的答案。

题解:

c++代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
int a[maxn];
#define EF if(ch==EOF) return x;
// #define lc k<<1
// #define rc k<<1|1
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct tree
{
int l , r;
int miax , maxx;
ll sum;
int set_lazy;
}t[maxn << ]; // inline void push_up(int k){
// // sum[k]=sum[lc]+sum[rc];
// // mx[k]=max(mx[lc],mx[rc]);
// // se[k]=max(se[lc],se[rc]);
// // mc[k]=0;
// // if(mx[lc]!=mx[rc]) se[k]=max(se[k],min(mx[lc],mx[rc]));
// // if(mx[k]==mx[lc]) mc[k]+=mc[lc];
// // if(mx[k]==mx[rc]) mc[k]+=mc[rc];
// t[k].sum = t[lc].sum + t[rc].sum;
// t[k].maxx = max(t[lc].maxx,t[rc].maxx);
// t[k].miax = max(t[lc].miax,t[rc].miax);
// t[k].set_lazy = 0;
// if(t[lc].maxx != t[rc].maxx) t[k].miax = max(t[k].miax,min(t[lc].maxx,t[rc].maxx));
// if(t[k].maxx == t[lc].maxx) t[k].set_lazy += t[lc].set_lazy;
// if(t[k].maxx == t[rc].maxx) t[k].set_lazy += t[rc].set_lazy; // }
inline void push_up(int rt){
t[rt].sum = t[rt << ].sum + t[rt << |].sum;
// t[rt].minn = min(t[rt << 1].minn,t[rt << 1|1].minn);
t[rt].maxx = max(t[rt << ].maxx,t[rt << |].maxx);
t[rt].miax = max(t[rt << ].miax, t[rt << |].miax);
t[rt].set_lazy = ;
if(t[rt << ].maxx != t[rt <<|].maxx) t[rt].miax = max(t[rt].miax,min(t[rt << ].maxx , t[rt <<|].maxx));
//打上标记,记录下标记个数
if(t[rt].maxx == t[rt << ].maxx) t[rt].set_lazy += t[rt << ].set_lazy;
if(t[rt].maxx == t[rt << |].maxx) t[rt].set_lazy += t[rt << |].set_lazy;
// cout << t[rt].sum << " " << rt <<endl;
} inline void dec_tag(int rt,int v){
if(v >= t[rt].maxx) return;
t[rt].sum += 1ll * (v - t[rt].maxx)*t[rt].set_lazy;
t[rt].maxx = v;
} inline void push_down(int rt){
dec_tag(rt << ,t[rt].maxx);
dec_tag(rt << |,t[rt].maxx);
}
// inline void push_down(int rt) {
// if(t[rt].set_lazy) { ///if set_lazy add_lazy = 0
// t[rt<<1].set_lazy = t[rt].set_lazy;
// t[rt<<1].sum = (t[rt<<1].r - t[rt<<1].l + 1) * t[rt].set_lazy;
// t[rt<<1].maxx = t[rt].set_lazy;
// t[rt<<1].minn = t[rt].set_lazy;
// t[rt<<1|1].set_lazy = t[rt].set_lazy;
// t[rt<<1|1].sum = (t[rt<<1|1].r - t[rt<<1|1].l + 1) * t[rt].set_lazy;
// t[rt<<1|1].maxx = t[rt].set_lazy;
// t[rt<<1|1].minn = t[rt].set_lazy;
// //tre[rt].add_lazy = 0;
// //tre[rt<<1].add_lazy = tre[rt<<1|1].add_lazy = 0;
// t[rt].set_lazy = 0;
// return ;
// }
// // if(tre[rt].add_lazy) {
// // tre[rt<<1].add_lazy += tre[rt].add_lazy;
// // tre[rt<<1].sum += (tre[rt<<1].r - tre[rt<<1].l + 1) * tre[rt].add_lazy;
// // tre[rt<<1].max += tre[rt].add_lazy;
// // tre[rt<<1].min += tre[rt].add_lazy;
// // tre[rt<<1|1].add_lazy += tre[rt].add_lazy;
// // tre[rt<<1|1].sum += (tre[rt<<1|1].r - tre[rt<<1|1].l + 1) *
// // tre[rt].add_lazy;
// // tre[rt<<1|1].max += tre[rt].add_lazy;
// // tre[rt<<1|1].min += tre[rt].add_lazy;
// // tre[rt].add_lazy = 0;
// // }
// } void build(int rt,int l ,int r){
t[rt].l = l, t[rt].r = r;
//t[rt].set_lazy = 1; if(l == r){
t[rt].sum = t[rt].maxx = a[l];
t[rt].miax = -;
t[rt].set_lazy = ;
// cout <<"nbb " <<t[rt].sum << " "<<rt << endl;
return;
}
int mid = (l + r) >> ;
build(rt <<,l,mid);
build(rt << |,mid + ,r);
push_up(rt);
} void up_date(int rt,int l,int r,int d){
if(d >= t[rt].maxx) return; //push_down(rt);
if(l <= t[rt].l && r >= t[rt].r && d > t[rt].miax){
// t[rt].sum = (t[rt].r - t[rt].l + 1) * d;
// t[rt].maxx = d;
// t[rt].minn = d;
// t[rt].set_lazy = d;
dec_tag(rt,d);
return;
}
push_down(rt);
int mid = (t[rt].l + t[rt].r) >> ;
if(r <= mid) {
up_date(rt<<,l,r,d);
} else if(l > mid) {
up_date(rt<<|,l,r,d);
} else {
up_date(rt<<,l,mid,d);
up_date(rt<<|,mid+,r,d);
}
push_up(rt);
} ll query_sum(int rt,int l,int r) { ///sum if(l <= t[rt].l && t[rt].r <= r) {
return t[rt].sum;
}
push_down(rt);
int mid = (t[rt].l + t[rt].r) >> ;
if(r <= mid) {
return query_sum(rt<<,l,r);
} else if(l > mid) {
return query_sum(rt<<|,l,r);
} else {
return query_sum(rt<<,l,mid) + query_sum(rt<<|,mid+,r);
}
} int query_max(int rt,int l,int r) { ///max //cout << t[rt].maxx << endl;
if(l <= t[rt].l && t[rt].r <= r) {
return t[rt].maxx;
}
push_down(rt);
int mid = (t[rt].l + t[rt].r) >> ;
if(r <= mid) {
return query_max(rt<<,l,r);
} else if(l > mid) {
return query_max(rt<<|,l,r);
} else {
return max(query_max(rt<<,l,mid), query_max(rt<<|,mid+,r));
}
}
// int query_min(int rt,int l,int r) { ///min
// push_down(rt); // if(l <= t[rt].l && t[rt].r <= r) {
// return t[rt].minn;
// }
// int mid = (t[rt].l + t[rt].r) >> 1;
// if(r <= mid) {
// return query_min(rt<<1,l,r);
// } else if(l > mid) {
// return query_min(rt<<1|1,l,r);
// } else {
// return min(query_min(rt<<1,l,mid), query_min(rt<<1|1,mid+1,r));
// }
// } int main(int argc, char const *argv[])
{
int t;
//scanf("%d",&t);
t = read();
while(t--){
int n , q;
//scanf("%d%d",&n,&q);
n = read(),q = read();
for(int i = ;i <= n; i++) //scanf("%d",&a[i]);
a[i] = read();
build(,,n);
// //0 cout << 1;
// for(int i = 1;i <= n; i++){
// cout << t[i].maxx ;
// }
while(q--){
int a,b,c;
//scanf("%d%d%d",&a,&b,&c);
a = read() ,b = read() , c = read();
if(a == ){
int z;
//scanf("%d",&z);
z = read(); up_date(,b,c,z);
}
else if(a == ){
printf("%d\n",query_max(,b,c));
}
else{
printf("%lld\n",query_sum(,b,c));
}
}
}
return ;
}

Gorgeous Sequence(线段树)的更多相关文章

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

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

  2. HDU 5306 Gorgeous Sequence[线段树区间最值操作]

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDOJ 5306 Gorgeous Sequence 线段树

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

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

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

  5. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  6. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  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. hdu4893Wow! Such Sequence! (线段树)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  9. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  10. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

随机推荐

  1. POJ1523 SPF 单点故障

    POJ1523 题意很简单,求删除割点后原先割点所在的无向连通图被分成了几个连通部分(原题说prevent at least one pair of available nodes from bein ...

  2. Linux相关TCP参数优化: proc/sys/net/ipv4/ 提高web质量

    tcp_wmem(3个INTEGER变量): min, default, max min:为TCP socket预留用于发送缓冲的内存最小值.每个tcp socket都可以在建议以后都可以使用它.默认 ...

  3. 不同地区Android开发者使用哪些设备测试APP?

    我们的团队密切关注着移动世界的趋势,以便可以提供所有有关变化的最紧密和最重要的信息.春天恰好是对app进行新一轮测试并检查其与不同Android设备兼容性如何的最佳时机.下面让我们一起来看看全世界范围 ...

  4. HDU-6703 array

    Description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of the arr ...

  5. Nginx动静分离-tomcat

    一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...

  6. Java中高级面试题(1)

    List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...

  7. HDU 6438 Buy and Resell

    高卖低买,可以交易多次 维护一个优先队列,贪心 相当于每天卖出 用当前元素减优先队列最小得到收益 用0/卖出,1/买入标志是否真实进行了交易,记录次数 #include<bits/stdc++. ...

  8. [CSP-S模拟测试]:炼金术士的疑惑(模拟+数学+高斯消元)

    题目传送门(内部题70) 输入格式 第一行一个正整数$n$,表示炼金术士已知的热化学方程式数量.接下来$n$行,每行一个炼金术士已知的热化学方程式.最后一行一个炼金术士想要求解的热化学方程式,末尾记为 ...

  9. HTML-空格字符实体

      不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只 ...

  10. VMware 虚拟化编程(15) — VMware 虚拟机的恢复方案设计

    目录 目录 前文列表 将已存在的虚拟机恢复到指定时间点 恢复为新建虚拟机 灾难恢复 恢复细节 恢复增量备份数据 以 RDM 的方式创建虚拟磁盘 创建虚拟机 Sample of VirtualMachi ...