区间更新,单点查询。

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(线段树)的更多相关文章

  1. Hdu-3333 Turning Tree (离线树状数组/线段树)

    Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法  HDU-3333 Turing ...

  2. HDU3333 Turing Tree(线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...

  3. ACM学习历程——HDU3333 Turing Tree(线段树 && 离线操作)

    Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...

  4. HDU 4630-No Pain No Game(线段树+离线处理)

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  5. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  6. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  7. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  8. HNCU专题训练_线段树(1)

    1.内存控制2.敌兵布阵4.广告牌5.区间第k大数(模板题)6.just a Hook7.I Hate It8.动态的最长递增子序列(区间更新题)9.图灵树10.覆盖的面积14.买票问题16.村庄问题 ...

  9. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

随机推荐

  1. 引擎设计跟踪(九.14.2d) [翻译] shader的跨平台方案之2014

    Origin: http://aras-p.info/blog/2014/03/28/cross-platform-shaders-in-2014/ 简译 translation: 作者在2012年写 ...

  2. 从程序员到CTO的Java技术路线图 (转自安卓巴士)

    在技术方面无论我们怎么学习,总感觉需要提升自已不知道自己处于什么水平了.但如果有清晰的指示图供参考还是非常不错的,这样我们清楚的知道我们大概处于那个阶段和水平. Java程序员 高级特性 反射.泛型. ...

  3. Codeforces Round #248 (Div. 2)C 题

    题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...

  4. Sqli-labs less 23

    第二部分/page-2 Advanced injection Less-23 Sql语句为$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1 ...

  5. ubuntu修改ip、网关、dns等

    一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...

  6. 【译】C++工程师需要掌握的10个C++11特性

    原文标题:Ten C++11 Features Every C++ Developer Should Use 原文作者:Marius Bancila 原文地址:codeproject 备注:非直译,带 ...

  7. delphi 中几种多线程操作方式

    在了解多线程之前我们先了解一下进程和线程的关系 一个程序至少有一个主进程,一个进程至少有一个线程. 为了保证线程的安全性请大家看看下面介绍 Delphi多线程同步的一些处理方案大家可以参考:http: ...

  8. linux使用crontab实现PHP执行定时任务及codeiginter参数传递相关

    http://www.phpddt.com/php/linux-crontab.html crontab: yum install crontabs //安装 说明: /sbin/service cr ...

  9. 解决SecureCRT连接linux超时后断开

    出自:http://blog.csdn.net/zljjava/article/details/20285679 1.从客户端入手: 2.从服务器端入手(需要服务器权限) 修改/etc/ssh/ssh ...

  10. 【poj1006-biorhythms】中国剩余定理

    http://poj.org/problem?id=1006 题意:中国剩余定理的裸题. 题目可转化为求最小的x满足以下条件: x%23=a;x%28=b;x%33=c; 关于中国剩余定理可看我昨天的 ...