区间更新,单点查询。

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*10];map<int,int>cao;
__int64 sum[N];
__int64 ans[100100];
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]=0;if(tl==tr) return ;int mid=(tl+tr)/2;build(tl,mid,2*s);build(mid+1,tr,2*s+1);
}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])/2;if(tr<=mid) update(tl,tr,x,2*s);else if(tl>mid) update(tl,tr,x,2*s+1);else{update(tl,mid,x,2*s);update(mid+1,tr,x,2*s+1);}
}__int64 query(int x,int s)
{if(l[s]==r[s]){return num[s];}num[2*s]+=num[s];num[2*s+1]+=num[s];num[s]=0;int mid=(l[s]+r[s])/2;if(x<=mid) return query(x,2*s);else query(x,2*s+1);
}int main()
{int T;scanf("%d",&T);while(T--){cao.clear();memset(sum,0,sizeof(sum));int cnt=0;int n;scanf("%d",&n);for(int i=1;i<=n;i++){int tmp;scanf("%d",&tmp);sum[i]=sum[i-1]+tmp;if(cao[tmp]==0){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=0;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=-1;cnt++;}sort(line,line+cnt,cmp);build(1,n,1);for(int i=0;i<cnt;i++){if(line[i].key==-1){__int64 tmp=query(line[i].x,1);ans[line[i].id]=sum[line[i].y]-sum[line[i].x-1]-tmp;}else{update(1,line[i].x,line[i].key,1);}}for(int i=0;i<m;i++)printf("%I64d\n",ans[i]);}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/p/4080121.html

hdu3333(线段树)相关推荐

  1. 数据结构---线段树

    线段树 转载请注明出处,谢谢!http://blog.csdn.net/metalseed/article/details/8039326  持续更新中···   一:线段树基本概念 1:概述 线段树 ...

  2. 【转】线段树题目 汇总 讲解(by not only success)

    转载自:http://www.notonlysuccess.com/ 非常喜欢他的代码风格以及简洁的思路,感谢notonlysuccess! PS:他的个人网站好像是上不去了-.- 线段树 很早前写的 ...

  3. ACM大牛总结的线段树专辑

    https://blog.csdn.net/qq_25605637/article/details/46967529 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章 ...

  4. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

  5. 转载自杭电大牛的博客 线段树 绝对经典

    转载自:http://www.notonlysuccess.com/ 不可不看的经典 学线段树必看,大牛很多,给后人留下记录的却没有几个,谢谢这位大牛~! 因为我这最近他博客打不开了...特意从别人那 ...

  6. 线段树模板(来自胡浩大牛)

    http://www.notonlysuccess.com/(今天看二叉树,想回来看看,发现大牛博客进不去...) 如果要学,就要好好学.我copy的,如有错,请看http://www.cnblogs ...

  7. 大牛整理的线段树集锦

     转载自:http://www.notonlysuccess.com/ 膜拜之... [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pk ...

  8. ACM_大牛总结的线段树专辑

    附上原出处:http://blog.csdn.net/qq_25605637/article/details/46967529 [完全版]线段树 这是从大牛那里粘过来的总结,对于刚训练线段树的我来说帮 ...

  9. 二逼平衡树——树套树(线段树套Splay平衡树)

    题面 Bzoj3196 解析 线段树和Splay两棵树套在一起,常数直逼inf,但最终侥幸过了 思路还是比较简单, 在原数组维护一个下标线段树,再在每一个线段树节点,维护一个对应区间的权值Splay. ...

最新文章

  1. 如何让li中的标签内容横向排列
  2. java http请求插件_java http请求工具整理
  3. .NET 中的 Worker Service 入门介绍
  4. robotframework安装_python3.9.0 + robotframework + selenium3 实例体验
  5. Matlab与simulink中的数据类型
  6. myeclipse----JVM运行内存溢出问题解决
  7. OpenCV Contours 使用记录
  8. Linux系统centos6.7上安装libevent
  9. 基于51单片机三路温湿度语音LCD1602液晶显示报警
  10. 服务器e5v2v3性能差距,服务器CPU中的E3、E5的区别,及V2、V3、V5的区别
  11. HTML注册表单的页面制作
  12. 小学计算机课打字游戏,小学信息技术打字游戏教案
  13. mos中Port-Specific的意思
  14. python扫雷游戏设计_Python 扫雷游戏 完整源代码+图片素材
  15. 计算机修改人类记忆曲线,遗忘曲线——揭秘人类记忆存储的奥秘
  16. Mac 此账户尚未用于app store_献给mac新手,Mac日常软件APP推荐
  17. 关键词提取(三种免费工具)
  18. OpenTsdb官方文档中文版----聚合器
  19. 谈谈本地缓存的几种方式以及他们的区别?
  20. 含有咪唑盐和透明质酸/L赖氨酸改性透明质酸衍生物/基于透明质酸聚谷氨酸水凝胶的研究

热门文章

  1. 正则表达式:密码中至少包含大写字母、小写字母、数字、特殊字符等字符中的2种或3种
  2. Description Resource Path Location Type Path must include project and resource name: /
  3. Appium 与 Chromedriver
  4. 【springboot】启动时指定lib目录
  5. java制表符_Java地位无可动摇的12个原因
  6. qt designer 插入图片_真的不错,宁波棉服图片
  7. linux6.5声卡驱动安装,详解CentOS 6.5如何安装Realtek无线网卡驱动
  8. python3安装常见问题_python3中pip的安装、常见问题及万能解决方案。
  9. delphi socket 怎样把接收到的二进制数据转换成字符串显示_广东qp用户数据免费咨询...
  10. 拥有此神技,脚本调试从此与 echo、set、test 说分手