# Data structure (Chairman tree): COGS 2213. K-String

Source: Internet
Author: User

2213. K String

★★★★ input file: `bzoj_4504.in` output file: `bzoj_4504.out` Simple comparison
Time limit: s memory limit: MB

"Title description" The rabbits are playing K-string games. First, they take a sequence of numbers of length n, select one of the successive substrings, and then count the sum of all the numbers in its substring (note that the repeated numbers here are counted only once). The rabbits wondered, in all successive substrings of this number sequence, to count the sum of all its numbers in the way described above, and the sum of the number K. "Input format" the first line, two integers n and K, respectively, the length of the number of n sequence and want to count the K-large and the next row N number a_i, indicating that the number sequence "output format" line an integer, indicating the K-large and "sample input"

9 {

3-2 1 2 2 1 3-2

"Sample Output"

4

Prompted

1 <= N <= 100000, 1 <= k <= 200000, 0 <= |a_i| <= 10^9 Data guarantees the existence of K-large and

and Hdu 5654 Xiaoxin and his watermelon candy are almost identical.

`1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn=100010;8 Const intinf=23333333;9 inthash[maxn],a[maxn],n,k;Ten intHEAD[MAXN],NXT[MAXN],PRE[MAXN]; One intrt[maxn],pos[maxn<<6]; A intch[maxn<<6][2],cnt; - Long Longlx[maxn<<6],tr[maxn<<6]; - voidPUSH_UP (intx) { the     intl=ch[x][0],r=ch[x][1]; -tr[x]=tr[l]+Tr[r]; -     if(lx[l]>tr[l]+Lx[r]) { -lx[x]=Lx[l]; +pos[x]=Pos[l]; -     } +     Else{ Alx[x]=tr[l]+Lx[r]; atpos[x]=Pos[r]; -     } - } - voidBuild (int&rt,intLintR) { -rt=++CNT; -     if(l==R) { inpos[rt]=l; -         if(!Pre[l]) { totr[rt]=Hash[a[l]]; +lx[rt]=Hash[a[l]]; -         } the         return; *     } \$     intMid= (l+r) >>1;Panax NotoginsengBuild (ch[rt][0],l,mid); -Build (ch[rt][1],mid+1, R); the push_up (RT); + } A  the voidUpdate (intprint&rt,intLintRintGintd) { +rt=++CNT; -tr[rt]=tr[pr]+D; \$     if(l==R) { \$pos[rt]=G;  -lx[rt]=Tr[rt]; -         return; the     } -ch[rt][0]=ch[pr][0];Wuyich[rt][1]=ch[pr][1]; the     intMid= (l+r) >>1; -     if(G<=mid) Update (ch[pr][0],ch[rt][0],l,mid,g,d); Wu     ElseUpdate (ch[pr][1],ch[rt][1],mid+1, r,g,d); - push_up (RT); About } \$ structdata{intXLong Longy;}; -Data Query (intTintLintRintXinty) - { -     if(L==x && r==y)return(data) {pos[t],lx[t]}; A     intMid= (l+r) >>1; +     if(Y<=mid)returnQuery (ch[t][0],l,mid,x,y); the     Else if(x>mid) { -Data Tmp=query (ch[t][1],mid+1, r,x,y); \$         return(data) {tmp.x,tmp.y+tr[ch[t][0]]}; the     } the     Else{ theData Tmp1=query (ch[t][0],l,mid,x,mid); theData Tmp2=query (ch[t][1],mid+1, r,mid+1, y); -         if(tmp1.y>tmp2.y+tr[ch[t][0]])returnTmp1; in         Else return(data) {tmp2.x,tmp2.y+tr[ch[t][0]]}; the     } the } About  the structnode{intI,l,r,x;Long Longy;}; the BOOL operator< (Constnode& A1,Constnode&A2) the{returna1.y<a2.y;} +Priority_queue<node>Q; -  the intMain () {BayiFreopen ("bzoj_4504.in","R", stdin); theFreopen ("Bzoj_4504.out","W", stdout); thescanf"%d%d",&n,&k); -      for(intI=1; i<=n;i++) -scanf"%d",&a[i]); the      for(intI=1; i<=n;i++) thehash[i]=A[i]; theSort (hash+1, hash+n+1); the      for(intI=1; i<=n;i++) -A[i]=lower_bound (hash+1, hash+n+1, A[i])-Hash; the      for(intI=1; i<=n;i++){ thepre[i]=Head[a[i]]; thenxt[head[a[i]]]=i;94head[a[i]]=i; the     } theBuild (rt[1],1, n); the      for(intI=1; i<n;i++){98Update (rt[i],rt[i+1],1, n,i+1,-Hash[a[i]]); About         if(Nxt[i]) Update (rt[i+1],rt[i+1],1, N,nxt[i],hash[a[i]]); -     }101      for(intI=1; i<=n;i++){102Data Tmp=query (Rt[i],1, n,i,n);103 Q.push (node) {i,i,n,tmp.x,tmp.y});104     } the     Long Longans;106      for(intI=1; i<=k;i++){107Node T=q.top (); Q.pop (); ans=T.y;108         if(t.l!=t.x) {109Data Tmp=query (RT[T.I],1, n,t.l,t.x-1); theQ.push (node) {t.i,t.l,t.x-1, tmp.x,tmp.y});111         } the         if(t.r!=t.x) {113Data Tmp=query (RT[T.I],1, n,t.x+1, T.R); theQ.push (node) {t.i,t.x+1, t.r,tmp.x,tmp.y}); the         } the     }117printf"%lld\n", ans);118     return 0;119}`

Have time must be re-dozen!!!

Data structure (Chairman tree): COGS 2213. K-String

Related Keywords:

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.