日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Necklace(树状数组+离线操作)

發布時間:2023/12/8 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Necklace(树状数组+离线操作) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=3874

Necklace

Time Limit: 15000/5000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3929????Accepted Submission(s): 1296


Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count 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 is ONLY COUNTED ONCE. For example, if the necklace is 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 will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.

?

Input The 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 balls. 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.

?

Output For each query, output a line contains an integer number, representing the result of the query.

?

Sample Input 2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5

?

Sample Output 3 7 14 1 3 6 題意: 求區間不重復元素和 題解: 詢問是5個0 所以肯定要找一種可以掃描一次得出結果不會重復計算的算法,這里介紹一種巧妙的方法: 離線操作,所謂離線就是將所有的詢問都讀如后,根據需要進行排序,這里一定要記錄一個id來保存輸入的順序,然后一個ans數組按照輸入順序儲存答案,然后從左到右的掃描一邊將所有的以當前掃描的點為終止點的答案都保存起來,最后按順序輸出ans 數組即可 對于這個題:一般按照右端點排序,因為右端點代表這一個查詢的結束,所以按右端點排序有特殊的意義,因為每次要去除重復的數字,可以考慮設一個last數組,標記其在之前是否出現過,如果出現過的化,將之前出現的地方的這個值更改成0 然后last數組的當前值更新成當前的位置,這樣樹狀數組中存放的值就肯定沒有重復元素了。 這種離線的思想很重要,一定要利用好將所有的數據排序后每次可以將相同的結尾的值一起算出來的性質,指針只用掃描一邊,每向后掃描后都將以這個值結尾的所有結果都處理出來。離線最大的好處是在依次向后更新last數組的時候前面的與其相關的詢問已經處理過了,所以去除前面的值,保留后面的不會影響前面的結果,也不會影響后面的計算。如果不離線,last 數組如果這樣更新的話,會影響到再次涉及前面的區間的和 代碼: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define N 1000005 6 #define M 50005 7 #define numq 200005 8 #define ll long long 9 int Last[N]; 10 ll shusz[M]; 11 int gaga[M]; 12 ll ans[numq]; 13 struct Q { 14 int l ; 15 int r; 16 int id; 17 bool operator < (const Q a) const 18 { 19 return r<a.r; 20 } 21 }qq[numq]; 22 int lb(int i) 23 { 24 return i&(-i); 25 } 26 void add(int j , int t) 27 { 28 for(int i =j ;i < M ;i+=lb(i)) 29 { 30 shusz[i]+=t; 31 } 32 } 33 ll sum (int x) 34 { 35 ll ans = 0 ; 36 for(int i = x ; i > 0 ; i-=lb(i)) 37 { 38 ans+=shusz[i]; 39 } 40 return ans; 41 } 42 ll sum(int x , int y) 43 { 44 ll ans = sum(y)-sum(x-1);//注意是x-1 45 return ans; 46 } 47 int main() 48 { 49 int T ; 50 scanf("%d",&T); 51 for(int i =0 ;i < T ; i++) 52 { 53 int n; 54 scanf("%d",&n); 55 int tm; 56 for(int j = 1 ; j <= n ; j++) 57 { 58 scanf("%d",&tm); 59 gaga[j] = tm; 60 } 61 int m ; 62 scanf("%d",&m); 63 for(int j = 1 ;j <= m ;j++) 64 { 65 int l , r ; 66 scanf("%d%d",&l,&r); 67 qq[j].l = l ; 68 qq[j].r = r; 69 qq[j].id = j; 70 } 71 memset(Last,-1,sizeof(Last)); 72 memset(ans,0,sizeof(ans)); 73 memset(shusz,0,sizeof(shusz)); 74 sort(qq+1,qq+m+1); 75 int cur = 1;//記錄掃描到第幾個詢問 76 for(int j = 1 ; j <= n ; j++)//掃描n個點 77 { 78 if(Last[gaga[j]] != -1) 79 add(Last[gaga[j]], -gaga[j]); 80 Last[gaga[j]] = j; 81 add(j, gaga[j]); 82 while(j == qq[cur].r) 83 { 84 ans[qq[cur].id] = sum(qq[cur].l, qq[cur].r); 85 cur++; 86 } 87 } 88 for(int j = 1; j <= m; j++) 89 printf("%lld\n", ans[j]); 90 } 91 return 0 ; 92 }

?

轉載于:https://www.cnblogs.com/shanyr/p/4725462.html

總結

以上是生活随笔為你收集整理的Necklace(树状数组+离线操作)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。