HDOJ 3874 Necklace
来源:互联网 发布:大数据团队人员配置 编辑:程序博客网 时间:2024/06/05 08:59
树状数组求区间内不同元素和
离线考虑,把询问区间按右端点排序,记录每个数上一次出现的位置,如果已经出现过,把上一个删掉,当前的加进去,即只留下最近的一个元素。
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2485 Accepted Submission(s): 880
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.
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.
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
261 2 3 4 3 531 23 52 661 1 1 2 3 531 12 43 5
Sample Output
3714136
Source
2011 Multi-University Training Contest 4 - Host by SDU
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long int LL;struct ASK{ int l,r,id;}ask[200100];int n,q;int pre[1000100];LL tree[50100],val[50100],ans[200100];bool cmp(ASK a,ASK b){ if(a.r!=b.r) return a.r<b.r; return a.l<b.l;}int lowbit(int x){ return x&(-x);}void ADD(int p,int v){ for(int i=p;i<=n+10;i+=lowbit(i)) tree[i]+=v;}LL SUM(int p){ LL sum=0; for(int i=p;i;i-=lowbit(i)) sum+=tree[i]; return sum;}int main(){ int TAT; scanf("%d",&TAT);while(TAT--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&val[i]); scanf("%d",&q); for(int i=0;i<q;i++) { scanf("%d%d",&ask[i].l,&ask[i].r); ask[i].id=i; } sort(ask,ask+q,cmp); memset(tree,0,sizeof(tree)); memset(pre,-1,sizeof(pre)); int cur=1; for(int i=0;i<q;i++) { while(cur<=ask[i].r) { int x=val[cur]; if(pre[x]!=-1) { ADD(pre[x],-x); } ADD(cur,x); pre[x]=cur++; } ans[ask[i].id]=SUM(ask[i].r)-SUM(ask[i].l-1); } for(int i=0;i<q;i++) printf("%I64d\n",ans[i]);} return 0;}
2 0
- HDOJ 3874 Necklace
- HDOJ Accepted Necklace
- hdoj 6030 Happy Necklace
- HDOJ 3874 Necklace 线段树 单点更新 成段查询
- hdoj 2660 Accepted Necklace【DFS】
- HDOJ 1817 Necklace of Beads
- HDOJ 题目3874 Necklace(线段树+离线求区间去重和)
- hdoj 5727 Necklace 二分图 全排列
- HDOJ(HDU).2660 Accepted Necklace (DFS)
- hdu 3874 Necklace
- hdu 3874 Necklace
- hdu 3874 Necklace
- hdu 3874 Necklace
- HDU 3874 Necklace
- HDU 3874 Necklace
- necklace
- HDOJ题目2660 Accepted Necklace(二维01背包)
- 2017 CCPC-WFinal&&HDOJ 6030 Happy Necklace(矩阵快速幂)
- WebService大讲堂之Axis2(5) 会话(session)管理
- spring定时任务配置,以及不执行的解决办法
- makefile--命令
- c#读取SAP数据
- Windows系统相对路径
- HDOJ 3874 Necklace
- 一个数据包的互联网漫游记
- android 邮箱 蓝牙
- xml 绑定到 comboBox (DisplayMember和ValueMember)
- arm__ mov立即数
- ARM内核驱动—驱动程序大揭密-字符设备控制技术
- POJ 2318 点在凸四边形内的判断(叉积+二分)
- 深入理解HTTP协议、HTTP协议原理分析
- es ps ts