hdu3333(线段树)
区间更新,单点查询。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
using namespace std;
#define N 30300
#define SN 300300
struct node
{
int x,y;
int key;
int id;
}line[N*]; map<int,int>cao;
__int64 sum[N];
__int64 ans[];
int l[SN],r[SN];
__int64 num[SN]; int cmp(node t,node t1)
{
if(t.y==t1.y)
{
if(t.x==t1.x) return t.key>t1.key;
return t.x>t1.x;
}
return t.y<t1.y;
} void build(int tl,int tr,int s)
{
l[s]=tl; r[s]=tr; num[s]=;
if(tl==tr) return ;
int mid=(tl+tr)/;
build(tl,mid,*s);
build(mid+,tr,*s+);
} void update(int tl,int tr,int x,int s)
{
if(l[s]==tl&&tr==r[s])
{
num[s]+=x;
return;
}
int mid=(l[s]+r[s])/;
if(tr<=mid) update(tl,tr,x,*s);
else if(tl>mid) update(tl,tr,x,*s+);
else
{
update(tl,mid,x,*s);
update(mid+,tr,x,*s+);
}
} __int64 query(int x,int s)
{
if(l[s]==r[s])
{
return num[s];
}
num[*s]+=num[s];
num[*s+]+=num[s];
num[s]=;
int mid=(l[s]+r[s])/;
if(x<=mid) return query(x,*s);
else query(x,*s+);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cao.clear();
memset(sum,,sizeof(sum));
int cnt=;
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
sum[i]=sum[i-]+tmp;
if(cao[tmp]==)
{
cao[tmp]=i;
}
else
{
line[cnt].x=cao[tmp];
line[cnt].y=i;
line[cnt].key=tmp;
cao[tmp]=i;
cnt++;
}
}
int m;
scanf("%d",&m);
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
line[cnt].id=i;
line[cnt].x=x;
line[cnt].y=y;
line[cnt].key=-;
cnt++;
}
sort(line,line+cnt,cmp);
build(,n,);
for(int i=;i<cnt;i++)
{
if(line[i].key==-)
{
__int64 tmp=query(line[i].x,);
ans[line[i].id]=sum[line[i].y]-sum[line[i].x-]-tmp;
}
else
{
update(,line[i].x,line[i].key,);
}
}
for(int i=;i<m;i++)
printf("%I64d\n",ans[i]);
}
return ;
}
hdu3333(线段树)的更多相关文章
- Hdu-3333 Turning Tree (离线树状数组/线段树)
Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法 HDU-3333 Turing ...
- HDU3333 Turing Tree(线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...
- ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- HDU 4630-No Pain No Game(线段树+离线处理)
题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- HNCU专题训练_线段树(1)
1.内存控制2.敌兵布阵4.广告牌5.区间第k大数(模板题)6.just a Hook7.I Hate It8.动态的最长递增子序列(区间更新题)9.图灵树10.覆盖的面积14.买票问题16.村庄问题 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
随机推荐
- Java NIO 与 基于reactor设计模式的事件处理模型
Java NIO非堵塞应用通常适用用在I/O读写等方面,我们知道,系统运行的性能瓶颈通常在I/O读写,包括对端口和文件的操作上,过去,在打开一个I/O通道后,read()将一直等待在端口一边读取字节内 ...
- 利用URLRewriter.dll 实现ASP.NET实现伪静态
大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有坏,对于访 ...
- webvector将html转为svg或者png图片的工具
有些js较多,html组织不好的页面转换起来很不理想,cnblog转换的还不错 http://cssbox.sourceforge.net/webvector/
- Red hat Linux 安装Node.js 源码安装
1. 下载源码包 http://nodejs.org/dist/v0.10.29/node-v0.10.29.tar.gz 2.准备安装环境,>python2.6, gcc, g++ pytho ...
- C# 序列化 Serialize 的应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 你必须知道的ADO.NET
原文:http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html 1. 什么是ADO.NET? 简单的讲,ADO.NET是一组允 ...
- ASP.NET 应用程序安全
原文:http://msdn.microsoft.com/zh-cn/magazine/hh708755.aspx 一.跨站点脚本 简介 XSS 攻击是指将脚本恶意注入用户的浏览会话,这通常在用户不知 ...
- A Product Array Puzzle
Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i ...
- HDU 1217 Arbitrage (Floyd)
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
- sql 泡沫 或者 递归查询
if object_id('[tb]') is not null drop table [tb] go ),parentid int) insert [tb] ,N union all ,N unio ...