Cf 220e little elephant and inversions (Chair tree + two points)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

Question: Give a sequence and ask how many binary groups (I, j) Meet A 1A2...ALARARLifecycle + lifecycle 1...ANThe 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.