区间更新,单点查询。

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. Spring.net Could not load type from string value

    最近有点懒了啊,都没有按时上来博客园更新下,个人觉得遇到难题的时候在这里留下脚印也亦造福他人,进来 晓镜水月 被项目围的团团转,asp.net MVC项目来的,但是我还是不务正业啊,在弄网络爬虫,这个 ...

  2. unset之讲解

    unset (PHP 4, PHP 5) unset — 释放给定的变量 说明¶ void unset ( mixed $var [, mixed $... ] ) unset() 销毁指定的变量. ...

  3. BZOJ 1088

    真是智商不够, 智商题:.... 假如:第1,2个格子已知,然后根据第二列的情况,就可以把所有满足的情况推出来,又萌萌哒.. 无耻攒字数: #include<stdio.h> using ...

  4. 大漠推荐的教程:创建你自己的AngularJS -- 第一部分 Scopes

    创建你自己的AngularJS -- 第一部分 Scopes http://www.html-js.com/article/1863

  5. myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题

    安装包下载: ed2k://|file|[myeclipse.10.0.更新发布].myeclipse-10.0-offline-installer-windows.exe|947752488|73b ...

  6. NSArray block用法

    28.使用block 块遍历整个数组.这个block 需要三个参数,id obj 表示数组中的元素. NSUInteger idx 标示元素的下标, bool *stop 是一个bool类型的参数. ...

  7. isMobile 一个简单的JS库,用来检测移动设备

    点这里 github地址:https://github.com/kaimallea/isMobile Example Usage I include the minified version of t ...

  8. Ms sql server sql优化技巧

    SET STATISTICS PROFILE ON SET STATISTICS IO ON SET STATISTICS TIME ON SELECT * FROM userinfo WHERE N ...

  9. (转)Android之ListView原理学习与优化总结

    转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...

  10. 叠罗汉II

    叠罗汉是一个著名的游戏,游戏中一个人要站在另一个人的肩膀上.为了使叠成的罗汉更稳固,我们应该让上面的人比下面的人更轻一点.现在一个马戏团要表演这个节目,为了视觉效果,我们还要求下面的人的身高比上面的人 ...