HDU 3874 Necklace 离线+树状数组
2014-09-03 14:19
381 查看
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3103 Accepted Submission(s): 1063
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
Source
2011 Multi-University Training
Contest 4 - Host by SDU
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,l,h) for(int i=(l);i<=(h);++i) #define DWN(i,h,l) for(int i=(h);i>=(l);--i) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define MAX3(a,b,c) max(a,max(b,c)) #define MAX4(a,b,c,d) max(max(a,b),max(c,d)) #define MIN3(a,b,c) min(a,min(b,c)) #define MIN4(a,b,c,d) min(min(a,b),min(c,d)) #define PI acos(-1.0) #define INF 0x7FFFFFFF #define LINF 1000000000000000000LL #define eps 1e-8 typedef long long ll; const int mn=50000+100; const int mm=200000+100; struct node{ int l,id; }; ll f[mn],res[mm]; ll a[mn],pre[1000010]; vector<node> p[mn]; int n,m; inline int lowbit(int i) { return i&(-i); } void add(int i,ll x) { while(i<=n) { f[i]+=x; i+=lowbit(i); } } ll query(int i) { ll ret=0; while(i>0) { ret+=f[i]; i-=lowbit(i); } return ret; } void solve() { node temp; MST(pre,-1); CLR(f); FOR(i,1,n) { if(pre[a[i]]!=-1) add(pre[a[i]],-a[i]); pre[a[i]]=i; add(i,a[i]); REP(j,p[i].size()) { temp=p[i][j]; res[temp.id]=query(i)-query(temp.l-1); } } } int main() { int T; cin>>T; while(T--) { scanf("%d",&n); FOR(i,1,n) { scanf("%I64d",&a[i]); p[i].clear(); } node temp; int r; scanf("%d",&m); REP(i,m) { scanf("%d%d",&temp.l,&r); temp.id=i; p[r].PB(temp); } solve(); REP(i,m) printf("%I64d\n",res[i]); } return 0; }
相关文章推荐
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
- HDU 3874 Necklace(树状数组的离线操作)
- HDU 3874 Necklace 树状数组 + 离线处理
- HDU 3874 Necklace(树状数组的离线操作)
- HDU 3874 Necklace(树状数组离线处理)
- HDU 3333&&HDU-3874 Necklace 离线树状数组
- hdu 3874 Necklace 树状数组 离线操作
- HDU 3874 Necklace(树状数组离线处理)
- hdu 3874 Necklace (树状数组)
- HDU 3874 Necklace (树状数组)
- hdu-3874-Necklace-(树状数组)
- HDU 3874 树状数组 + 离线处理
- hdu 3874 Necklace(树状数组的灵活应用)
- HDU(3874)树状数组+离线
- HDU 3874 Necklace 树状数组的应用
- hdu 3874 树状数组 离线处理
- hdu 3874 Necklace 求数组任意区间和(相同元素只算一次) 树状数组+离线算法
- HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)
- HDU 4358 Boring counting(树状数组离线操作+欧拉序列)
- HDU-3874 Necklace 线段树+离线