hdu 3874 Necklace (树状数组+离线操作)
2015-08-08 02:47
387 查看
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3923 Accepted Submission(s): 1292
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each query, output a line contains an integer number, representing the result of the query.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
3
7
14
1
3
6
[align=left]Source[/align]
2011 Multi-University Training Contest 4 - Host by SDU
上一道题的弱化版本。
因为数据数组能存下,不需要离散化。
其他都差不多。
懒得写了 >_<
/************************************************************************* > File Name: code/hdu/3333.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年08月07日 星期五 17时04分07秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #define y0 abc111qqz #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define tm crazy111qqz #define lr dying111qqz using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int inf = 0x7fffffff; const int N=5E4+3; LL c ; LL n,m,qur; LL ref ; LL pre ; LL ori ; struct Q { LL val,id; }q ; struct S { LL x,y; LL id,ans; }s[200005]; bool cmp( Q a,Q b) { if (a.val<b.val) return true; return false; } bool cmp2(S a,S b) { if (a.y<b.y) return true; return false; } LL lowbit ( int x) { return x&(-x); } void update ( 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]; } return res; } int main() { int T; cin>>T; while (T--) { memset(ref,0,sizeof(ref)); memset(c,0,sizeof(c)); memset(pre,-1,sizeof(pre)); //标记上次出现位置的数组 scanf("%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++) { int tmp = 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 ( int i = 1 ; i <= qur ; i++ ) { cout<<s[i].ans<<endl; } } return 0; }
相关文章推荐
- HDOJ 2317 Nasty Hacks
- Android入门——Handler机制
- hdu 3333 Turing Tree (树状数组+离线处理+离散化)
- post 提交数据
- 黑马程序员——数组转换(二进制-八进制-十六进制)-第6天
- OC_NSUseDefault
- 黑马程序员_java_面向对象总结
- 黑马程序员_java_集合总结(下)
- 黑马程序员_java_集合总结(上)
- 黑马程序员_java_反射总结
- 程序员该有的艺术气质—SOLID原则
- 黑马程序员_java_多线程总结(下)
- MySQL调优 ---- IN
- 黑马程序员_java_IO流总结(下)
- leetcode解题笔记-Summary Ranges
- 黑马程序员_java_IO流总结(中)
- 了解Android微信里的WebView是如何实现分享的功能
- 黑马程序员_java_IO流总结(上)
- 黑马程序员_java_API总结
- 字符串模拟利用gson解析与生成json