xtu資料結構 D. Necklace

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   

D. NecklaceTime Limit: 5000msMemory Limit: 32768KB64-bit integer IO format: %I64d      Java class name: Main 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. 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 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. OutputFor each query, output a line contains an integer number, representing the result of the query. Sample Input
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
Sample Output
3714136

 解題:離線樹狀數組,為什麼要把y座標從小到大進行排序呢?因為樹狀數組的特性所致,右邊的節點可能包含左邊的節點的值,所以從左往右,不斷去掉重複的數值更簡便。

離線處理即先一次性把所有詢問儲存起來。最後一次性回複。用一個數組pre[i]記錄元素d[i]距離i最近的一次出現的下標。pre[i] == -1即表示該元素d[i]目前是唯一的,不存在重複元素的。

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f14 using namespace std;15 const int maxn = 210000;16 struct query {17     int x,y,id;18 } q[maxn];19 LL tree[51000];20 int pre[51000],loc[1100000],n,m,d[51000];21 LL ans[maxn];22 bool cmp(const query &a,const query &b) {23     return a.y < b.y;24 }25 int lowbit(int x) {26     return x&(-x);27 }28 void update(int x,int val) {29     for(; x <= n; x += lowbit(x)) {30         tree[x] += val;31     }32 }33 LL sum(int x) {34     LL ans = 0;35     for(; x; x -= lowbit(x))36         ans += tree[x];37     return ans;38 }39 int main() {40     int t,i,j;41     scanf("%d",&t);42     while(t--) {43         memset(loc,-1,sizeof(loc));44         memset(tree,0,sizeof(tree));45         scanf("%d",&n);46         for(i = 1; i <= n; i++) {47             scanf("%d",d+i);48             pre[i] = loc[d[i]];49             loc[d[i]] = i;50             update(i,d[i]);51         }52         scanf("%d",&m);53         for(i = 1; i <= m; i++) {54             scanf("%d %d",&q[i].x,&q[i].y);55             q[i].id = i;56         }57         sort(q+1,q+m+1,cmp);58         int y = 0;59         for(i = 1; i <= m; i++) {60             for(j = y+1; j <= q[i].y; j++) {61                 if(pre[j] != -1) update(pre[j],-d[j]);62             }63             y = q[i].y;64             ans[q[i].id] = sum(q[i].y) - sum(q[i].x-1);65         }66         for(i = 1; i <= m; i++) {67             printf("%I64d\n",ans[i]);68         }69     }70     return 0;71 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.