Uvalive 6860 Most Influential Pumpkin

Source: Internet
Author: User

Test instructions: give you a sequence, each operation can make a continuous sequence of 1, each time after the update to output the median of the sequence.

Note that the median size only increases, does not decrease, and increases the median by up to 1 after each operation.

The original sequence block operation, if the block in the query "L,r", then make the block cnt++, so that only the end of the query interval alone operation can be.

The number of less than or equal to the current median in the lookup sequence after each update, if the number is less than or equal to N/2. The median + +.

When you find a number less than or equal to the median, use lower_bound to time out, and you can save a counter p to indicate the number of times before the update, because most of the interval P-values do not change. (But the worst complication is much greater = =)


Code:

#include <bits/stdc++.h> #define FI first #define SE second #define PB push_back #define Lson o<<1 #define Rson

O<<1|1 #define CLR (A, X) memset (A, X, sizeof (a)) using namespace Std;
typedef long Long LL;
typedef pair<int, int> PII;
Const double EPS = 1e-10;

int dcmp (double x) {if (Fabs (x) <eps) return 0; return x<0?-1:1;} const int N = 6E4+5;
    struct Node {int len, CNT, p;

    PII a[300];
    void Clear () {len = cnt = p = 0;}
    void push_back (int x, int y) {a[len++] = {x, y};}

    void My_sort () {sort (A, a+len);} void Update (int L, int R) {for (int i = 0; i < len; i++) {if (A[i].se<=r && a[i].se>=
            L) {a[i].fi++;
    }} my_sort ();
        } int sum (int x) {while (p>=0 && a[p].fi+cnt>x) p--;
        while (P+1<len && a[p+1].fi+cnt<=x) p++;
    return p+1;

}}b[300];

int a[n];
    int main () {int n, q, x; while (~scanF ("%d%d", &n, &q)) {if (!n &&!q) continue; int len = sqrt (n+0.5), tot = n/len+!!
        (N%len);
        for (int i = 0; i < tot; i++) b[i].clear ();
            for (int i = 0; i < n; i++) {scanf ("%d", &a[i]);
        B[I/LEN].PB (A[i], i);
        } for (int i = 0; i < tot; i++) B[i].my_sort ();
        Sort (A, a+n);
        int Mid = A[N/2];
            while (q--) {int L, R;
            scanf ("%d%d", &l, &r);
            L--, r--;
            for (int i = l/len+1; i < R/len; i++) b[i].cnt++;
            if (L/len = = R/len) b[l/len].update (L, R);
                else {b[l/len].update (L, (l/len+1) *len-1);
            B[r/len].update ((R/len) *len, R);
            } int tmp = 0;
            for (int i = 0; i < tot; i++) {tmp + = B[i].sum (Mid);
            } if (tmp <= N/2) mid++;
        printf ("%d\n", Mid);
}} 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.