Necklace(树状数组+离线操作)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3874
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3929 Accepted Submission(s): 1296
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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000005
#define M 50005
#define numq 200005
#define ll long long
int Last[N];
ll shusz[M];
int gaga[M];
ll ans[numq];
struct Q {
int l ;
int r;
int id;
bool operator < (const Q a) const
{
return r<a.r;
}
}qq[numq];
int lb(int i)
{
return i&(-i);
}
void add(int j , int t)
{
for(int i =j ;i < M ;i+=lb(i))
{
shusz[i]+=t;
}
}
ll sum (int x)
{
ll ans = ;
for(int i = x ; i > ; i-=lb(i))
{
ans+=shusz[i];
}
return ans;
}
ll sum(int x , int y)
{
ll ans = sum(y)-sum(x-);//注意是x-1
return ans;
}
int main()
{
int T ;
scanf("%d",&T);
for(int i = ;i < T ; i++)
{
int n;
scanf("%d",&n);
int tm;
for(int j = ; j <= n ; j++)
{
scanf("%d",&tm);
gaga[j] = tm;
}
int m ;
scanf("%d",&m);
for(int j = ;j <= m ;j++)
{
int l , r ;
scanf("%d%d",&l,&r);
qq[j].l = l ;
qq[j].r = r;
qq[j].id = j;
}
memset(Last,-,sizeof(Last));
memset(ans,,sizeof(ans));
memset(shusz,,sizeof(shusz));
sort(qq+,qq+m+);
int cur = ;//记录扫描到第几个询问
for(int j = ; j <= n ; j++)//扫描n个点
{
if(Last[gaga[j]] != -)
add(Last[gaga[j]], -gaga[j]);
Last[gaga[j]] = j;
add(j, gaga[j]);
while(j == qq[cur].r)
{
ans[qq[cur].id] = sum(qq[cur].l, qq[cur].r);
cur++;
}
}
for(int j = ; j <= m; j++)
printf("%lld\n", ans[j]);
}
return ;
}
Necklace(树状数组+离线操作)的更多相关文章
- HDU3874Necklace(树状数组+离线操作)
此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...
- HDU---4417Super Mario 树状数组 离线操作
题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...
- bzoj 1878 SDOI2009树状数组 离线操作
本来想写2120的,结果想起来了这个 我们先对于询问左端点排序,用树状数组存区间字母个数,对于每种字母, 第一次出现的位置记录为1,剩下的记录为0,然后记录下,每种颜色 后面第一个和他相同颜色的位置 ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SPOJ3267--D-query (树状数组离线操作)
题意查询区间 [l,r]内有多少个不同的数字 先把所有询问按 右端点进行排序,然后离线操作.如果该位置的数字 已经出现过那么把前一个位置-1,当前位置+1.扫一遍输出. #include <cs ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- 【树状数组】Bzoj1878[SDOI2009] HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
随机推荐
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- HHVM源码剖析
一.前言 hhvm源码中充满了很多C++11的新特性,并且使用了各种设计模式如工厂,模板方法等,利用智能指针包裹指针,让delete没有肆意的出现 模板,继承,explicit,纯虚函数的出现令代码中 ...
- 插值查找C++
和上一篇折半查找很类似,只有四则运算不一样,思想类似. 只是在插值查找的过程中,考虑了查找键的值. #include <iostream> using namespace std; //需 ...
- Jmeter+Ant+Jenkins接口自动化测试(一)_环境部署
前言: 2017年最后一个月份,今天抽出时间把之前的一些记录分享出来,也为今年画上个简单的句号吧,无论好与坏,无论成功与失败,简单的记忆,不要留下点点空白. 特别提示: 知识是用来分享的,但是也要尊重 ...
- JAVA的命名方式 ,JAVA的第一个打印时间的程序
本文内容来自:Java的命名方式 ,Java的第一个打印时间的程序 – Break易站 1. Java的命名方式 Java采用了一种全新的方法来避免上述所有问题·为了给一个类库生成不会与其他名字混淆的 ...
- css动画-音频跳动
css动画-音频跳动 ---------------------------------------------------------------------------- ------------ ...
- python3基础(一)
1. python文件主程序入口文件一般来要申明python路径,编码信息,作者说明等: #!/usr/bin/env python # _*_ coding: utf-8 _*_ # Author: ...
- 第二章:Python基础の快速认识基本数据类型和操作实战
本课主题 字符串和操作实战 二进制操作实战 List 列表和操作实战 Tuple 元組和操作实战 Dict 字典和操作实战 作業需求 引言 这遍文章简单介绍了 Python 字符串和集合的方法和应用, ...
- mysql之其他
一 IDE工具介绍 下载链接:https://pan.baidu.com/s/1bpo5mqj 掌握: 1. 测试+链接数据库 2. 新建库 3. 新建表,新增字段+类型+约束 4. 设计表:外键 5 ...
- redux入门指南
前言:大概一个月没有写博客了,这两天正好是周末,就写点东西来梳理下之前几个月的所写与所得; 大概两个月前,学习了一下 redux ,还是一点难度的,花了我一天的时间来搞明白他, 但是都没怎么记录,今天 ...