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打广告,但是现在我自己都不太好意思去看那篇文章 ...
随机推荐
- associated 2 maps
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content= ...
- 13test02:信用卡校验
/*#include<iostream> using namespace std; void input(); int counter=0,jishu_sum=0,oushu_sum=0, ...
- POJ 2021
#include <iostream> #include <string> #include <algorithm> #define MAXN 105 using ...
- Python之socketserver源码分析
一.socketserver简介 socketserver是一个创建服务器的框架,封装了许多功能用来处理来自客户端的请求,简化了自己写服务端代码.比如说对于基本的套接字服务器(socket-based ...
- 优化DB2缓冲页的大小
零部件日结无法进行下去,建议配置C:\Program Files\SQLLIB目录下的db2cli.ini文件,加入此节:[DMSCNDB]CLIPkg=5 并重启DB2试试 另外,可以在命令行处理器 ...
- hdu 1404/zoj 2725 Digital Deletions 博弈论
暴力打表!! 代码如下: #include<iostream> #include<algorithm> #include<cstdio> #include<c ...
- Eclipse Java EE 创建 Dynamic Web Project
1. 创建一个web工程,此处用eclipse创建(如果对创建web工程很熟悉,可以不看的,本文目的是做一个记录) 1) 打开新建工程对话框,选择Dynamic web Proje ...
- http://www.cnblogs.com/draem0507/archive/2013/02/01/2889317.html
http://www.cnblogs.com/draem0507/archive/2013/02/01/2889317.html
- C语言,一个彩票摇奖程序摇出22选5的中奖号码
摇奖机摇奖,无非就是利用它的随机性,让球从摇奖机中随机地掉出,就成了中奖号码.而C语言中也同样有个rand()函数可以产生随机数,利用这个rand()函数产生的随机数,同样可以代替从摇奖机中随机摇出的 ...
- oracle记录解锁
oracle 怎样查一个表中的记录是否被锁住了 怎么查询一个数据库中有几个表引用了其中某个特定表的主键做为其外键的select t.table_name from user_constraints ...