#include <iostream>
using namespace std;
const int N = 1e6 + 1 ;
int n , m;
int a [ N ] ;
struct node {
int tag , sum ;
node(){ tag = sum = 0 ; }
void init ( int x , int y ) { tag = x , sum = y ; }
};
namespace xds {
node tree [ N << 2 ] ;
void build ( int k , int l , int r ) {
if ( l == r ) { tree [ k ] . init ( 0 , a [ l ] ) ; return ; }
int mid = l + r >> 1 ;
build ( k << 1 , l , mid );
build ( k << 1 | 1 , mid + 1 , r );
tree [ k ] . sum = min ( tree [ k << 1 ] . sum , tree [ k << 1 | 1 ] . sum ) ;
return ;
}
void push_down ( int k ) {
if ( tree [ k ] . tag == 0 ) return;
tree [ k << 1 ] . tag += tree [ k ] . tag;
tree [ k << 1 | 1 ] . tag += tree [ k ] . tag;
tree [ k << 1 ] . sum += tree [ k ] . tag;
tree [ k << 1 | 1 ] . sum += tree [ k ] . tag;
tree [ k ] . tag = 0 ;
return ;
}
void update ( int k , int l , int r , int x , int y , int z ) {
if ( l >= x && r <= y ) {
tree [ k ] . tag += z;
tree [ k ] . sum += z;
return;
}
int mid = l + r >> 1 ;
push_down ( k ) ;
if ( x <= mid ) update ( k << 1 , l , mid , x , y , z ) ;
if ( y > mid ) update ( k << 1 | 1 , mid + 1 , r , x , y , z ) ;
tree [ k ] . sum = min ( tree [ k << 1 ] . sum , tree [ k << 1 | 1 ] . sum ) ;
return ;
}
int query ( int k , int l , int r , int x , int y ) {
if ( l >= x && r <= y ) { return tree [ k ] . sum ; }
int mid = l + r >> 1 ;
push_down ( k ) ;
int rhs = ( 1 << 30 );
if ( x <= mid ) rhs = min ( rhs , query ( k << 1 , l , mid , x , y ) ) ;
if ( y > mid ) rhs = min ( rhs , query ( k << 1 | 1 , mid + 1 , r , x , y ) ) ;
return rhs ;
}
}
int main ( ) {
// freopen ( "text.in" , "r" , stdin ) ;
cin >> n >> m;
for ( int i = 1 ; i <= n ; i ++ ) {
cin >> a [ i ] ;
}
xds :: build ( 1 , 1 , n ) ;
for ( int i = 1 ; i <= m ; i ++ ) {
static int x , y , z ;
cin >> z >> x >> y ;
if ( xds :: query ( 1 , 1 , n , x , y ) >= z ) xds :: update ( 1 , 1 , n , x , y , -z ) ;
else {
return printf ( "-1\n%d\n" , i ) * 0 ;
}
}
puts("0");
return 0 ;
}
标签:node,NOIP2012,int,rhs,教室,tag,xds,P1083,sum
From: https://www.cnblogs.com/dadidididi/p/16858868.html