#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; using namespace std; #ifndef LOCAL #define debug(x) ; #else #define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl; template ostream &operator<<(ostream &out, const pair &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template ostream &operator<<(ostream &out, const vector &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } #endif #define mod 1000000007 //1e9+7(prime number) #define INF 1000000000 //1e9 #define LLINF 2000000000000000000LL //2e18 #define SIZE 200010 /* Binary Indexed Tree(0-index) */ template struct BIT{ int bit_size; vector data; BIT(int n){ bit_size = n; data.assign(n+2, 0); } //add x to kth value void add(int k, Type x){ k++; while(k <= bit_size){ data[k] += x; k += k & (-k); } } //sum of [0,k) Type query(int k){ Type rec = 0; while(k > 0){ rec += data[k]; k -= k & (-k); } return rec; } //sum of [a, b) Type query(int a, int b){ return query(b) - query(a); } }; vector vec[SIZE]; int main(){ int N, Q; int a[SIZE]; int l[SIZE], r[SIZE]; scanf("%d%d", &N, &Q); for(int i=0; i > query; for(int i=0; i > pq; for(int i=N-1; i>=0; i--) { pq.push({-a[i], i}); while(pq.size() && a[i] > -pq.top().first) { vec[i].push_back(pq.top().second); debug(i); debug(pq.top().second); pq.pop(); } } BIT bit(N); int ans[SIZE]; while(pq.size()) { int p = pq.top().second; bit.add(p, 1); pq.pop(); } int t = 0; for(int i=0; i