SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#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(水题,记录区间第一大和第二大数)的更多相关文章
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- codeforces 577B B. Modulo Sum(水题)
题目链接: B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 702A A. Maximum Increase(水题)
题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...
- openjudge dp水题记录
当发现自己竟然不会打dp的时候内心是崩溃的,然后按照一年前的刷题记录刷openjudge,然后发现自己准确率比一年前(刚学信竞两个月时)的准确率低得多,已经没救. 列一下最近打的几道sb题 2985: ...
- Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)
求不重复的子串个数 用所有的减去height就好了 推出来的... #include <iostream> #include <cstdio> #include <sst ...
- SPOJ - AMR11H Array Diversity (水题排列组合或容斥)
题意:给定一个序列,让你求两种数,一个是求一个子序列,包含最大值和最小值,再就是求一个子集包含最大值和最小值. 析:求子序列,从前往记录一下最大值和最小值的位置,然后从前往后扫一遍,每个位置求一下数目 ...
- Codeforces Round #599 (Div. 2) A. Maximum Square 水题
A. Maximum Square Ujan decided to make a new wooden roof for the house. He has
- NOIP前的水题记录
CF147B Smile House 二分+矩阵快速幂,注意一下储存矩阵相乘结果的矩阵,初始化时,a[i][i]=-inf(而其他都可以a[i][i]=0,为了保证答案的可二分性). CF715B C ...
随机推荐
- Qt获得网页源码
1.工程中添加网络模块 打开你的.pro文件插入以下代码 QT += network 2.添加代码 CodeQString NetWork::getWebSource(QUrl url) { QNet ...
- VC6.0编译boost
今天学习了下VC6.0下boost的编译,只是对regex进行了编译,据说全部编译需要2个多小时,在此记录下学习过程中遇到的问题以便今后查看. 最开始直接从网上(www.boost.org)下载了当前 ...
- Makefile中=、:=、+=、?=的区别
=,就是基本的赋值 :=,覆盖原来的值 +=,添加新值 ?=,如果没有赋值则赋值
- 《Linux系统 date、cal、hwclock时间命令的用法》
date命令的用法: [root@apache ~]# date //查看当前系统的时间 Sat Jun 14 13:46:02 CST 2014 [root@apache ~]# date -s & ...
- 使用MongoDB的开源项目
根据谷歌的搜索结果筛选出来的. 统计应用 counlty https://count.ly/ mongopress 开源CMS系统 http://www.mongopress.org/ Rubedo ...
- Win7下MongoDB安装
一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...
- Stanford parser学习:LexicalizedParser类分析
上次(http://www.cnblogs.com/stGeekpower/p/3457746.html)主要是对应于javadoc写了下LexicalizedParser类main函数的功能,这次看 ...
- (转载)delphi文件流
delphi文件流 [复制链接] 在Delphi中,所有流对象的基类为TStream类,其中定义了所有流的共同属性和方法. TStream类中定义的属性介绍如下: 1.Size: 此属性以字节返回流中 ...
- .NET开发之窗体间的传值转化操作
DOTNET开发之窗体间的传值转化操作 好想把自己最近学到的知识写下来和各位朋友分享,也希望得到大神的指点.今天终于知道自己要写点什么,就是关于WPF开发时简单的界面传值与简单操作. 涉及两个界面:一 ...
- 1066. Root of AVL Tree (25)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...