/*let's start with the first glimpse of the wonderful way to come up with 23333. Look for each number around a few than he was a few days ago just learned the interval K small to seek the method then ... Enumeration in the middle of the point for the left interval two points to find he is the number one big right interval and then * up*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 50010#defineMAXM 50010*18*5#definell Long Longusing namespacestd;ll N,cnt,root[maxn],order[maxn],a[maxn],ans;structnode{ll Lc,rc,sum;} T[MAXM];LL Init () {ll x=0;Chars=GetChar (); while(s<'0'|| S>'9') s=GetChar (); while(s>='0'&&s<='9') {x=x*Ten+s-'0'; s=GetChar ();} returnx;} ll Build (ll s,ll l,ll R) {ll k=++cnt;t[k].sum=S; T[K].LC=l;t[k].rc=R; returnK;}voidInsert (LL &root,ll pre,ll pos,ll l,ll R) {Root=build (t[pre].sum+1, t[pre].lc,t[pre].rc); if(L==R)return; LL Mid=l+r>>1; if(pos<=mid) Insert (T[root].lc,t[pre].lc,pos,l,mid); ElseInsert (t[root].rc,t[pre].rc,pos,mid+1, R);} ll Query (ll l,ll r,ll k,ll l,ll R) {if(L==R)returnl; ll Sum=t[t[r].lc].sum-t[t[l].lc].sum; LL Mid=l+r>>1; if(k<=sum)returnQuery (T[l].lc,t[r].lc,k,l,mid); Else returnQuery (t[l].rc,t[r].rc,k-sum,mid+1, R);}intMain () {//freopen ("queueb.in", "R", stdin); //freopen ("Queueb.out", "w", stdout);n=init (); for(LL i=1; i<=n;i++) {A[i]=init (); Order[i]=A[i]; } Sort (Order+1, order+1+N); ll Num=n,pos,l,r,x,s1,s2; for(LL i=1; i<=n;i++) {pos=lower_bound (order+1, order+1+n,a[i])-order; Insert (Root[i],root[i-1],pos,1, num); } for(LL i=1; i<=n;i++) {x=a[i];s1=s2=0; L=1; r=i; while(l<=R) {ll Mid=l+r>>1; POS=query (root[0],root[i],mid,1, num); if(order[pos]<x) {S1=mid;l=mid+1; } Elser=mid-1; } l=1; r=n-i+1; while(l<=R) {ll Mid=l+r>>1; POS=query (root[i-1],root[n],mid,1, num); if(order[pos]<x) {S2=mid;l=mid+1; } Elser=mid-1; } ans+=s1*S2; } printf ("%lld\n", ans); return 0;}
/*In fact, there are several smaller than his small can be used in the reverse order of the tree array is to ask the left a few bigger than his this same ah attention to the right of the discretization of the order and the left is not the same*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXN 50010#definell Long Longusing namespacestd;intn,b[maxn],t[maxn],l[maxn],r[maxn];ll ans;structnode{into,x;} A[MAXN];intinit () {intx=0;Chars=GetChar (); while(s<'0'|| S>'9') s=GetChar (); while(s>='0'&&s<='9') {x=x*Ten+s-'0'; s=GetChar ();} returnx;}intCMP1 (ConstNode &a,ConstNode &b) { if(a.x==b.x)returnA.o>B.O; returna.x<b.x;}intCMP2 (ConstNode &a,ConstNode &b) { if(a.x==b.x)returna.o<B.O; returna.x<b.x;}voidADD (intPosintdata) { while(pos<=N) {T[pos]+=data; POS+=pos& (-POS); }}intFindintPOS) { intR=0; while(POS) {R+=T[pos]; POS-=pos& (-POS); } returnR;}intMain () {Freopen ("queueb.in","R", stdin); Freopen ("Queueb.out","W", stdout); N=init (); for(intI=1; i<=n;i++) {a[i].x=init (); A[I].O=i; } sort (A+1, A +1+N,CMP1); for(intI=1; i<=n;i++) B[A[I].O]=i; for(intI=1; i<=n;i++) {Add (b[i],1); L[i]=find (b[i]-1); } memset (T,0,sizeof(t)); Sort (a+1, A +1+N,CMP2); for(intI=1; i<=n;i++) B[A[I].O]=i; for(inti=n;i>=1; i--) {Add (b[i],1); R[i]=find (b[i]-1); } for(intI=1; i<=n;i++) ans+ = (LL) l[i]*R[i]; printf ("%lld\n", ans); return 0;}
COGS 859. Sequence