#include using namespace std; using ll = long long; #define CIN( LL , A ) LL A; cin >> A #define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( int VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) #define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; return 0 class Pair { public: ll m_a; int m_i; inline Pair( const ll& a = 0 , const int& i = 0 ) : m_a( a ) , m_i( i ) {} }; class Order_a { public: inline bool operator()( const Pair& p0 , const Pair& p1 ) { return p0.m_a < p1.m_a; }; }; #define SET_MIN_SUM( I , PREV ) \ Pair& LR_sorted_i = LR_sorted[ I ]; \ if( LR_sorted_prev != LR_sorted_i.m_a ){ \ LR_sorted_comp_length_minus++; \ FOR( j , PREV , LR_sorted_i.m_a ){ \ int& A_comp_j = A_comp[j]; \ InvA_comp[A_comp_j]++; \ if( A_comp_max_plus <= A_comp_j ){ \ A_comp_max_plus = A_comp_j + 1; \ } \ } \ LR_sorted_prev = LR_sorted_i.m_a; \ } \ i_half = LR_sorted_i.m_i / 2; \ if( LR_sorted_i.m_i % 2 == 0 ){ \ L_num[i_half] = I; \ } else { \ R_num[i_half] = I; \ } \ int& X_comp_i = X_comp[i_half]; \ ll& min_sum_i = min_sum[ I ]; \ FOR( j , X_comp_i , A_comp_max_plus ){ \ min_sum_i += InvA_comp[j]; \ } \ min_sum_i *= AX_sorted_comp[X_comp_i]; \ FOR( j , 0 , X_comp_i ){ \ min_sum_i += InvA_comp[j] * AX_sorted_comp[j]; \ } \ \ int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); constexpr const int N_lim = 100002; constexpr const int N_lim2 = N_lim * 2; CIN( int , N ); CIN( int , Q ); Pair AX[N_lim2]; FOR( i , 0 , N ){ Pair& Ai = AX[i]; cin >> Ai.m_a; Ai.m_i = i; } Pair LR[N_lim2]; int i2; int i2_plus; int Ni; FOR( i , 0 , Q ){ i2 = i * 2; i2_plus = i2 + 1; Pair& Li = LR[i2]; Pair& Ri = LR[i2_plus]; cin >> Li.m_a; Li.m_a--; Li.m_i = i2; cin >> Ri.m_a; Ri.m_i = i2_plus; Ni = N + i; Pair& Xi = AX[Ni]; cin >> Xi.m_a; Xi.m_i = Ni; } Order_a ord{}; ll NQ = N + Q; sort( AX , AX + NQ , ord ); Pair ( &AX_sorted )[N_lim2] = AX; int A_comp[N_lim]; int X_comp[N_lim]; ll AX_sorted_comp[N_lim]; ll AX_sorted_comp_prev = -1; int AX_sorted_comp_length_minus = -1; FOR( i , 0 , NQ ){ Pair& AX_sorted_i = AX_sorted[i]; if( AX_sorted_comp_prev != AX_sorted_i.m_a ){ AX_sorted_comp_length_minus++; AX_sorted_comp[AX_sorted_comp_length_minus] = AX_sorted_i.m_a; AX_sorted_comp_prev = AX_sorted_i.m_a; } if( AX_sorted_i.m_i < N ){ A_comp[AX_sorted_i.m_i] = AX_sorted_comp_length_minus; } else { X_comp[AX_sorted_i.m_i - N] = AX_sorted_comp_length_minus; } } ll Q2 = Q * 2; sort( LR , LR + Q2 , ord ); Pair ( &LR_sorted )[N_lim2] = LR; int L_num[N_lim]; int R_num[N_lim]; int LR_sorted_prev = -1; int LR_sorted_comp_length_minus = -1; int i_half; ll InvA_comp[N_lim2] = {}; ll min_sum[N_lim2] = {}; int A_comp_max_plus = 0; { SET_MIN_SUM( 0 , 0 ); } FOR( i , 1 , Q2 ){ SET_MIN_SUM( i , LR_sorted_prev ); } FOR( i , 0 , Q ){ cout << min_sum[R_num[i]] - min_sum[L_num[i]] << "\n"; } return 0; }