题目链接

传送门

题面

思路

对于线段树的每个结点我们存这个区间的最大值\(mx\)、最大值个数\(cnt\)、严格第二大数\(se\),操作\(0\):

  • 如果\(mx\leq val\)则不需要更新改区间;
  • 如果\(se\leq val<mx\)则只需将区间最大值进行更新,此时\(sum=sum-cnt\times (mx - val)\);
  • 如果\(val<se\)则递归下去。

    (详情请看吉老师\(ppt\)

    因为一开始加了个\(lazy\)标记导致情况复杂化,且不好处理,对拍好久才发现。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1000000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int t, n, q, op, l, r, x;
int a[maxn]; struct node {
int l, r, mx, se, cnt;
LL sum;
}segtree[maxn<<2]; void push_up(int rt) {
if(segtree[lson].mx >= segtree[rson].mx) {
segtree[rt].mx = segtree[lson].mx;
if(segtree[lson].mx == segtree[rson].mx) {
segtree[rt].cnt = segtree[lson].cnt + segtree[rson].cnt;
segtree[rt].se = max(segtree[lson].se, segtree[rson].se);
} else {
segtree[rt].cnt = segtree[lson].cnt;
segtree[rt].se = max(segtree[lson].se, segtree[rson].mx);
}
} else {
segtree[rt].mx = segtree[rson].mx;
segtree[rt].cnt = segtree[rson].cnt;
segtree[rt].se = max(segtree[lson].mx, segtree[rson].se);
}
segtree[rt].sum = segtree[lson].sum + segtree[rson].sum;
} void push_down(int rt) {
if(segtree[lson].mx > segtree[rt].mx) {
segtree[lson].sum -= 1LL * segtree[lson].cnt * (segtree[lson].mx - segtree[rt].mx);
segtree[lson].mx = segtree[rt].mx;
}
if(segtree[rson].mx > segtree[rt].mx) {
segtree[rson].sum -= 1LL * segtree[rson].cnt * (segtree[rson].mx - segtree[rt].mx);
segtree[rson].mx = segtree[rt].mx;
}
} void build(int rt, int l, int r) {
segtree[rt].l = l, segtree[rt].r = r;
segtree[rt].cnt = 0;
segtree[rt].se = -inf;
if(l == r) {
scanf("%d", &segtree[rt].mx);
segtree[rt].cnt = 1;
segtree[rt].sum = segtree[rt].mx;
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid);
build(rson, mid + 1, r);
push_up(rt);
} void update(int rt, int l, int r, int x) {
if(segtree[rt].l == segtree[rt].r) {
if(x < segtree[rt].mx) {
segtree[rt].mx = x;
segtree[rt].sum = x;
}
return;
}
if(segtree[rt].mx <= x) {
return;
}
if(segtree[rt].l == l && segtree[rt].r == r && segtree[rt].se < x) {
segtree[rt].sum -= 1LL * segtree[rt].cnt * (segtree[rt].mx - x);
segtree[rt].mx = x;
return;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) update(lson, l, r, x);
else if(l > mid) update(rson, l, r, x);
else {
update(lson, l, mid, x);
update(rson, mid + 1, r, x);
}
push_up(rt);
} int query1(int rt, int l, int r) {
if(segtree[rt].l == l && segtree[rt].r == r) {
return segtree[rt].mx;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query1(lson, l, r);
else if(l > mid) return query1(rson, l, r);
else return max(query1(lson, l, mid), query1(rson, mid + 1, r));
} LL query2(int rt, int l, int r) {
if(segtree[rt].l == l && segtree[rt].r == r) {
return segtree[rt].sum;
}
push_down(rt);
int mid = (segtree[rt].l + segtree[rt].r) >> 1;
if(r <= mid) return query2(lson, l, r);
else if(l > mid) return query2(rson, l, r);
else return query2(lson, l, mid) + query2(rson, mid + 1, r);
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &q);
build(1, 1, n);
while(q--) {
scanf("%d%d%d", &op, &l, &r);
if(op == 0) {
scanf("%d", &x);
update(1, l, r, x);
} else if(op == 1) {
printf("%d\n", query1(1, l, r));
} else {
printf("%lld\n", query2(1, l, r));
}
}
}
return 0;
}

