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 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
随机推荐
- nopCommerce - asp.net开源商城
nopcommerce官网 http://nopcommerce.codeplex.com/ nopCommerce is a open source e-commerce solution that ...
- js实现文本框验证和实现小数的加减乘除
<script type="text/javascript"> //加法 var m=accAdd(1.22,1.22); //减法 var m1=accSub(1.2 ...
- mysql查询问题
需求:根据选择不同的分类id,查找到同时属于选中的分类的文章id sql语句: select result,GROUP_CONCAT(category_id) from (select categor ...
- css布局:左边固定宽度,右边自适应宽度或右侧固定,左侧自适应三种方法
方法一:浮动布局 这种方法我采用的是左边浮动,右边加上一个margin-left值,让他实现左边固定,右边自适应的布局效果 HTML Markup <div id="left" ...
- Objective-C Runtime Reference
This document describes the OS X Objective-C 2.0 runtime library support functions and data structur ...
- vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等
vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...
- shiro 配置拦截规则之后css和js等失效
使用shiro作为平台的权限管理工具,shiro的配置文件如下: package com.ros.config; import java.util.LinkedHashMap;import java. ...
- Electric Motor Manufacturer - Motor Protection: 5 Questions, 5 Answers
I. Selection principle of motor protectorThe Electric Motor Manufacturer stated that the reasonab ...
- AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排
一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...
- WebStorm换主题(护眼)
一.下载喜欢颜色的主题 http://www.phpstorm-themes.com/ 我用的豆沙绿护眼 <scheme name="Solarized Light My" ...