求最大值(贪心+差分+线段树)

求最大值

题号:NC14402
时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

给出一个序列,你的任务是求每次操作之后序列中 (a[j]-a[i])/(j-i)【1<=i<j<=n】的最大值。

操作次数有Q次,每次操作需要将位子p处的数字变成y. 

输入描述:

本题包含多组输入,每组输入第一行一个数字n,表示序列的长度。
然后接下来一行输入n个数,表示原先序列的样子。
再接下来一行一个数字Q,表示需要进行的操作的次数。
最后Q行每行两个元素p,y,表示本操作需要将位子p处的数字变成y.
数据范围:
3<=n<=200000
1<=Q<=200000
-1000000000<=a[i]<=1000000000

输出描述:

每组数据输出Q行,每行输出一个浮点数,保留两位小数,表示所求的最大值。

示例1

输入

复制

5
2 4 6 8 10
2
2 5
4 9

输出

复制

3.00
3.00

说明

第一次操作之后的序列变成:2 5 6 8 10
第二次操作之后的序列变成:2 5 6 9 10

备注:

输入只有整形
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}

#define For(i,sta,en) for(int i = sta;i <= en;i++)
#define ls now<<1
#define rs now<<1|1
#define mid (l+r)/2
const int maxn = 2e5+11;
int  t[maxn<<2],n,m,num[maxn];

void build(int now,int l,int r){
    if(l == r) {t[now] = num[l] - num[l - 1];return;}
    build(ls,l,mid);
    build(rs,mid+1,r);
    t[now] = max(t[ls],t[rs]);
}

void update(int now,int l,int r,int pos,int value){
    if(l == r) {
        t[now] = value;
        return;
    }
    if(pos <= mid) update(ls,l,mid,pos,value);
    else  update(rs,mid+1,r,pos,value);
    t[now] = max(t[ls],t[rs]);
}

int main(){
    while(~scanf ("%d", &n)){
        For(i,1,n) scanf ("%d", num+i);
        build(1,2,n);  //注意建树范围,从2到n
        scanf ("%d", &m);int pos,value;
        For(i,1,m){
            scanf ("%d%d", &pos, &value);
            num[pos] = value;             //原数组修改
            if(pos > 1) update(1,2,n,pos,num[pos] - num[pos - 1]);   //如果不是第一个点
            if(pos < n) update(1,2,n,pos+1,num[pos + 1] - num[pos]);  //如果不是最后一个点
            double tem = t[1];
            printf("%.2lf\n",tem);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值