Code:

#include <bits/stdc++.h>
#define N 50001
#define ll long long
#define lson now<<1
#define rson now<<1|1
#define inf 1000000000
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
ll A[N];
struct Matrix
{
ll a[3][3];
ll*operator[](int x) { return a[x]; }
void re() { memset(a,0,sizeof(a));}
void Min() { for(int i=0;i<3;++i) for(int j=0;j<3;++j) a[i][j]=-inf; }
ll getmax() { return max(a[0][1], a[2][1]); }
}t[N<<2];
Matrix operator*(Matrix a,Matrix b)
{
int i,j,k;
Matrix c; c.Min();
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)for(k=0;k<3;++k) c[i][j]=max(c[i][j],a[i][k]+b[k][j]);
}
return c;
}
void pushup(int l,int r,int now)
{
t[now]=t[lson];
int mid=(l+r)>>1;
if(r>mid) t[now]=t[now]*t[rson];
}
void build(int l,int r,int now)
{
if(l==r)
{
t[now][0][0]=t[now][0][1]=t[now][2][0]=t[now][2][1]=A[l];
t[now][0][2]=t[now][1][0]=t[now][1][2]=-inf;
t[now][1][1]=t[now][2][2]=0;
return;
}
int mid=(l+r)>>1;
if(l<=mid) build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
pushup(l,r,now);
}
Matrix qmax(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R) return t[now];
int mid=(l+r)>>1;
Matrix re;
if(L<=mid)
{
re=qmax(l,mid,lson,L,R);
if(R>mid) re=re*qmax(mid+1,r,rson,L,R);
}
else
{
re=qmax(mid+1,r,rson,L,R);
}
return re;
}
void update(int l,int r,int now,int p,int v)
{
if(l==r)
{
A[p]=v;
t[now][0][0]=t[now][0][1]=t[now][2][0]=t[now][2][1]=v;
return;
}
int mid=(l+r)>>1;
if(p<=mid) update(l,mid,lson,p,v);
else update(mid+1,r,rson,p,v);
pushup(l,r,now);
}
int main()
{
// setIO("input");
int n,q,i,j;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%lld",&A[i]);
build(1,n,1);
scanf("%d",&q);
for(i=1;i<=q;++i)
{
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op==0)
{
update(1,n,1,l,r);
}
else
{
if(l>r) swap(l,r);
printf("%lld\n",qmax(1,n,1,l,r).getmax());
}
}
return 0;
}

  

SP1716 GSS3(线段树+矩阵乘法)的更多相关文章

  1. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  2. hdu 5068(线段树+矩阵乘法)

    矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...

  3. 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)

    题意 ​ 题目链接:https://www.luogu.org/problem/P4150 ​ 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...

  4. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

  5. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

  6. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  7. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

  8. HDU 5068 Harry And Math Teacher 线段树+矩阵乘法

    题意: 一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条). 有两种操作: 0 x y : 输出第x层到第y层的路径数量. 1 x y z : 改变第x层 的 y门 ...

  9. [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]

    这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...

随机推荐

  1. matplotlib笔记2

    颜色和样式 八种内建默认颜色缩写b:blue g:green r:red c:cyan m:magenta y:yellow k:black w:white其它颜色表示方法可以参照百度给的值https ...

  2. Go语言学习笔记(7)——函数和方法

    Go语言中同时有函数和方法! 函数: go程序必须要包含一个main函数.main函数不能有任何参数和返回值! 1. 定义方法示例: func max(num1, num2 int) int { // ...

  3. T100——错误信息提示传入参数显示

    LET l_str1 = l_xccc.xccc901LET l_str2 = l_inat015LET l_str = l_str1.trim(),'|',l_str2.trim() CALL cl ...

  4. 剑指offer(5)——从尾到头打印链表

    题目: 输入一个链表的头结点,从尾到头反过来打印出每个结点的值.结点定义如下: public class ListNode { int val; ListNode next = null; ListN ...

  5. MySQL的explain语句分析

    +----+-------------+-------+------------+------+---------------+-----+---------+------+------+------ ...

  6. poj 2406 求最短重复字串

    题解: KMP中next数组的巧妙运用.在这里我们假设这个字符串的长度是len,那么如果len可以被len-next[len]整除的话,我们就可以说len-next[len]就是那个最短子串的长度为什 ...

  7. C#特性 详解

    一:Conditional:条件特性,预定义了一个条件方法. 使用方法: [Conditional("DEBUG")] public void test() { MessageBo ...

  8. Apache Log View 5.37破解笔记

    i春秋作家:Sp4ce 之前说过要分享这个日志分析工具[记一次简单的攻击日志分析]的破解版,在破解的路上踩了几个坑,记录分享下. 0×00程序概述 原程序 大小: 2283672 字节文件版本: 5. ...

  9. GC案例

    FGC----jmap -histo:live导致 线上某服务的老年代配置了CMS,但却在gc.log发现连续Full GC的问题.JVM参数配置如下: -XX:+UseCMSInitiatingOc ...

  10. nohub

    nohup command > myout.file 2>&1 & nohup command > /dev/null 2>&1 &