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. Nginx反向代理 Laravel获取真实IP地址(PHP)

    使用VUE前后端分离开发 后端使用Laravel  想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...

  2. Laravel -- Blade模板

    {{--流程控制--}} @if($name == '1') this is 1 @elseif($name == '2') this.is 2 @else who am i? @endif @for ...

  3. dns文件

    1.dns简介 dns为域名解析系统,当本地浏览器输入域名访问网站时,如果本地host中没有配置域名与IP的对应关系,那么域名信息将会被发送到dns服务器上,由dns服务器将域名解析为IP(过程较为复 ...

  4. Python3爬虫(八) 数据存储之TXT、JSON、CSV

    Infi-chu: http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台.但是不利于检索. 1.举例: 使用requests ...

  5. 43-Identity MVC:UI

    1-打开之前写的MvcCookieAuthSample项目, 在AccountController新加Register,Login方法 public class AccountController : ...

  6. Linux系统下安装rz/sz命令

    执行命令 yum install -y lrzsz rz -be本地上传文件到服务器

  7. MySQL分区的限制(最多有多少个分区)

    MySQL分区的限制 •   只能对数据表的整型列进行分区,或者数据列可以通过分区函数转化成整型列 •   最大分区数目不能超过1024 •   如果含有唯一索引或者主键,则分区列必须包含在所有的唯一 ...

  8. c/c++ 随机数生成

    当程序需要一个随机数时有两种情况下使用: 1.程序中只需使用一次随机数,则调用rand()函数即可 2.程序需要多次使用随机数,那么需要使用srand()函数生成随机数种子在调用rand()函数保证每 ...

  9. bzoj 一些题目汇总

    2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...

  10. springmvc 处理put,delete请求

    前言:ajax用post编辑,删除提示越权操作状态为500,修改半晌最后大神指点说是:type修改为post和delete模式 最后还是一知半解,但是程序却正常使用了.当然注意我用的mvc,contr ...