Tree cover Tree Bzoj 3236: [Ahoi2013] Job

Source: Internet
Author: User
Tags bit set rand

http://www.lydsy.com/JudgeOnline/problem.php?id=3236


Bit set SBT

Treap hopeless, no matter how to optimize the constant is not over, so casually changed to SBT ...

Start SBT sz I use the S maintenance results mad re ...

The last 90s card over ..... Slower than the MO team ....

Abused 2h ...

I knew I'd write a mo team ...

Mo team is clearly O (nsqrt (n) logn) My is O (nlognlogn) Why can be slow to think of ah ...

I must have been too weak ...
Orz 5s God Ben .....

。。。。。。。。。。。。。。。。。。。。

Have time to try Http://hi.baidu.com/greencloud/item/0e25878061444bde5e0ec1f7 later


#define _test _test #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream
> #include <cmath> #include <algorithm> using namespace std; /************************************************ Code by Willinglive blog:http://willinglive.cf ****************** /#define REP (i,l,r) for (int i=l,___t= (R), i<=___t;i++) #define PER (i,r,l) for (int i=r , ___t= (l); i>=___t;i--) #define MS (arr,x) memset (arr,x,sizeof (arr)) #define LL Long Long #define INE (i,u,e) for (int i=h
    Ead[u];~i;i=e[i].next) Inline const int Getint () {int R=0,k=1;char c=getchar (); for (;c< ' 0 ' | |
    C> ' 9 '; C=getchar ()) if (c== '-') k=-1;
    for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 ';
return k*r;
}/////////////////////////////////////////////////int n,m;
int a[100010]; inline int rnd () {return (rand () <<16) |rand ();} struct Data{int id,l,r,a,b;}
Q[1000010]; BOOL CMP (data A,data b) {return A.R&LT;B.R;} int ans1[1000010], ans2[1000010];
int last[100010];
namespace SBT {/////////////////////////////////#define LS T[O].L
#define RS T[O].R int root[100010]; struct Data{int l,r,s,sz,cnt,x,w,sum;}
T[1000010];
int sz; inline void update (int o) {t[o].s=t[ls].s+t[rs].s+t[o].cnt; t[o].sz=t[ls].sz+t[rs].sz+1;
T[O].SUM=T[LS].SUM+T[RS].SUM+T[O].W;} inline void L_rot (int &o) {int t=rs; RS=T[T].L; T[t].l=o; T[T].S=T[O].S; T[T].SZ=T[O].SZ;
T[t].sum=t[o].sum;update (o); o=t;} inline void R_rot (int &o) {int t=ls; LS=T[T].R; T[t].r=o; T[T].S=T[O].S; T[T].SZ=T[O].SZ;
T[t].sum=t[o].sum;update (o); o=t;} inline void maintain (int &o,bool b) {if (!b) {if (!
		LS) return;
		if (T[T[LS].L].SZ&GT;T[RS].SZ) R_rot (o);
		else if (T[T[LS].R].SZ&GT;T[RS].SZ) L_rot (LS), R_rot (o);
	else return; } else {if (!
		RS) return;
		if (T[T[RS].R].SZ&GT;T[LS].SZ) L_rot (o);
		else if (T[T[RS].L].SZ&GT;T[LS].SZ) R_rot (RS), L_rot (o);
	else return; } maintain (ls,0);
    Maintain (rs,1); Maintain (o,0); Maintain (o,1); } inline void Insert (int &o,int x) {if (o==0) {o=++sz; T[o].sz=t[o].s=t[o].cnt=1; T[o].x=x; t[o].w=0;
		t[o].sum=0;
	Return } t[o].s++;
	t[o].sz++;
		if (x<t[o].x) {insert (ls,x);
	Maintain (o,0);
		} else if (x>t[o].x) {insert (rs,x);
	Maintain (o,1);
} else t[o].cnt++;
	} inline int Query1 (int o,int x) {int res=0;
		while (o) {if (x<t[o].x) O=ls;
		else if (x>t[o].x) res+=t[ls].s+t[o].cnt,o=rs;
	else return res+t[ls].s+t[o].cnt;
} return res;
	} inline int Query2 (int o,int x) {int res=0;
		while (o) {if (x<t[o].x) Res+=t[rs].s+t[o].cnt,o=ls;
		else if (x>t[o].x) o=rs;
	else return res+t[rs].s+t[o].cnt;
} return res;
		} inline void Add (int o,int clr,int x) {while (O) {t[o].sum+=x;
		if (clr<t[o].x) O=ls;
		else if (clr>t[o].x) o=rs;
	else t[o].w+=x,o=0;
	}} inline int query3 (int o,int x) {int res=0;
		while (o) {if (x<t[o].x) O=ls;
		else if (x>t[o].x) res+=t[ls].sum+t[o].w,o=rs; else return Res+t[ls].sum+t[o].W;
} return res;
	} inline int Query4 (int o,int x) {int res=0;
		while (o) {if (x<t[o].x) Res+=t[rs].sum+t[o].w,o=ls;
		else if (x>t[o].x) o=rs;
	else return RES+T[RS].SUM+T[O].W;
} return res; }}/////////////////////////////////namespace BIT {/////////////////////////////////inline void insert (int o,int x) {F
or (; o<=n;o+=o&-o) Sbt::insert (sbt::root[o],x);
	} inline int Query1 (int o,int a,int b) {using namespace SBT;
	int s=0;
    for (; o>0;o-=o&-o) S+=sbt::query2 (root[o],a) + sbt::query1 (root[o],b);
return s;
	} inline int Query2 (int o,int a,int b) {using namespace SBT;
	int s=0;
    for (; o>0;o-=o&-o) S+=sbt::query3 (root[o],b) + sbt::query4 (root[o],a)-Sbt::query3 (Root[o],n);
return s; } inline void Add (int o,int clr,int x) {for (; o<=n;o+=o&-o) Sbt::add (sbt::root[o],clr,x);}} void input () {using namespace
    BIT; scanf ("%d%d", &AMP;N,&AMP;M);
    int x;
    Rep (i,1,n) Insert (I,a[i]=getint ());
   	Rep (i,1,m) q[i].id=i, Q[i].l=getint (), Q[i].r=getint (), Q[i].a=getint (), Q[i].b=getint ();
Sort (&q[1],&q[m+1],cmp);
	} void Solve () {using namespace BIT;
	int cur=1;
	int id,l,r,a,b;
        Rep (i,1,n) {int &lst=last[A[i]];
        if (lst==0) Add (i,a[i],1);
        else Add (lst,a[lst],-1), add (i,a[i],1);
        
		Last[a[i]]=i;
			
			for (; q[cur].r==i && cur<=m;cur++) {id=q[cur].id; l=q[cur].l-1; r=q[cur].r; a=q[cur].a; b=q[cur].b;
			int S1=query1 (R,A,B)-R;
            int S2=query1 (L,A,B)-l;
            
            ANS1[ID]=S1-S2;
            S1=query2 (R,A,B);
            S2=query2 (L,A,B);
		ANS2[ID]=S1-S2;
}} Rep (i,1,m) printf ("%d%d\n", ans1[i],ans2[i]); }/////////////////////////////////////////////////int Main () {#ifndef _test freopen ("std.in", "R", stdin); freope
    N ("Std.out", "w", stdout);
    #endif input (), solve ();
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.