#include <iostream>
#include <stdio.h>
#include <algorithm>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R
/*
AC
水题
题意:给出n个数,两种操作
U i x 将第i个数改成x
Q x y 查询[x,y]中两个数的和的最大值,这两个数不能为同一个
即只要求该区间第一大和第二大的数,他们的和即为答案
*/
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=;
int n,m; struct Node{
long long m1,m2; //存储该区间第一大的数和第二大的数
}tree[maxn<<]; void pushUp(Node &rt,Node &ls,Node &rs){
if(ls.m1>rs.m1){
rt.m1=ls.m1;
if(ls.m2>=rs.m1)
rt.m2=ls.m2;
else
rt.m2=rs.m1;
}
else if(ls.m1==rs.m1){
rt.m1=rt.m2=ls.m1;
}
else{
rt.m1=rs.m1;
if(rs.m2>=ls.m1)
rt.m2=rs.m2;
else
rt.m2=ls.m1;
}
}
void build(int rt,int L,int R){
if(L==R){
scanf("%lld",&tree[rt].m1);
tree[rt].m2=-INF;
return;
}
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(tree[rt],tree[rt<<],tree[rt<<|]);
}
void update(int rt,int L,int R,int x,long long c){
if(L==R){
tree[rt].m1=c;
tree[rt].m2=-INF;
return;
}
int mid=(L+R)>>;
if(x<=mid)
update(lson,x,c);
else
update(rson,x,c);
pushUp(tree[rt],tree[rt<<],tree[rt<<|]);
} Node query(int rt,int L,int R,int l,int r){
if(l<=L&&R<=r){
return tree[rt];
}
int mid=(L+R)>>;
Node tmp,r1,r2;
if(r<=mid)
tmp=query(lson,l,r);
else if(l>mid)
tmp=query(rson,l,r);
else{
r1=query(lson,l,mid);
r2=query(rson,mid+,r);
pushUp(tmp,r1,r2);
}
return tmp;
}
int main()
{
char str[];
int x,y;
long long v;
scanf("%d",&n);
build(,,n);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%s",str);
if(str[]=='U'){
scanf("%d%lld",&x,&v);
update(,,n,x,v);
}
else{
scanf("%d%d",&x,&y);
Node ans=query(,,n,x,y);
//cout<<ans.m1<<" "<<ans.m2<<endl;
printf("%lld\n",ans.m1+ans.m2);
}
}
return ;
}

SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)的更多相关文章

  1. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  2. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  3. codeforces 577B B. Modulo Sum(水题)

    题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. codeforces 702A A. Maximum Increase(水题)

    题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. openjudge dp水题记录

    当发现自己竟然不会打dp的时候内心是崩溃的,然后按照一年前的刷题记录刷openjudge,然后发现自己准确率比一年前(刚学信竞两个月时)的准确率低得多,已经没救. 列一下最近打的几道sb题 2985: ...

  6. Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)

    求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...

  7. SPOJ - AMR11H Array Diversity (水题排列组合或容斥)

    题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...

  8. Codeforces Round #599 (Div. 2) A. Maximum Square 水题

    A. Maximum Square Ujan decided to make a new wooden roof for the house. He has

  9. NOIP前的水题记录

    CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...

随机推荐

  1. JAVA作业-1

    编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果. import javax.swing.JOptionPane; // import class JOptionPane publ ...

  2. 远程连接postgres,出现server doesnt listen

    已修改pg_hdb.conf中的 # IPv4 local connections:     host    all             all             127.0.0.1/32 ...

  3. 记录一次会话CRT

    记录一次会话CRT --------------------- su -oracle sqlplus / as sysdba sqlplus username/password  如:普通用户登录   ...

  4. C++使用类型代替枚举量

    自己写的C++类型枚举量,可以使用类型识别取代模板,绑定枚举量和多组调用函数,在调用阶段只要指定某组函数就可以根据枚举量调用相应函数. 代码如下: #ifndef __MYENUM_H__ #defi ...

  5. MD5加密方式

    MD5加密是一种安全系数比较高的加密方式,具有不可逆的加密特征,就是很难进行破解,现在对MD5加密进行破解的方式还是采用跑数据库的方式,时间比较长,耗费性能比较大,所以一般的破解都是要收费的. C#中 ...

  6. mobileconfig文件的签名和认证(signed、verified)

    专题:基于IOS上MDM技术相关资料整理及汇总mobileconfig文件的签名和认证(signed.verified) 一.功能描述: 鉴于我们的设备和MDM server之间已经可以通信,并能完成 ...

  7. ASP.NET MVC + EF 利用存储过程读取大数据,1亿数据测试很OK

    看到本文的标题,相信你会忍不住进来看看! 没错,本文要讲的就是这个重量级的东西,这个不仅仅支持单表查询,更能支持连接查询, 加入一个表10W数据,另一个表也是10万数据,当你用linq建立一个连接查询 ...

  8. 2013-07-29 IT 要闻速记快想

    ### ========================= ###传动视暴雪82亿美元赎身,腾讯参与投资 ### ========================= ###帮助企业解决打印&邮 ...

  9. jqueryGrid 内置的onclickSubmit afterSubmit

    $(document).ready(function() { $('#jpgCustomers').jqGrid({ //url from wich data should be requested ...

  10. Delphi ISO 收藏!

    CodeGear RAD Studio 2007 最终版(With Update4) v11.0.2902.10471http://altd.codegear.com/download/radstud ...