xtu数据结构 D. Necklace
D. Necklace
64-bit integer IO format: %I64d Java class name: Main
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
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
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
解题:离线树状数组,为什么要把y坐标从小到大进行排序呢?因为树状数组的特性所致,右边的节点可能包含左边的节点的值,所以从左往右,不断去掉重复的数值更简便。
离线处理即先一次性把所有询问存储起来。最后一次性回复。用一个数组pre[i]记录元素d[i]距离i最近的一次出现的下标。pre[i] == -1即表示该元素d[i]目前是唯一的,不存在重复元素的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct query {
int x,y,id;
} q[maxn];
LL tree[];
int pre[],loc[],n,m,d[];
LL ans[maxn];
bool cmp(const query &a,const query &b) {
return a.y < b.y;
}
int lowbit(int x) {
return x&(-x);
}
void update(int x,int val) {
for(; x <= n; x += lowbit(x)) {
tree[x] += val;
}
}
LL sum(int x) {
LL ans = ;
for(; x; x -= lowbit(x))
ans += tree[x];
return ans;
}
int main() {
int t,i,j;
scanf("%d",&t);
while(t--) {
memset(loc,-,sizeof(loc));
memset(tree,,sizeof(tree));
scanf("%d",&n);
for(i = ; i <= n; i++) {
scanf("%d",d+i);
pre[i] = loc[d[i]];
loc[d[i]] = i;
update(i,d[i]);
}
scanf("%d",&m);
for(i = ; i <= m; i++) {
scanf("%d %d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q+,q+m+,cmp);
int y = ;
for(i = ; i <= m; i++) {
for(j = y+; j <= q[i].y; j++) {
if(pre[j] != -) update(pre[j],-d[j]);
}
y = q[i].y;
ans[q[i].id] = sum(q[i].y) - sum(q[i].x-);
}
for(i = ; i <= m; i++) {
printf("%I64d\n",ans[i]);
}
}
return ;
}
xtu数据结构 D. Necklace的更多相关文章
- xtu数据结构 H. City Horizon
H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java cl ...
- xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- xtu数据结构 G. Count the Colors
G. Count the Colors Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- xtu数据结构 B. Get Many Persimmon Trees
B. Get Many Persimmon Trees Time Limit: 1000ms Memory Limit: 30000KB 64-bit integer IO format: %lld ...
- xtu数据结构 C. Ultra-QuickSort
C. Ultra-QuickSort Time Limit: 7000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...
- 数据结构(主席树,Bit):XTU 1247/COGS 2344. pair-pair
pair-pair 输入文件:pair-pair.in 输出文件:pair-pair.out 简单对比 时间限制:7 s 内存限制:64 MB Time Limit : 7000 MS M ...
- CH1807 Necklace【Hash】【字符串】【最小表示法】
1807 Necklace 0x18「基本数据结构」练习 背景 有一天,袁☆同学绵了一条价值连城宝石项链,但是,一个严重的问题是,他竟然忘记了项链的主人是谁!在得知此事后,很多人向☆同学发来了很多邮件 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
随机推荐
- git push 冲突
git commit -am "test 3" git pull 用自己的:git checkout --ours xxxx路径xxxx 用他人的:git checkout --t ...
- ubuntu中wine下安装QQ
原文:http://jingyan.baidu.com/article/359911f55da27057fe0306d8.html 可以把win改成最新版
- spring mvc添加静态资源访问时@Controller无效的解决
web.xml中的url-pattern设置为/,添加mvc:resources访问静态资源时,@Controller无效的问题 web.xml: <servlet> <servle ...
- 浏览器详谈及其内部工作机制 —— web开发必读
浏览器介绍 如今,浏览器格局基本上是五分天下,分别是:IE.Firefox.Safari.Chrome.Opera,而浏览器引擎就更加集中了,主要是四大巨头:IE的浏览器排版引擎Trident,目前随 ...
- CDN加速静态文件服务器的访问
1.用于加速用户下载资源的速度. 简单来说,CDN相当于一个中间代理,原来我们需要请求某个网址比如www.baidu.com,请求会直接发送至百度的服务器上,假如请求者在新疆,但百度的服务器在北京,这 ...
- 【算法基础】欧几里得gcd求最大公约数
package Basic; import java.util.Scanner; public class Gcd { public static void main(String[] args) { ...
- JS原型、原型链、构造函数、实例与继承
https://cloud.tencent.com/developer/article/1408283 https://cloud.tencent.com/developer/article/1195 ...
- 2006: C语言实验——拍皮球
2006: C语言实验——拍皮球 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 231 Solved: 162[Submit][Status][Web ...
- C++函数调用过程深入分析<转>
转自http://blog.csdn.net/dongtingzhizi/article/details/6680050 C++函数调用过程深入分析 作者:靠谱哥 微博:洞庭之子-Bing 0. 引言 ...
- Linux部署多个tomcat
Linux部署多个tomcat 1.环境:1.1. Centos 5.01.2.Tomcat 5.5.17 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2.2.不同的to ...