Necklace
Time limit:15000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3923 Accepted Submission (s): 1292
Problem Descriptionmery has a beautiful necklace. The necklace is a made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value is the same, so if the and more balls has the same beautiful value, we just Coun T it once. We define the beautiful value of some interval [x, y] as F (x, y). F (x, y) is calculated as the sum of the beautiful value from the Xth ball to the Yth ball and the same value are only Counte D ONCE. For example, if the necklace are 1 1 1 2 3 1, we have f (1,3) =1, F (2,4) =3, F (2,6) =6.
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She'll give you M intervals [l,r] (1<=l<=r<=n) and your must tell her F (l,r) of them.
Inputthe First line is T (t<=10), representing the number of test cases.
For each case, the first line is a number n,1 <=n <=50000, indicating the number of the magic balls. The second line contains n non-negative integer numbers not greater 1000000, representing the beautiful value of the N Bal Ls. The third line has a number M, 1 <=m <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
Outputfor each query, output a line contains an integer number, representing the result of the query.
Sample Input261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
Sample Output3714136
Source2011 multi-university Training Contest 4-host by SDU The weakened version of the title. Because the data array can be stored, it does not need to be discretized. The rest is similar. Too lazy to write >_<
/************************************************************************* > File name:code/hdu/3333.cpp > AUTHOR:111QQZ > Email: [email protected] > Created time:2015 August 07 Friday 17:04 07 seconds ************************ ************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intINF =0x7fffffff;Const intn=5e4+3; LL C[n]; LL N,m,qur; LLref[N]; LL Pre[n]; LL Ori[n];structq{LL Val,id;} Q[n];structs{LL x, y; LL Id,ans;} s[200005];BOOLCMP (Q a,q b) {if(A.val<b.val)return true; return false;}BOOLCMP2 (S a,s b) {if(A.Y<B.Y)return true; return false;} LL Lowbit (intx) { returnx& (-x);}voidUpdate (LL x,ll delta) { for(LL i = x; i <= n; i = i +lowbit (i)) {C[i]= C[i] +Delta; }}ll sum (ll x) {ll res=0 ; for(LL i = x; I >=1; i = i-lowbit (i)) {Res= Res +C[i]; } returnRes;}intMain () {intT; CIN>>T; while(t--) {memset (ref,0,sizeof(ref)); Memset (c,0,sizeof(c)); memset (PRE,-1,sizeof(pre));//the array where the last occurrence was markedscanf"%lld",&N); for(LL i =1; I <= N; i++) {scanf ("%lld",&q[i].val); Q[i].id=i; } sort (Q+1, q+n+1, CMP); LL CNT=0; for(LL i =1; I <= N; i++ ) { if(q[i].val!=q[i-1].val) {cnt++; } ref[Q[i].id] =CNT; Ori[q[i].id]=Q[i].val; } scanf ("%lld",&Qur); for(LL i =1; I <= Qur; i++) {scanf ("%lld%lld",&s[i].x,&s[i].y); S[i].id=i; } sort (S+1, s+1+QUR,CMP2); s[0].Y =0; for(LL i =1; I <= Qur; i++) { for(LL j = s[i-1].y+1; J <= S[i].y; J + +) { intTMP =ref[j]; if(pre[tmp]==-1) {update (j,ori[j]); } Else{update (j,ori[j]); Update (pre[tmp],-Ori[j]); } Pre[tmp]=J; } S[s[i].id].ans= SUM (s[i].y)-sum (s[i].x-1); } for(inti =1; I <= Qur; i++) {cout<<s[i].ans<<Endl; } } return 0;}
HDU 3874 Necklace (tree-like array + offline operation)