#define MOD_TYPE 1 #pragma region Macros #include using namespace std; #if 0 #include #include using Int = boost::multiprecision::cpp_int; using lld = boost::multiprecision::cpp_dec_float_100; #endif #if 1 #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif using ll = long long int; using ld = long double; using ull = unsigned long long; using pii = pair; using pll = pair; using pld = pair; template using smaller_queue = priority_queue, greater>; constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353); //constexpr ll MOD = 1; constexpr int INF = (int)1e9 + 10; constexpr ll LINF = (ll)4e18; constexpr double PI = acos(-1.0); constexpr double EPS = 1e-9; constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0}; constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0}; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define REPI(i, m, n) for (int i = m; i < (int)(n); ++i) #define repi(i, n) REPI(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << "\n" #define Yes(n) cout << ((n) ? "Yes" : "No") << "\n" #define possible(n) cout << ((n) ? "possible" : "impossible") << "\n" #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n" #define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n" #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x) cerr << #x << ":" << x << "\n"; struct io_init { io_init() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(30) << setiosflags(ios::fixed); }; } io_init; template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } template inline void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template constexpr istream &operator>>(istream &is, pair &p) noexcept { is >> p.first >> p.second; return is; } template constexpr ostream &operator<<(ostream &os, pair &p) noexcept { os << p.first << " " << p.second; return os; } #pragma endregion template struct BIT { int n; vector dat; BIT() {} BIT(int n_) : n(n_), dat(n_, 0) {} // 0-indexed void add(int i, T x) { i++; while (i <= n) { dat[i - 1] += x; i += i & -i; } } // [0, i) T sum(int i) { T res = 0; while (i > 0) { res += dat[i - 1]; i -= i & -i; } return res; } // 0-indexed T get(int i) { return sum(i + 1) - sum(i); } // [l, r) T sum(int l, int r) { return sum(r) - sum(l); } void display() { for (int i = 0; i < n; i++) cerr << get(i) << " "; cerr << "\n"; } }; template struct SegmentTree { using F = function; using G = function; using H = function; using P = function; int n; F f; G g; H h; P p; T ti; E ei; vector dat; vector laz; SegmentTree() {} SegmentTree( int n_, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(n_); } SegmentTree( vector &v, F f, G g, H h, T ti, E ei, P p = [](E a, int b) { return a; }) : f(f), g(g), h(h), ti(ti), ei(ei), p(p) { init(v.size()); build(v); } void init(int n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, ti); laz.clear(); laz.resize(2 * n - 1, ei); } void build(const vector v) { for (int i = 0; i < v.size(); i++) dat[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } void eval(int len, int k) { if (laz[k] == ei) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = ei; } T update(int a, int b, E x, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(int a, int b, E x) { return update(a, b, x, 0, 0, n); } T query(int a, int b, int k, int l, int r) { eval(r - l, k); if (r <= a || b <= l) return ti; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(int a, int b) { return query(a, b, 0, 0, n); } void disp() { rep(i, n) cerr << query(i, i + 1) << " "; cerr << "\n"; } }; auto my_min = [](ll a, ll b) { return min(a, b); }; int n; vector a; BIT bit_l, bit_r; SegmentTree sg; ll inv = 0; struct Mo { vector left, right, order; vector> v; int width; int nl, nr, ptr; Mo(int n) : width((int)sqrt(n)), nl(0), nr(0), ptr(0), v(vector>(n, vector(2, 0))) {} void insert(int l, int r) /* [l, r) */ { left.push_back(l); right.push_back(r); } /* ソート */ void build() { order.resize(left.size()); iota(begin(order), end(order), 0); sort(begin(order), end(order), [&](int a, int b) { if (left[a] / width != left[b] / width) return left[a] < left[b]; return right[a] < right[b]; }); } /* クエリを 1 つぶんすすめて, クエリのidを返す */ void process(int &query_id, ll &len) { if (ptr == order.size()) { query_id = -1; return; } const auto id = order[ptr]; while (nl > left[id]) distribute(--nl, 0); while (nr < right[id]) distribute(nr++, 1); while (nl < left[id]) distribute(nl++, 0); while (nr > right[id]) distribute(--nr, 1); query_id = order[ptr++], len = right[id] - left[id]; } inline void distribute(int idx, bool R) { v[idx][R].flip(); if (v[idx][R] ^ !R) add(idx, R); else del(idx, R); } void add(int idx, bool R) { //cerr << "add\n"; //dbg(idx); //dbg(R); if (!R) // left { sg.update(0, a[idx], -1); bit_l.add(a[idx], -1); } else // right { sg.update(a[idx] + 1, n, -1); bit_r.add(a[idx], -1); } inv -= bit_l.sum(a[idx] + 1, n) + bit_r.sum(0, a[idx]); //sg.disp(); } void del(int idx, bool R) { //cerr << "del\n"; //dbg(idx); //dbg(R); if (!R) // left { sg.update(0, a[idx], 1); bit_l.add(a[idx], 1); } else // right { sg.update(a[idx] + 1, n, 1); bit_r.add(a[idx], 1); } inv += bit_l.sum(a[idx] + 1, n) + bit_r.sum(0, a[idx]); //sg.disp(); } }; void solve() { int q; cin >> n >> q; bit_l = BIT(n), bit_r = BIT(n); vector sg_init(n, 0); sg = SegmentTree(sg_init, my_min, plus(), plus(), (ll)1e18, 0LL); a.resize(n); rep(i, n) { cin >> a[i]; a[i]--; } for (int i = n - 1; i >= 0; i--) { inv += bit_r.sum(0, a[i]); bit_r.add(a[i], 1); } //dbg(inv); Mo mo(n); rep(qi, q) { int l, r; cin >> l >> r; l--; mo.insert(l, r); } mo.build(); rep(i, n) sg.update(a[i] + 1, n, 1); //sg.disp(); vector ans(q); rep(qi, q) { int i; ll len; mo.process(i, len); //dbg(i); //dbg(inv); ans[i] = inv + sg.query(0, n) * len; //sg.disp(); } rep(i, q) cout << ans[i] << "\n"; } int main() { solve(); }