1275. 最大数 动态增加删除数 线段树 模板题 4倍空间

题目

在这里插入图片描述

题解思路

这题值动态的更新查询树 , 后面添加的值和之前更新的值有关系。

不能用静态的方法做。

这里把线段树模板的sum改成最大值即可。
一开始初始化的时候要开到够大

之前用线段树只开两倍空间 , 这次被搞了, 一直段错误 , 根据y总的估计 起码要4倍空间才能不段错误。

AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

const  int  INF =  0x3f3f3f3f;

struct node
{
    long long sum;
    int l,r,lz;
};
const int N = 201000 ;

node tee[4*N];

int n;

void  in(int i , int t1 , int t2)
{
    tee[i].l = t1 , tee[i].r = t2;
    if ( t1 == t2 )
    {
        tee[i].sum = 0 ;
        tee[i].lz = 0 ;
        return ;
    }
    int j = (t1+t2)/2;
    in(i*2,t1,j);
    in(i*2+1,j+1,t2);
    tee[i].sum =  max (tee[i*2].sum , tee[i*2+1].sum ) ;
    tee[i].lz = 0 ;
}
void pd(int i )
{
    if ( tee[i].lz != 0 )
    {
        tee[i*2].lz += tee[i].lz;
        tee[i*2+1].lz += tee[i].lz;
        tee[i*2].sum += tee[i].lz*(tee[i*2].r - tee[i*2].l + 1 );
        tee[i*2+1].sum += tee[i].lz*(tee[i*2+1].r - tee[i*2+1].l + 1 );
        tee[i].lz = 0 ;
    }
}
long long sea( int i ,int t1 , int t2 )
{
    if ( tee[i].l >= t1 && tee[i].r <= t2 )
        return tee[i].sum;
    pd(i);
    long long sum = 0 ;
    if ( tee[i*2].r >= t1 )
        sum = max( sea(i*2,t1,t2) , sum );
    if ( tee[i*2+1].l <= t2 )
        sum = max( sea(i*2+1,t1,t2) , sum );
    return sum;
}
void _add( int i , int t1 , int t2 , int vv )
{
    if ( tee[i].l >= t1 && tee[i].r <= t2 )
    {
         tee[i].sum += vv*(tee[i].r - tee[i].l + 1);
         tee[i].lz += vv ;
         return;
    }
    pd(i);
    if ( tee[i*2].r >= t1)
        _add(i*2,t1,t2,vv);
    if (tee[i*2+1].l <= t2)
        _add(i*2+1,t1,t2,vv);
    tee[i].sum = max(tee[i*2].sum  ,  tee[i*2+1].sum ) ;

}
int main ()
{
    //ios::sync_with_stdio(false);
    int m  , p  ;
    cin >> m >> p ;
    in ( 1 , 1 , m ) ;
    n = 0 ;
    long long a  = 0 ;
    while( m -- )
    {
        char ch[2] ;
        long long t1 ;
        cin >> ch >> t1 ;
        if ( ch[0] == 'Q' )
        {
            a = sea( 1 ,  n - t1 + 1   , n ) ;
            cout << a  <<"\n" ;
        }else
        {
            n++;
            _add(1, n , n , ( t1 + a )%p  );
        }
    }
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值