7.9T2EASY(easy)
EASY(easy)






sol:非常经典的题,取了一次之后,把线段树上这一段变成相反数 然后再贪心取和最大的。 重复以上操作,发现最后一定有对应的解,且根据贪心过程一定 是最大的 线段树上维护区间和最大/小及位置,左/右连续最大/小及位置, 取反标记
除了写起来特别麻烦之外都还好
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N];
#define Mp make_pair
#define c1 (x<<1)
#define c2 (x<<1|1)
struct Record{int l,r,Zhi;};
inline Record min(Record a,Record b) {return (a.Zhi<b.Zhi)?a:b;}
inline Record max(Record a,Record b) {return (a.Zhi>b.Zhi)?a:b;}
struct Node
{
pair<int,int>Mxl,Mnl,Mxr,Mnr;
Record Mx,Mn;
int Sum,Rev;
}T[N<<];
inline void Rev(Node &b)
{
swap(b.Mnl,b.Mxl); swap(b.Mnr,b.Mxr); swap(b.Mn,b.Mx);
b.Mnl.first*=-; b.Mnr.first*=-; b.Mn.Zhi*=-;
b.Mxl.first*=-; b.Mxr.first*=-; b.Mx.Zhi*=-;
b.Sum*=-; b.Rev^=;
}
inline void Down(int x)
{
if(!T[x].Rev) return;
Rev(T[c1]); Rev(T[c2]); T[x].Rev^=;
}
inline Node Merg(Node b1,Node b2)
{
Node Res;
Res.Rev=;
Res.Mnl=min(b1.Mnl,Mp(b1.Sum+b2.Mnl.first,b2.Mnl.second));
Res.Mxl=max(b1.Mxl,Mp(b1.Sum+b2.Mxl.first,b2.Mxl.second));
Res.Mnr=min(b2.Mnr,Mp(b2.Sum+b1.Mnr.first,b1.Mnr.second));
Res.Mxr=max(b2.Mxr,Mp(b2.Sum+b1.Mxr.first,b1.Mxr.second));
Res.Mn=min((Record){b1.Mnr.second,b2.Mnl.second,b1.Mnr.first+b2.Mnl.first},min(b1.Mn,b2.Mn));
Res.Mx=max((Record){b1.Mxr.second,b2.Mxl.second,b1.Mxr.first+b2.Mxl.first},max(b1.Mx,b2.Mx));
Res.Sum=b1.Sum+b2.Sum;
return Res;
}
inline void Build(int x,int l,int r)
{
if(l==r)
{
T[x].Mnl=T[x].Mxl=T[x].Mnr=T[x].Mxr=Mp(a[l],l);
T[x].Mn=T[x].Mx=(Record){l,l,a[l]};
T[x].Sum=a[l]; T[x].Rev=;
return;
}
int mid=(l+r)>>;
Build(c1,l,mid); Build(c2,mid+,r);
T[x]=Merg(T[c1],T[c2]);
}
inline void Change(int x,int l,int r,int Pos,int Val)
{
if(l==r)
{
T[x].Mnl=T[x].Mxl=T[x].Mnr=T[x].Mxr=Mp(Val,l);
T[x].Mn=T[x].Mx=(Record){l,l,Val};
T[x].Sum=Val;
return;
}
Down(x);
int mid=(l+r)>>;
if(Pos<=mid) Change(c1,l,mid,Pos,Val);
else Change(c2,mid+,r,Pos,Val);
T[x]=Merg(T[c1],T[c2]);
}
inline void Reverse(int x,int l,int r,int ql,int qr)
{
if(l==ql&&r==qr) {Rev(T[x]); return;}
int mid=(l+r)>>;
Down(x);
if(qr<=mid) Reverse(c1,l,mid,ql,qr);
else if(ql>mid) Reverse(c2,mid+,r,ql,qr);
else Reverse(c1,l,mid,ql,mid),Reverse(c2,mid+,r,mid+,qr);
T[x]=Merg(T[c1],T[c2]);
}
inline Node Query(int x,int l,int r,int ql,int qr)
{
if(l==ql&&r==qr) return T[x];
int mid=(l+r)>>;
Down(x);
if(qr<=mid) return Query(c1,l,mid,ql,qr);
else if(ql>mid) return Query(c2,mid+,r,ql,qr);
else
{
Node b1=Query(c1,l,mid,ql,mid),b2=Query(c2,mid+,r,mid+,qr);
return Merg(b1,b2);
}
}
int tot=;
pair<int,int>Ans[];
int main()
{
freopen("easy.in","r",stdin);
freopen("easy.out","w",stdout);
int i,opt,Pos,Val,l,r,k,Sum;
R(n);
for(i=;i<=n;i++) R(a[i]);
R(m);
Build(,,n);
while(m--)
{
R(opt);
if(!opt)
{
R(Pos); R(Val); Change(,,n,Pos,Val);
}
else
{
R(l); R(r); R(k); tot=Sum=;
for(i=;i<=k;i++)
{
Node Res=Query(,,n,l,r);
if(Res.Mx.Zhi<=) break;
Ans[++tot]=Mp(Res.Mx.l,Res.Mx.r);
// printf("%d %d %d\n",Ans[tot].first,Ans[tot].second,Res.Mx.Zhi);
Sum+=Res.Mx.Zhi;
Reverse(,,n,Ans[tot].first,Ans[tot].second);
}
for(i=;i<=tot;i++) Reverse(,,n,Ans[i].first,Ans[i].second);
Wl(Sum);
// return 0;
}
}
return ;
}
/*
input
9
9 -8 9 -1 -1 -1 9 -8 9
3
1 1 9 1
1 1 9 2
1 4 6 3
output
17
25
0 input
15
-4 8 -3 -10 10 4 -7 -7 0 -6 3 8 -10 7 2
15
1 3 9 2
1 6 12 1
0 6 5
0 10 -7
1 4 9 1
1 7 9 1
0 10 -3
1 4 10 2
1 3 13 2
1 4 11 2
0 15 -9
0 13 -9
0 11 -10
1 5 14 2
1 6 12 1
output
14
11
15
0
15
26
18
23
8
*/
7.9T2EASY(easy)的更多相关文章
- leetcode 1.回文数-(easy)
2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
- LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 572. Subtree of Another Tree(easy)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- 刷题向》一道简单的思路题BZOJ1800(EASY+)
这道题其实并不难,主要原因是数据范围很小,当然数据如果大来也可以优化,但重点是在做的时候用的思路很通用, 所以本题是一道思想题(当然思想也不难) 标题里的“+”体现在一些边界处理中. 直接甩题目 De ...
- 值得一做》关于一道暴搜BZOJ1024(EASY+)
为什么要写这道题的DP捏? 原因很简单,因为为原来在openjudge上有一道题叫分蛋糕,有一个思路和这道题很像:“分锅”. 分锅:即为考虑计算当前情况的最优解时,把当前状态结果,分散为考虑当前状态的 ...
随机推荐
- jenkins转换显示语言为中文简体(jenkins汉化)
jenkins版本2.117 单位使用的jenkins一直是英文版本,有同事建议切换为中文版. 以下过程完成转换. 一.安装插件 主界面-->系统管理-->插件管理-->可选插件 图 ...
- C++入门基础知识(一)
一:关键字 在C语言中,我们已经学习过了很多的关键字,例如:static,struct等,下面展现一下C++中的一些关键字. 二:命名空间 在C/C++中,变量.函数和类都是大量存在的,这些变量.函数 ...
- ZOOKEEPER进阶
集群个数: 2n+1,因为集群当宕机大于等于二分之一的机子时,集群选举会失败.故 2n+1台机器和3n台机器可靠性相同 Leader的作用: 为了实现各个节点数据的一致性,需要一个负责协调数据同步的操 ...
- Unable to load the specified metadata resource
本地运行都正常,就是发布到服务器上不行,查找了一些文章,都没解决我的问题,后来发现是路径不对和文件缺失. 原来的配置文件中是这样的: <add name="TRidentityEnti ...
- 解决sql "Compatibility_199_804_30003" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。
关联条件加 COLLATE Compatibility_199_804_30003
- djang部署vue项目
1,将vue项目npm run build 在此之前需要修改打包后的js,css文件路径: 需新建vue.config.js 在文件中添加: module.exports = { // 输出目录 as ...
- hadoop 中ALL Applications 中Tracking 下History查找不到MapReduce Job 日志
运行一个Map Reduce job 想查看日志: 点击History ,找不到网页 解决办法如下: 1.其中有一个进程是需要启动的: Hadoop自带了一个历史服务器,可以通过历史服务器查看已经运行 ...
- Photoshop从入门到精通所有视频教程(43G)以及素材资料免费拿
包含了Photoshop从入门到精通所有需要了解的视频教程资料,并且包含了大量的P图素材. 资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请 ...
- 工作总结 [ActionName("ss123")] 更改路由中Action名称 获取或设置操作的名称
- nginx反向代理局域网访问外网
.配置内网hosts vim /etc/hosts 添加 host1(能连外网的服务器ip) central.maven.org 2.在host1 服务器上nginx配置 server { ...