\(ps.\)在这里贴几组数据帮助大家找\(bug\):

Input

4

3 3

1167335444 1577370753 1848018061

0 1 1 577330338

0 2 2 25842012

0 1 2 2081289238

13 4

2092509202 227315181 749615568 1128285623 1865077425 1779921231 1864459374 2072421312 1354378672 20493878 1571784125 1812319171 1767594153

0 1 5 1790650736

0 1 13 1584744642

2 8 8

2 1 1

5 5

206578960 2138572088 1531732505 476202306 1864171007

1 1 1

0 2 2 159050728

1 1 3

1 2 3

2 1 2

7 1

243178151 1437281627 1355768485 1346835035 87676247 1491584559 2023149422

1 4 6

Output

1584744642

1584744642

206578960

1531732505

1531732505

365629688

1491584559

Input

1

3 5

1281319710 961042073 1775161183

0 1 3 1126944798

1 3 3

2 1 2

1 1 1

0 2 2 339585676

Output

1126944798

2087986871

1126944798

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

  1. Gorgeous Sequence(线段树)

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

  2. 【HDU5306】【DTOJ2481】Gorgeous Sequence【线段树】

    题目大意:给你一个序列a,你有三个操作,0: x y t将a[x,y]和t取min:1:x y求a[x,y]的最大值:2:x y求a[x,y]的sum 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...

  3. HDU 4893 Wow! Such Sequence! (线段树)

    Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...

  4. HDU 5828 Rikka with Sequence (线段树)

    Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  5. Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

  6. 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)

    题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...

  7. ACdream 1427—— Nice Sequence——————【线段树单点更新,区间查询】

    Nice Sequence Time Limit: 4000/2000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) Su ...

  8. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

    E. Correct Bracket Sequence Editor   Recently Polycarp started to develop a text editor that works o ...

  9. HDU 6047 Maximum Sequence(贪心+线段树)

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

随机推荐

  1. git worktree 目录修复

    三种方式挨个尝试,1不行用2 2不行用3 1.拉取阶段失败 git worktree add -f -B xxx_branch ./xxx_branch origin/xxx_branch 强制拉取 ...

  2. OpenVAS开源风险评估系统部署方案

    OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessus“本是同根生”,在Nessus商业化之后仍 ...

  3. 2019广东外语外贸大学CTF新手赛-密码学-RSA题解

    题面 n=100000463700003241 e=17 密文: 分析: 题面已明示是RSA加密,已公开n与公钥e,n为1e18内的数字(64位).要爆破RSA,显然是先分析n的值. n的值是由两个素 ...

  4. Java IO 与 NIO 服务器&客户端通信小栗子

    本篇包含了入门小栗子以及一些问题的思考 BIO package com.demo.bio; import java.io.*; import java.net.ServerSocket; import ...

  5. 基于FPGA Manager的Zynq PL程序写入方案

    本文主要描述了如何在Linux系统启动以后,在线将bitstream文件更新到ZYNQ PL的过程及方法.相关内容主要译自xilinx-wiki,其中官网给出了两种方法,分别为Device Tree ...

  6. Source Insight 4.0相对路径的设置

    比如在D盘有个51的Firmware工程 里面有几个文件夹存放程序文件,项目文件在Project文件夹里,也就是整个Firmware里的文件都是有用的. Keil项目文件位置. 到这里就可以双击Tem ...

  7. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  8. 将 C++/WinRT 中的线程切换体验带到 C# 中来(WPF 版本)

    原文:将 C++/WinRT 中的线程切换体验带到 C# 中来(WPF 版本) 如果你要在 WPF 程序中使用线程池完成一个特殊的任务,那么使用 .NET 的 API Task.Run 并传入一个 L ...

  9. .net core中的Session以及HttpContext对象使用小结

    session用于识别用户并保持用户信息,就是一个会话 ,在浏览器不关闭的前提下,可以保存用户的信息,比如登录的保存用户信息从一个网页跳转到另一个网页,你的用户信息就可以用session. .net ...

  10. MVC学习笔记(二)—用EF创建数据库

    1.创建一个空项目 2.在项目中创建EFCore的类库 3.在NuGut控制台为EFCore项目中安装entity  3.1 命令为:Install-Package EntityFramework(在 ...