HDU3874 /HDU3333 树状数组 区间求不重复数和
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4604 Accepted Submission(s): 1581
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.
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.
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
7
14
1
3
6
给一串数,若干询问,每次询问一个区间内不重复数字之和。
/*
把询问的几个区间先存起来,将其按照区间右端点从小到大排序,这样当去掉某一区间内的重复值时就不会
影响其他区间,因为其他区间的右端点要么比他小,要么比他大,比他小的不会受影响(树状数组向上更新)
,比他大的恰好也要去重。去重时利用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 树状数组 区间求不重复数和的更多相关文章
- hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场
题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 树状数组区间加法&区间求和操作
树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询
题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...
- hdu 1116 敌兵布阵(树状数组区间求和)
题意: 给出一行数字,然后可以修改其中第i个数字,并且可以询问第i至第j个数字的和(i <= j). 输入: 首行输入一个t,表示共有t组数据. 接下来每行首行输入一个整数n,表示共有n个数字. ...
随机推荐
- sql 数字转人民币大写函数(两种方法)
,)) returns @rmb table( 亿 ) ,仟万 ) ,佰万 ) ,拾万 ) ,万 ) ,仟 ) ,佰 ) ,拾 ) ,元 ) ,角 ) ,分 )) as begin insert in ...
- ubuntu安装中文支持
sudo apt-get install language-pack-zh-hant language-pack-zh-hans
- Liferay 6.2 改造系列之十九:修改站点设置的表单内容
在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sections that will b ...
- Laravel之Service Container服务容器
managing class dependencies and performing dependency injection. Dependency injection is a fancy phr ...
- 如何创建一个Android项目
第一步: File -->New ---->Android Application Project 点击创建 第二步:接下来是几个下拉选择框. Minimum Required SDK 是 ...
- express-5 质量保证(2)
跨页测试 跨页测试更有挑战性,因为需要你控制和观测浏览器. 现在设置一个跨页测试情境的例子.比如,你的网站上有一个包含联系表单的Request Group Rate页面.营销部门想知道客户是从哪个页面 ...
- linux TCP Wrappers
1. TCP Wrapper简介 (出处:http://www.softpanorama.org/Net/Network_security/TCP_wrappers/) (维基百科)TCP Wrapp ...
- 餐厅系统app版
- Python基础2- Hello,world
第一个程序Hello,world! 交互式编程:在终端窗口中输入:python回车后直接进入python交互模式,然后在提示符>>>后面输入:print 'Hello,world!' ...
- 【转】CentOS6.3安装Broadcom无线网卡驱动
转自: http://blog.csdn.net/jimanyu/article/details/9697833 下面是具体的步骤 一:确定无线网卡的型号,驱动下载 第一步要确定机子的无线网卡型号是什 ...