[Code+ Month race] Yazid's freshman prom __ tree-like array

Source: Internet
Author: User

The problem was done in a very interesting way.

It was very bad, and it didn't occur to me.
Consider the number of intervals in which the number is x, by sequence a tectonic sequence B, BX (i) =−1+2∗[a (i) ==x] b_x (i) =-1+2*[a (i) ==x], prefixed and SX (i) =sx (i−1) +b (i) s_x (i) =s_x (i-1) +b (i).
ANS=∑X=0N−1∑1≤J<I≤N[SX (i) >SX (j)] Ans=\sum_{x=0}^{n-1}\sum_{1\le Js_x (j)]
The simple approach is O (N2LOGN) O (n^2\log N), which can be maintained with a tree-like array.
But the magic is that we can insert and query one arithmetic progression at a time. (This is the descending sequence, but in fact I think the increment sequence is the same)
This becomes the interval modification, the interval query, may use the line segment tree to realize.
Of course, the difference can be used in a tree-like array.

That's what I was thinking. After considering X, I can be the right endpoint of a legal interval and only if the maximum suffix of I is greater than 0, so we can use the maximum number of segments and the equivalent of pushing those +1 to the right to fill 1 pits, I is the same as the left endpoint. At this point, the legal interval is bound to be filled in this section of the pit, then we will be filled with these holes in the sequence of a segment to use the above simple practice, we can get an O (NLOGN) O (n\log N) practice.
The only advantage of this approach is that the larger constant size of the brain hole can be used to brush Bzoj rank1.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
char * cp= (char *) malloc (5000000); inline void in (int &x) {while (*cp< ' 0 ') | |
    *cp> ' 9 ') ++CP;
for (x=0;*cp>= ' 0 ' &&*cp<= ' 9 ';) x=x*10+*cp++-' 0 ';} const int n=5e5+5,s=1e6+5;
int n;
int a[n],b[n];
int head[n],tail[n],nxt[n],lst[n];

BOOL Flag[n];
int Bit[s];
int B; inline void Add (int x) {for (x+=n+1;x<=b;x+=x&-x) ++bit[x];} inline void del (int x) {for (x+=n+1;x<=b;x+=x 
&-x) bit[x]=0;
    inline int query (int x) {int ans=0;
    for (X+=n+1;x;x-=x&-x) ans+=bit[x];
return ans;
    int main () {freopen ("f.in", "R", stdin);//Freopen ("F.out", "w", stdout);
    Fread (Cp,1,5000000,stdin);
    In (n);
    b=n<<1|1;
        {int t;
    In (t);

    for (int i=1;i<=n;++i) in (A[i]);
        for (int i=1;i<=n;++i) {lst[i]=tail[a[i]];
    Tail[a[i]]=i;
        for (int i=n;i;--i) {nxt[i]=head[a[i]];
    Head[a[i]]=i;
LL ans=0;    for (int i=n;i--;) {int x=0;
            for (int j=head[i];j;) {x=j;
            B[x]=1;

            for (int y=1;x<=n&&y>=0;++x,y+=b[x]=a[x]==i?1:-1) flag[x]=1;
            J=NXT[J];
        while (J&AMP;&AMP;X&GT;J) j=nxt[j];
        } x=n;
            for (int j=tail[i];j;) {x=j;

            for (int y=1;x&&y>=0;--x,y+=b[x]=a[x]==i?1:-1) flag[x]=1;
            J=LST[J];
        while (J&AMP;&AMP;X&LT;J) j=lst[j];
        } x=0;
            for (int j=head[i];j;) {x=j;
            while (X&&flag[x-1])--x;

            int l=x;
            Add (0);
                for (int y=0;flag[x];++x) {y+=b[x];
                Add (y);
            Ans+=query (y-1);
            } x=l;
            Del (0);
                for (int y=0;flag[x];++x) {y+=b[x];
                Del (y);
            flag[x]=0;
            } J=nxt[j]; while (J&AMP;&AMP;J&LT;=X)J=NXT[J];
} printf ("%lld\n", ans); }

Summary:
① unexpectedly can have batch query, batch modify this operation, it seems I really is old.
② This problem can be divided into the rule. Do not know how to do, later have time/opportunity to ask/think about it.

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.