1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 1000006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , q;
int RT , siz[MAXN] , val[MAXN] , fa[MAXN] , ch[MAXN][2] , idx;
void pu( int rt ) { siz[rt] = 1 + siz[ch[rt][0]] + siz[ch[rt][1]]; } void rotate( int u ) { int f = fa[u] , g = fa[f] , w = ch[f][1] == u , k = ch[u][w ^ 1]; if( g ) ch[g][ch[g][1] == f] = u; ch[u][w ^ 1] = f , ch[f][w] = k; fa[f] = u , fa[k] = f , fa[u] = g; pu( f ) , pu( u ); } void splay( int u , int to = 0 ) { if (!to) RT = u; int f , g; while( fa[u] != to ) { f = fa[u] , g = fa[f]; if( g != to ) rotate( ( ch[f][1] == u ^ ch[g][1] == f ) ? u : f ); rotate( u ); } } int poi; int kth( int u , int x ) { if( !u ) return 0; if( x <= siz[ch[u][0]] ) return kth( ch[u][0] , x ); if( x == siz[ch[u][0]] + 1 ) return u; return kth( ch[u][1] , x - siz[ch[u][0]] - 1 ); }
void ins( int u , int v ) { while( ch[u][0] ) u = ch[u][0]; ch[u][0] = ++ idx; val[idx] = v , fa[idx] = u , siz[idx] = 1 , u = idx; while( fa[u] ) u = fa[u] , pu( u ); }
void era( int u , int k ) { int pr = kth( RT , k - 1 ); splay( pr ); int nx = kth( RT , k + 1 ); splay( nx , pr ); ch[ch[RT][1]][0] = 0; pu( ch[RT][1] ) , pu( RT ); }
int qwq[MAXN]; void bsplay( int& rt , int f , int l , int r ) { if( l > r ) return; int m = l + r >> 1; rt = ++ idx; val[rt] = qwq[m] , siz[rt] = 1 , fa[rt] = f; bsplay( ch[rt][0] , rt , l , m - 1 ) , bsplay( ch[rt][1] , rt , m + 1 , r ); pu( rt ); }
int mx[MAXN << 2] , cn[MAXN << 2]; ll lt[MAXN << 2]; int A[MAXN] , LeaF[MAXN << 2];
ll wkr( int rt , int v ) { if( !lt[rt] || v >= mx[rt] ) return 0; if( LeaF[rt] ) return mx[rt]; if( v >= mx[rt << 1] ) return wkr( rt << 1 | 1 , v ); return lt[rt] - ( lt[rt << 1] - wkr( rt << 1 , v ) ); }
void pus( int rt ) { mx[rt] = max( mx[rt << 1] , mx[rt << 1 | 1] ); lt[rt] = lt[rt << 1] + wkr( rt << 1 | 1 , mx[rt << 1] ); }
void build( int rt , int l , int r ) { if( l == r ) { mx[rt] = lt[rt] = A[l] , LeaF[rt] = cn[rt] = 1; return; } int m = l + r >> 1; build( rt << 1 , l , m ) , build( rt << 1 | 1 , m + 1 , r ); pus( rt ); }
void ad( int rt , int l , int r , int p ) { if( l == r ) { cn[rt] ++; return; } int m = l + r >> 1; if( p <= m ) ad( rt << 1 , l , m , p ); else ad( rt << 1 | 1 , m + 1 , r , p ); }
void fuck( int rt , int l , int r , int p ) { if( l == r ) { if( cn[rt] == 1 ) mx[rt] = lt[rt] = 0; else -- cn[rt]; return; } int m = l + r >> 1; if( p <= m ) fuck( rt << 1 , l , m , p ); else fuck( rt << 1 | 1 , m + 1 , r , p ); pus( rt ); }
int tv; ll re; void que( int rt , int l , int r , int L , int R ) { if( L <= l && r <= R ) { re += wkr( rt , tv ) , tv = max( tv , mx[rt] ); return; } int m = l + r >> 1; if( L <= m ) que( rt << 1 , l , m , L , R ); if( R > m ) que( rt << 1 | 1 , m + 1 , r , L , R ); }
void ot( int rt ) { if( ch[rt][0] ) ot( ch[rt][0] ); printf("%d ",val[rt]); if( ch[rt][1] ) ot( ch[rt][1] ); }
int g[MAXN][20]; int qmx( int l , int r ) { int t = 31 - __builtin_clz( r - l + 1 ); return max( g[l][t] , g[r - ( 1 << t ) + 1][t] ); }
void solve() { cin >> n >> q; rep( i , 2 , n + 1 ) qwq[i] = i - 1; qwq[1] = -0x3f3f3f3f , qwq[n + 2] = 0x3f3f3f3f; bsplay( RT , 0 , 1 , n + 2 ); rep( i , 1 , n ) scanf("%d",A + i) , g[i][0] = A[i]; build( 1 , 1 , n ); rep( k , 1 , 18 ) rep( i , 1 , n - ( 1 << k ) + 1 ) g[i][k] = max( g[i][k - 1] , g[i + ( 1 << k - 1 )][k - 1] ); rep( i , 1 , q ) { int o , l , r; scanf("%d%d%d",&o,&l,&r); if( o == 1 ) { if( l == r ) continue; int t = kth( RT , r + 1 ); splay( t ); ad( 1 , 1 , n , val[t] ); ins( ch[RT][1] , val[t] ); int v1 = val[kth( RT , l + 1 )] , v2 = val[kth( RT , l + 2 )]; if( A[v1] < A[v2] ) fuck( 1 , 1 , n , v1 ) , era( RT , l + 1 ); else fuck( 1 , 1 , n , v2 ) , era( RT , l + 2 ); } else { int L = val[kth( RT , l + 1 )] , R = val[kth( RT , r + 1 )]; tv = re = qmx( l , L ); que( 1 , 1 , n , L , R ); printf("%lld\n",re); }
} }
signed main() {
solve(); }
|