NThe reverse logarithm cannot exceed khttp: // codeforces.com/contest/220/problem/e
Obviously, if (I, j) is satisfied, (I, j + r) r> = 0 must be satisfied, so the left endpoint is enumerated and the right endpoint is maintained, typical two points. Then, the reverse logarithm of the entire interval is updated. Delete a number or add a reverse logarithm that needs to be increased or reduced. It is better to have the current sequence. Typical is the persistent line segment tree. After discretization, the Chairman tree is created from the beginning to the end and from the end to maintain the prefix and suffix. Then there is the two points process. Pay attention to the details when updating.# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> # define lowbit (I) (I & (-I )) # define ll long using namespace STD; const int n = 100005; const int M = 5000000; int n, m, a [n]; ll K; vector <int> V; struct persistent_tree {int lson [m], rson [m], C [m], t [m]; int tot, M; void Init (int t, int _ m) {tot = 0; M = _ m; t [T] = bulid (1, m);} int bulid (int l, int R) {int root = tot ++; c [root] = 0; If (L! = R) {int M = (L + r)> 1; lson [root] = bulid (L, M); rson [root] = bulid (m + 1, r);} return root;} int Update (INT root, int POs, int Val) {int newroot = tot ++, TMP = newroot; c [newroot] = C [root] + val; int L = 1, R = m; while (L <r) {int mid = (L + r)> 1; if (Pos <= mid) {rson [newroot] = rson [root]; lson [newroot] = tot ++; newroot = lson [newroot]; root = lson [root]; r = mid;} else {lson [newroot] = lson [root]; rson [newroot] = tot ++; newroot = rson [newroot]; root = rson [root]; L = Mid + 1;} C [newroot] = C [root] + val;} return TMP;} int query (INT root, int l, int R, int L, int R) {If (r <L) return 0; If (L = L & R = r) return C [root]; int M = (L + r)> 1; if (r <= m) return query (lson [root], L, M, L, R ); else if (L> m) return query (rson [root], M + 1, R, L, R); else return query (lson [root], L, M, l, m) + query (rson [root], M + 1, R, m + 1, R);} void insert (INT now, int old, int POs, int Val) {T [now] = Update (T [old], POs, Val) ;}} pre, SUF; int main () {CIN >>> n >> K; v. push_back (-1); For (INT I = 1; I <= N; I ++) {CIN> A [I]; V. push_back (A [I]);} Sort (v. begin (), V. end (); V. resize (unique (v. begin (), V. end ()-v. begin (); For (INT I = 1; I <= N; I ++) A [I] = lower_bound (v. begin (), V. end (), a [I])-v. begin (); M = v. size ()-1; pre. init (0, m); SUF. init (m + 1, m); For (INT I = 1; I <= N; I ++) pre. insert (I, I-1, a [I], 1); For (INT I = N; I> = 1; I --) SUF. insert (I, I + 1, a [I], 1); LL cur = 0, ANS = 0; For (INT I = 1; I <= N; I ++) cur + = (LL) pre. query (pre. T [I], 1, m, a [I] + 1, m); // The Reverse logarithm of cur in the initial state for (INT I = 1, j = 2; I <= N; I ++) {While (j <= N & cur> K) {// Delete the J vertex, that is, find [1, i] cur-= (LL) pre. query (pre. T [I], 1, m, a [J] + 1, m); cur-= (LL) SUF. query (SUF. T [J], 1, m, 1, a [J]-1); j ++;} ans + = N-J + 1; if (j = I + 1) {// always keep j> I, delete J cur-= (LL) pre. query (pre. T [I], 1, m, a [J] + 1, m); cur-= (LL) SUF. query (SUF. T [J], 1, m, 1, a [J]-1); j ++;} // restore I + 1 // find [1, [J, m] cur + = (LL) pre. query (pre. T [I], 1, m, a [I + 1] + 1, m); cur + = (LL) SUF. query (SUF. T [J], 1, m, 1, a [I + 1]-1) ;}cout <ans <Endl; return 0 ;}