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)的更多相关文章

  1. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

  2. 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 ...

  3. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...

  4. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  5. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. 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 ...

  7. 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 ...

  8. 刷题向》一道简单的思路题BZOJ1800(EASY+)

    这道题其实并不难,主要原因是数据范围很小,当然数据如果大来也可以优化,但重点是在做的时候用的思路很通用, 所以本题是一道思想题(当然思想也不难) 标题里的“+”体现在一些边界处理中. 直接甩题目 De ...

  9. 值得一做》关于一道暴搜BZOJ1024(EASY+)

    为什么要写这道题的DP捏? 原因很简单,因为为原来在openjudge上有一道题叫分蛋糕,有一个思路和这道题很像:“分锅”. 分锅:即为考虑计算当前情况的最优解时,把当前状态结果,分散为考虑当前状态的 ...

随机推荐

  1. Java 集合和泛型

    一.集合(Collections) Java使用集合来组织和管理对象. 1.Java的集合类 集合类主要负责保存.盛装和管理对象,因此集合类也被称为容器类. 集合类分为Set.List.Map和Que ...

  2. vb.net DBEntities框架联表查询 Join

    在项目中配置好DBEntities 使用两个表:主表Table, 子表Table_Item 主要是用到了委托和泛型,ForEach用的是不带返回值的委托 Sub GetDb() Dim st As N ...

  3. C#Linq之求和,平均值,最大值,最小值

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. java.sql.SQLException: Could not retrieve transaction read-only status from server 问题解决

    网上有2种主要说法 第一种 问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报:          java.sql.SQLException: could not ret ...

  5. nginx 配置反向代理根目录到其他服务器

    location /detail/json { if ( $uri = "/detail/json" ) { rewrite "/detail/json" /i ...

  6. 非JAVA WEB项目提供Http接口调用实现

    package com.monitor.app.utils; import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import ...

  7. TensorFlow中CNN的两种padding方式“SAME”和“VALID”

    来源 dilation_rate为一个可选的参数,默认为1,这里我们可以先不管它. 整理一下,对于"VALID",输出的形状计算如下: new_height=new_width=⌈ ...

  8. Linux 下vim命令详解

    原博文:https://www.cnblogs.com/zknublx/p/6058679.html 高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx   ...

  9. Flink 1.0到1.9特性

    Flink API 历史变迁 在 Flink 1.0.0 时期,加入了 State API,即 ValueState.ReducingState.ListState 等等.State API 可以认为 ...

  10. three.js之创建一条直线

    <!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>My first thr ...