#include using namespace std; using ll = long long; using pll = pair; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; struct rangefreq{ int n; vector> dat; vector> sum; rangefreq(const vector &v={}){ n = 1; while(n < v.size()) n <<= 1; dat.assign(2*n-1, {}); sum.assign(2*n-1, {0}); for(int i=0;i=0;i--){ dat[i].resize(dat[i*2+1].size()+dat[i*2+2].size()); merge(dat[i*2+1].begin(),dat[i*2+1].end(), dat[i*2+2].begin(),dat[i*2+2].end(), dat[i].begin() ); for(int j=0;j query(int a, int b, ll x, int k=0, int l=0, int r=-1) const //[a,b) count(*> n >> q; vector a(n); for(int i=0; i> a[i]; vector sum(n+1, 0LL); for(int i=0; i> l >> r; l--; r--; ll x; cin >> x; int len = r - l + 1; ll s = sum[r+1] - sum[l]; auto [cnt, sum1] = freq.query(l, r+1, x); cout << (ll)cnt*x - sum1 + (s - sum1) - (ll)(len - cnt)*x << '\n'; } } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }