Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4604    Accepted Submission(s): 1581

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
题意:
给一串数,若干询问,每次询问一个区间内不重复数字之和。
代码:
 /*
把询问的几个区间先存起来,将其按照区间右端点从小到大排序,这样当去掉某一区间内的重复值时就不会
影响其他区间,因为其他区间的右端点要么比他小,要么比他大,比他小的不会受影响(树状数组向上更新)
,比他大的恰好也要去重。去重时利用map。
*/
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<algorithm>
#include<cmath>
const int MAXN=;
const int MAXM=;//MAXM,MAXN改为30005,1000005就是HDU3333题题解
long long A[MAXN];
long long NE[MAXN];
int t,n,m;
struct Necklace
{
int R,L,id;//区间右端点,区间左端点,序号
}neck[MAXM];
bool cmp(Necklace a,Necklace b)
{
if(a.R==b.R)
return a.L<b.L;
else return a.R<b.R;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int id,long long c)
{
while(id<=MAXN)
{
A[id]+=c;
id+=lowbit(id);
}
}
long long sum(int id)
{
long long s=;
while(id>)
{
s+=A[id];
id-=lowbit(id);
}
return s;
}
using namespace std;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&NE[i]);
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d%d",&neck[i].L,&neck[i].R);
// if(neck[i].L>neck[i].R)
// swap(neck[i].L,neck[i].R);
neck[i].id=i;
}
sort(neck,neck+m,cmp);
int p=;
map<int,int>mp;
long long ans[MAXM];
memset(A,,sizeof(A));
for(int i=;i<m;i++)
{
while(p<=neck[i].R)//小于等于区间右端点的
{
long long x=NE[p];
if(mp[x]!=)//如果前面出现过此X值就减去旧的加上新的
add(mp[x],-x);
add(p,x);
mp[x]=p;
p++;
}
ans[neck[i].id]=sum(neck[i].R)-sum(neck[i].L-);
}
for(int i=;i<m;i++)
printf("%lld\n",ans[i]);
}
return ;
}

HDU3874 /HDU3333 树状数组 区间求不重复数和的更多相关文章

  1. hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场

    题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...

  2. HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用

    这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...

  3. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. 树状数组区间加法&区间求和操作

    树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...

  5. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  6. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

  7. 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询

    题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...

  8. HDU 1394 树状数组+离散化求逆序数

    对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...

  9. hdu 1116 敌兵布阵(树状数组区间求和)

    题意: 给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j). 输入: 首行输入一个t,表示共有t组数据. 接下来每行首行输入一个整数n,表示共有n个数字. ...

随机推荐

  1. HTML5_ScrollInToView方法

    scrollIntoView(ture)元素上边框与视窗顶部齐平 scrollIntoView(false)元素下边框与视窗底部齐平 <html> <head> <tit ...

  2. 【POI】使用POI处理xlsx的cell中的超链接 和 插入图片 和 设置打印区域

    使用POI对xlsx中插入超链接和 插入图片 package com.it.poiTest; import java.awt.image.BufferedImage; import java.io.B ...

  3. JMeter中的场景执行持续时间设置

    jmeter之调度器配置 JMeter的线程组设置里有一个调配器设置,用于设置该线程组下脚本执行的开始时间.结束时间.持续时间及启动延迟时间.当需要半夜执行性能测试时会用到这个功能. 设置调度器配置, ...

  4. LR检查点的if (status == LR_FAIL)写法

    C语言例子:登录失败返回一个信息 int status = web_url("Login", "URL=https://secure.computing.com//log ...

  5. LoadRunner上传文件脚本

  6. 网页打印A4纸-----表格在跨页时自动换页打印的实现 (转)

    在最近所做的一个项目中,需要通过网页来打印不少的表单,但是又不想每个打印页签各占用一个页面,这样就需要生存很多不同的冗余页面,为了减少冗余,所有的表单通过jquery的页签tab来实现的. 一 :基本 ...

  7. AOP静态代理解析1-标签解析

    AOP静态代理使用示例见Spring的LoadTimeWeaver(代码织入) Instrumentation使用示例见java.lang.instrument使用 AOP的静态代理主要是在虚拟机启动 ...

  8. Swift3.0语言教程使用字符串创建和初始化字符串

    Swift3.0语言教程使用字符串创建和初始化字符串 Swift3.0语言教程使用字符串创建和初始化字符串,在编程语言中,字面值是很常见的数据描述形式.人们可以通过字面所表达的意思,获知其含义,尤其是 ...

  9. css3写箭头

    左箭头 .left-arrow { position: absolute; left: 6%; top: 31%; overflow: hidden; zoom:; width: 0.4rem; he ...

  10. 2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    I. Sale in GameStore(贪心) time limit per test 2 seconds memory limit per test 512 megabytes input sta ...