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不断地收集新的贝壳,因此, 他的项链变 ...
随机推荐
- php date函数
PHP星期几获取代码: 1 date("l"); 2 //data就可以获取英文的星期比如Sunday 3 date("w"); 4 //这个可以获取数字星期比 ...
- ExpandableListView的完美实现,JSON数据源,右边自定义图片
转载请标明出处: http://www.cnblogs.com/dingxiansen/p/8194669.html 本文出自:丁先森-博客园 最近在项目中要使用ExpandableListView来 ...
- 小白的Python之路 day4 软件目录结构规范
软件目录结构规范 为什么要设计好目录结构? "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同 ...
- ubuntu 编译运行 opencv C++ 项目
ubuntu 编译运行 opencv C++ 项目 环境 ubuntu 16.04 opencv3.4.0 一. 编译方法 1)命令行 g++ imageResize.cpp -o resize `p ...
- Linux命令每日一个
2014-3-31 1:39 (1)tree linux以树状的结构显示当前目录及其包含的子目录下的文件 #apt-get install tree #tree //在当前目录下直接使用该命令即可 ...
- vue的挖坑和爬坑之css背景图样式终极解决方法
原问题 #wrapper{ width:100%; height:100%; position:fixed; background-image:url(./img/open_bg.jpg) } 在.v ...
- Java贪吃蛇感想
暑假敲代码的效率真的不高呀,一个这种小游戏从最开始构思到最后实施代码,从最开始的Demo版本到最后的第四版本,花了一个星期了.本想再更新一个版本,加入双人对战模式,还想写个AI版,可是我估计按照现在我 ...
- HTML + CSS短标题(二,三,四文字长度)两端对齐的方式
今天切图碰到了一个看似好弄,却并不好弄的文本两端对齐问题.如图1-1
- vue实现星级评价效果
希望对你们有用,已经自己试过可以的才发布出来的 效果如下: html: <template> <div class="evaStar"> <ul cl ...
- 如何在Spring框架上做开发之Context启动中的“Hook”
1.概述 有些时候,我们需要在spring启动过程中加入一些自己的逻辑,特别是一些基本框架和spring做整合的时候(例如:mybatis-spring-boot-starter),就需要使用Spri ...