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
| #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 int long long #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 A[MAXN] , re[MAXN]; int buc[MAXN] , lst[MAXN] , nxt[MAXN];
struct que { int r , ll , rr , idx , c; } Q[MAXN * 3] ; int cnt; bool cmp( const que& a , const que& b ) { return a.r < b.r; }
int kk[MAXN << 2] , bb[MAXN << 2] , tk[MAXN << 2] , tb[MAXN << 2] , T[MAXN << 2] , tg[MAXN << 2] , fk[MAXN << 2]; void pu( int rt ) { kk[rt] = kk[rt << 1] + kk[rt << 1 | 1] , bb[rt] = bb[rt << 1] + bb[rt << 1 | 1]; T[rt] = max( T[rt << 1] , T[rt << 1 | 1] ); fk[rt] = ( fk[rt << 1] & fk[rt << 1 | 1] & T[rt << 1] == T[rt << 1 | 1] ); } inline void fuck( int rt , int c ) { T[rt] = tg[rt] = c , fk[rt] = 1; } void pd( int rt , int m ) { int ls = rt << 1 , rs = rt << 1 | 1; if( tk[rt] ) { kk[ls] += tk[rt] * ( m - ( m >> 1 ) ) , kk[rs] += tk[rt] * ( m >> 1 ); tk[ls] += tk[rt] , tk[rs] += tk[rt]; tk[rt] = 0; } if( tb[rt] ) { bb[ls] += tb[rt] * ( m - ( m >> 1 ) ) , bb[rs] += tb[rt] * ( m >> 1 ); tb[ls] += tb[rt] , tb[rs] += tb[rt]; tb[rt] = 0; } if( ~tg[rt] ) { fuck( ls , tg[rt] ) , fuck( rs , tg[rt] ); tg[rt] = -1; } } void build( int rt , int l , int r ) { if( l == r ) { T[rt] = kk[rt] = re[l] , fk[rt] = 1; return; } int m = l + r >> 1; build( rt << 1 , l , m ) , build( rt << 1 | 1 , m + 1 , r ); pu( rt ); } int t1; void chkmn( int rt , int l , int r , int L , int R , int c ) { if( L <= l && R >= r && T[rt] <= c ) return; if( L <= l && R >= r && fk[rt] ) { kk[rt] += ( c - T[rt] ) * ( r - l + 1 ) , bb[rt] -= t1 * ( c - T[rt] ) * ( r - l + 1 ); tk[rt] += c - T[rt] , tb[rt] -= t1 * ( c - T[rt] ); fuck( rt , c ); return; } pd( rt , r - l + 1 ); int m = l + r >> 1; if( L <= m ) chkmn( rt << 1 , l , m , L , R , c ); if( R > m ) chkmn( rt << 1 | 1 , m + 1 , r , L , R , c ); pu( rt ); } int query( int rt , int l , int r , int L , int R ) { if( L <= l && R >= r ) { return kk[rt] * t1 + bb[rt]; } pd( rt , r - l + 1 ); int m = l + r >> 1 , ret = 0; if( L <= m ) ret += query( rt << 1 , l , m , L , R ); if( R > m ) ret += query( rt << 1 | 1 , m + 1 , r , L , R ); return ret; } int ans[MAXN]; void solve() { int typ; cin >> typ; cin >> n; rep( i , 1 , n ) scanf("%lld",A + i) , A[i] = min( A[i] , n ); int cur = 0; rep( i , 1 , n ) { ++ buc[A[i]]; while( buc[cur] ) ++ cur; re[i] = cur; } rep( i , 0 , n ) lst[i] = n + 1; per( i , n , 1 ) nxt[i] = lst[A[i]] , lst[A[i]] = i; cin >> q; int l , r; rep( i , 1 , q ) { scanf("%lld%lld",&l,&r); Q[++ cnt] = (que) { r , 1 , r , i , 1 }; if( l != 1 ) { Q[++ cnt] = (que) { l - 1 , 1 , l - 1 , i , -1 }; Q[++ cnt] = (que) { l - 1 , l , r , i , -1 }; } } sort( Q + 1 , Q + 1 + cnt , cmp ); cur = 1; memset( tg , -1 , sizeof tg ); build( 1 , 1 , n ); rep( i , 1 , n ) { t1 = i; while( cur <= cnt && Q[cur].r == i ) ans[Q[cur].idx] += Q[cur].c * query( 1 , 1 , n , Q[cur].ll , Q[cur].rr ) , ++ cur; chkmn( 1 , 1 , n , i , nxt[i] - 1 , A[i] ); } rep( i , 1 , q ) printf("%lld\n",ans[i]); }
signed main() {
solve(); }
|