区间更新,单点查询。

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. 大漠推荐的教程:创建你自己的AngularJS -- 第一部分 Scopes

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

  2. 100个经典的C算法

    1.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数 为多少? #include<stdio.h&g ...

  3. ssh-copy-id(双机互信)

    最简单的2步骤: ssh-keygen -t rsa 需要输入的地方就回车 ssh-copy-id root@192.168.0.10 详细:ssh-keygen 创建公钥和密钥. ssh-copy- ...

  4. Chapter 5

    1. 2模块导入 3.包导入

  5. JS正则汇总

    1.基本定义: \s:用于匹配单个空格符,包括tab键和换行符; \S:用于匹配除单个空格符之外的所有字符; \d:用于匹配从0到9的数字; \w:用于匹配字母,数字或下划线字符; \W:用于匹配所有 ...

  6. POJ 1491

    #include<iostream> #include<cmath> #include<iomanip> #define MAXN 50 using namespa ...

  7. Baidu和Google搜索引擎使用技巧(转)

    转自:Baidu和Google搜索 http://www.douban.com/note/261208979/ 百度搜索一:基本搜索   二:高级搜索   谷歌搜索一:基本搜索1)可部分匹配也可完全匹 ...

  8. ExtJs布局之table

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  9. BFS+贪心 HDOJ 5335 Walk Out

    题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...

  10. Spark源码分析(二)-SparkContext创建

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3872785.html  SparkContext是应用启动时创建的Spark上下文对象,是一个重要的入口 ...