picks loves segment tree I

题目背景

来源:

\(\text {2018 WC Segment Tree Beats}\)

原作者:

\(\text {C_SUNSHINE}\)

\(\text {jiry_2}\)

题目描述:

  • 给定一个长度为\(n\)的数列\(A\),接下来有\(m\)次操作:
  • 区间\([l,r]\)中的所有数变成\(min(A_i,x)\)
  • 询问区间\([l,r]\)中所有数的和
  • \(n,m \le 50000\)
  • 我会分块!
  • \(n,m \le 500000\)
  • 线段树?

输入输出格式

输入格式

第一行两个正整数表示\(N,M\)

第二行\(N\)个正整数,表示数列\(A_i\)

接下来\(M\)行每行包含\(3\)或\(4\)个整数,表示一个操作,具体如下:

操作1: 1 x y x 含义:将区间\([l,r]\)内每个数变成\(min(A_i,x)\)

操作2: 2 x y 含义:输出区间\([l,r]\)内每个数的和

输出格式

输出包含若干行整数,即为所有操作\(2\)的结果。

说明:

对于所有的数据,有\(N \le 500000,M \le 500000,a_i \le 2 \times 10^9\)

保证所有出现的数据在\(int64/long \ long\)范围内


本菜鸡说不清楚,直接当板子了,看网上的dalao们都写的Ⅴ,Ⅵ,Ⅶ,Ⅸ什么的,我太菜,从基础开始来吧。


Code:

#include <cstdio>
#define ll long long
#define ls id<<1
#define rs id<<1|1
const int N=5e5+10;
ll mx[N<<2],se[N<<2],sum[N<<2],tag[N<<2],a[N];
ll max(ll x,ll y){return x>y?x:y;}
int cnt[N<<2],n,m;
void updata(int id)
{
if(mx[ls]>mx[rs])
{
mx[id]=mx[ls];
cnt[id]=cnt[ls];
se[id]=max(se[ls],mx[rs]);
}
else if(mx[ls]<mx[rs])
{
mx[id]=mx[rs];
cnt[id]=cnt[rs];
se[id]=max(mx[ls],se[rs]);
}
else
{
mx[id]=mx[ls];
cnt[id]=cnt[ls]+cnt[rs];
se[id]=max(se[ls],se[rs]);
}
sum[id]=sum[ls]+sum[rs];
}
void build(int id,int l,int r)
{
if(l==r)
{
sum[id]=mx[id]=a[l];
cnt[id]=1;
se[id]=0;
return;
}
int mid=l+r>>1;
build(ls,l,mid),build(rs,mid+1,r);
updata(id);
}
void pushdown(int id)
{
if(tag[id])
{
if(mx[ls]>tag[id])
{
sum[ls]-=(mx[ls]-tag[id])*cnt[ls];
tag[ls]=mx[ls]=tag[id];
}
if(mx[rs]>tag[id])
{
sum[rs]-=(mx[rs]-tag[id])*cnt[rs];
tag[rs]=mx[rs]=tag[id];
}
tag[id]=0;
}
}
void change(int id,int L,int R,int l,int r,ll x)
{
if(mx[id]<=x) return;
if(L==l&&R==r&&se[id]<x)
{
sum[id]-=(mx[id]-x)*cnt[id];
tag[id]=mx[id]=x;
return;
}
pushdown(id);
int Mid=L+R>>1;
if(r<=Mid) change(ls,L,Mid,l,r,x);
else if(l>Mid) change(rs,Mid+1,R,l,r,x);
else change(ls,L,Mid,l,Mid,x),change(rs,Mid+1,R,Mid+1,r,x);
updata(id);
}
ll query(int id,int L,int R,int l,int r)
{
if(l==L&&r==R) return sum[id];
pushdown(id);
int Mid=L+R>>1;
if(r<=Mid) return query(ls,L,Mid,l,r);
else if(l>Mid) return query(rs,Mid+1,R,l,r);
else return query(ls,L,Mid,l,Mid)+query(rs,Mid+1,R,Mid+1,r);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",a+i);
build(1,1,n);
ll x;
for(int op,l,r,i=1;i<=m;i++)
{
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
scanf("%lld",&x);
change(1,1,n,l,r,x);
}
else
printf("%lld\n",query(1,1,n,l,r));
}
return 0;
}

2018.10.13

picks loves segment tree I的更多相关文章

  1. 2018.07.29~30 uoj#170. Picks loves segment tree VIII(线段树)

    传送门 线段树好题. 维护区间取两种最值,区间加,求区间两种历史最值,区间最小值. 自己的写法调了一个晚上+一个上午+一个下午+一个晚上并没有调出来,90" role="prese ...

  2. 题解-hzy loves segment tree I

    Problem 题目概要:给定一棵 \(n\) 个节点的树,点有点权,进行 \(m\) 次路径取\(\max\)的操作,最后统一输出点权 \(n\leq 10^5,m\leq 5\times 10^6 ...

  3. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  4. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  5. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  6. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  7. Segment Tree Query I & II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  8. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  9. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

随机推荐

  1. BC追踪

    项目又要开始改造了,记录一下改造过程中碰到的坑和解决思路,避免以后回头看看自己的笔记都不知道写了什么. (一)敏感信息混淆 (二)活用ComponentScan (三)Swagger配置多项目共用 ( ...

  2. PHP计算两个时间戳之间间隔时分秒

    /功能:计算两个时间戳之间相差的日时分秒//$begin_time 开始时间戳//$end_time 结束时间戳function timediff($begin_time,$end_time){ if ...

  3. JavaSE 第二次学习随笔(四)

    ---------------------------------------------------------------------------------------------------- ...

  4. Python学习之property

    Python中使用Property函数可以将类中的函数当作属性来调用. 案例 __metaclass__=type class Rectangle: def __init__(self): self. ...

  5. 12、K最近邻算法(KNN算法)

    一.如何创建推荐系统? 找到与用户相似的其他用户,然后把其他用户喜欢的东西推荐给用户.这就是K最近邻算法的分类作用. 二.抽取特征 推荐系统最重要的工作是:将用户的特征抽取出来并转化为度量的数字,然后 ...

  6. 利尔达NB-IOT的PSM和eDRX低功耗模式笔记

    1. NB-IOT的技术优势,广覆盖,NB-IOT与GPRS和LTE相比较,最大链路预算提升了20dB,相当于提升了100倍,即使在地车车库.地下室.地下管道等普通无线网络信号难以到达的地方也容易覆盖 ...

  7. 破解PHPStrom 10 and Pycharm

    注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...

  8. 使用JDK自带的keytool工具生成证书

    一.keytool 简介 keytool 是java用于管理密钥和证书的工具,它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及 ...

  9. Spotlight on MySQL

    聚光灯在MySQL 1.Sessios会话Total Users:总用户数前连接到MySQL服务器的用户会话总数Active Users:活跃用户此控件表示连接到当前正在执行SQL语句或其他数据库请求 ...

  10. HTML5 本地存储Web Storage简单了解

    ​HTML5本地存储规范,定义了两个重要的API :Web Storage  和  本地数据库Web SQL Database. 本地存储Web Storage 实际上是HTML4的cookie存储机 ...