题目
题解思路
这题值动态的更新查询树 , 后面添加的值和之前更新的值有关系。
不能用静态的方法做。
这里把线段树模板的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 ;
}