"chunking" "Weights chunked" bzoj1901 Zju2112 Dynamic Rankings

Source: Internet
Author: User

An O (n*sqrt (n)) with modified interval k large value algorithm.

The sequence is divided into sqrt (n) blocks first.

Then the weights are divided, the total maintenance of sqrt (n) weights, for the weight of the block t[i], stored the sequence of blocks of the first block of the weight of the case.

For interval queries, you need to get the number of occurrences of each value in the interval, then the weight sweep O (sqrt (n)), the complete part we can get through the weighted block difference (O (1)) (such as LB~RB block is t[rb]-t[lb-1]), scattered parts we maintain an additional weight of the block , you can accumulate this value. O (sqrt (n)).

For the modification, directly in the location after the ownership value of the block modified, a single modification O (1), involving O (sqrt (n)) weight of the block, so is O (sqrt (n)).

So the average per operation is O (sqrt (n)), and the spatial complexity is O (n*sqrt (n)).

(Defect: ① must be discretized; ② space complexity is high, to n=100000, almost card space)

This code is currently on the Bzoj Rank1

802901 (8)
No. RunID User Memory time Language code_length submit_time
1 lizitong 10192 KB 208 MS C + + 3468 B 20 14-12-11 13:01:16
#include <cstdio> #include <algorithm> #include <cmath>using namespace Std;int num,ch[12],f,c;inline    void R (int &x) {c=0;f=1; for (;c< ' 0 ' | |    C> ' 9 '; C=getchar ()) if (c== '-') f=-1;    for (x=0;c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) (x*=10) + = (c ' 0 '); X*=f;}    inline void P (int x) {if (x<10) putchar (x+ ' 0 '); Else{p (X/10);p Utchar (x%10+ ' 0 ');}} struct Point{int v,p;} T[20001];bool operator < (const point &a,const point &b) {return A.V&LT;B.V;} int N,m,xs[10001],ys[10001],ks[10001],sum=1,en,en2,ma[20001],l[111],r[111];int a[20001],num[10001],num2[20001], L2[145];char op[10001];struct val_block{int b[20001],sumv[145];void Insert (const int &x) {++b[x]; ++sumv[num2[x]];} void Delete (const int &x) {--b[x];--sumv[num2[x]];}} T[111],s;int Kth (const int &AMP;L,CONST int &r,const int &x) {int cnt=0,res;if (Num[l]+1>=num[r]) {for (int i=l  ; i<=r;++i) S.insert (A[i]); for (int i=1;;            ++i) {Cnt+=s.sumv[i];     if (cnt>=x)         {Cnt-=s.sumv[i]; for (int j=l2[i];;              ++J) {cnt+=s.b[j]; if (cnt>=x) {res=j; goto OUT2;}}        }} out2:for (int i=l;i<=r;++i) S.delete (A[i]);  return res;    }for (int i=l;i<=r[num[l]];++i) S.insert (A[i]); for (int i=l[num[r]];i<=r;++i) S.insert (A[i]);    int lb=num[l],rb=num[r]-1; for (int i=1;;        ++i) {cnt+= (t[rb].sumv[i]-t[lb].sumv[i]+s.sumv[i]);            if (cnt>=x) {cnt-= (t[rb].sumv[i]-t[lb].sumv[i]+s.sumv[i]); for (int j=l2[i];;          ++J) {cnt+= (t[rb].b[j]-t[lb].b[j]+s.b[j]), if (cnt>=x) {res=j; goto out;}} }} out:for (int i=l;i<=r[num[l]];++i) S.delete (A[i]); for (int i=l[num[r]];i<=r;++i) S.delete (A[i]); return res;}  void Makeblock () {int sz=sqrt (n), if (!sz) sz=1;for (; sum*sz<n;++sum) {l[sum]=r[sum-1]+1; r[sum]=sum*sz;  for (int i=l[sum];i<=r[sum];++i) num[i]=sum; }l[sum]=r[sum-1]+1; r[sum]=n;for (int i=l[sum];i<=r[sum];++i) Num[i]=sum;}  void Val_mb () {int tot=1,sz=sqrt (EN2), if (!sz) sz=1;for (; tot*sz<en2;++tot) {l2[tot]= (tot-1) *sz+1;  int R=tot*sz;  for (int i=l2[tot];i<=r;++i) Num2[i]=tot; }l2[tot]= (tot-1) *sz+1;for (int i=l2[tot];i<=en2;++i) Num2[i]=tot;}  void Init_ts () {for (int i=1;i<=sum;++i) {t[i]=t[i-1]; for (int j=l[i];j<=r[i];++j) t[i].  Insert (A[j]); }}int Main () {R (n); R (m); En=n; Makeblock (); for (int i=1;i<=n;++i) {R (T[I].V), t[i].p=i;} getchar (); for (int i=1;i<=m;++i) {Op[i]=getchar (); R (Xs[i]);  R (Ys[i]);  if (op[i]== ' Q ') R (Ks[i]);  else {t[++en].v=ys[i]; t[en].p=en;}    }sort (t+1,t+en+1); ma[a[t[1].p]=++en2]=t[1].v;        for (int i=2;i<=en;++i) {if (T[I].V!=T[I-1].V) ++en2;      MA[A[T[I].P]=EN2]=T[I].V; }VAL_MB (); Init_ts ();    En=n;        for (int i=1;i<=m;++i) {if (op[i]== ' C ') {++en; for (int j=num[xs[i]];j<=sum;++j) t[j]. Delete (A[xs[i]]), t[j].    Insert (A[en]);        A[xs[i]]=a[en]; }else P (ma[kth (xs[i],ys[i],ks[I])), puts (""); }return 0;}

"chunking" "Weights chunked" bzoj1901 Zju2112 Dynamic Rankings

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.