Necklace(树状数组+离线操作)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3874
Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3929 Accepted Submission(s): 1296
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define N 1000005 6 #define M 50005 7 #define numq 200005 8 #define ll long long 9 int Last[N]; 10 ll shusz[M]; 11 int gaga[M]; 12 ll ans[numq]; 13 struct Q { 14 int l ; 15 int r; 16 int id; 17 bool operator < (const Q a) const 18 { 19 return r<a.r; 20 } 21 }qq[numq]; 22 int lb(int i) 23 { 24 return i&(-i); 25 } 26 void add(int j , int t) 27 { 28 for(int i =j ;i < M ;i+=lb(i)) 29 { 30 shusz[i]+=t; 31 } 32 } 33 ll sum (int x) 34 { 35 ll ans = 0 ; 36 for(int i = x ; i > 0 ; i-=lb(i)) 37 { 38 ans+=shusz[i]; 39 } 40 return ans; 41 } 42 ll sum(int x , int y) 43 { 44 ll ans = sum(y)-sum(x-1);//注意是x-1 45 return ans; 46 } 47 int main() 48 { 49 int T ; 50 scanf("%d",&T); 51 for(int i =0 ;i < T ; i++) 52 { 53 int n; 54 scanf("%d",&n); 55 int tm; 56 for(int j = 1 ; j <= n ; j++) 57 { 58 scanf("%d",&tm); 59 gaga[j] = tm; 60 } 61 int m ; 62 scanf("%d",&m); 63 for(int j = 1 ;j <= m ;j++) 64 { 65 int l , r ; 66 scanf("%d%d",&l,&r); 67 qq[j].l = l ; 68 qq[j].r = r; 69 qq[j].id = j; 70 } 71 memset(Last,-1,sizeof(Last)); 72 memset(ans,0,sizeof(ans)); 73 memset(shusz,0,sizeof(shusz)); 74 sort(qq+1,qq+m+1); 75 int cur = 1;//记录扫描到第几个询问 76 for(int j = 1 ; j <= n ; j++)//扫描n个点 77 { 78 if(Last[gaga[j]] != -1) 79 add(Last[gaga[j]], -gaga[j]); 80 Last[gaga[j]] = j; 81 add(j, gaga[j]); 82 while(j == qq[cur].r) 83 { 84 ans[qq[cur].id] = sum(qq[cur].l, qq[cur].r); 85 cur++; 86 } 87 } 88 for(int j = 1; j <= m; j++) 89 printf("%lld\n", ans[j]); 90 } 91 return 0 ; 92 }
转载于:https://www.cnblogs.com/shanyr/p/4725462.html
Necklace(树状数组+离线操作)相关推荐
- HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)
Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,"正难则反",我们试着求有多少 ...
- 8.8线段树和树状数组
题目链接 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28619#overview 密码 acmore 还是感觉不怎么会线段树,还是 ...
- 【树状数组】Bzoj1878[SDOI2009] HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
- hdu4417 Super Mario(树状数组+离线区间操作)
题目链接 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jum ...
- HDU 3333HDU-3874 Necklace 离线树状数组
这题是要求一段区间内的不重复的数字之和.我们通过对询问区间的右端点进行排序,然后记录每一数字的上一次的出现的位置,由于询问都是不回溯的那么就可以线性的更新了. #include &l ...
- P1972 [SDOI2009]HH的项链(离线树状数组)
整理的算法模板合集: ACM模板 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...
- D. Multiset(树状数组 + 二分)
Multiset 可能更好的阅读体验 思路 二分 + 树状数组做法 我们发现每个数的范围是$ <= 1e6$的,所以可以直接在线操作,不用离散化离线操作. 这个时候我们的treetreetree ...
最新文章
- 流氓网站5599.net修改ie主页分析
- Linux软件安装-----apache安装
- Spartan-6的时钟资源、全局时钟缓冲器/多路复用器
- mysql停止服务命令_0789不停止MySQL服务重做备库的方法
- onethink swfupload upload.php,OneThink--ThinkPHP3.2 缓存文件泄露
- (计算机组成原理)第五章中央处理器-第五节2:指令流水线影响因素和分类及多发技术
- 国土空间适宜性评价与承载力评价之间的逻辑关系是什么?
- office2010 反应慢_office2010打开时间太慢。怎么办?
- 柱状图如何添加数字标签_分类堆叠柱状图顺序排列及其添加合适条块标签
- 实用新型专利申请文件撰写示例
- 加速度传感器工作原理及应用
- 台式计算机组装攻略,台式机如何组装 电脑组装详细步骤【图文】
- PHP语言之MySQL操作
- 【课程·研】自然辩证法 | 课堂汇报:工程师的伦理规范
- 2022谈谈手机充电头/快充头/电源适配器
- 荣耀手机怎么使用鸿蒙系统,荣耀供应链6月全面恢复,安卓系统依然是第一选择,鸿蒙只是备选...
- 智慧树源码_公众号题库源码
- 如何扎实的学好ABAP?我的个人经验
- CPU 时间片轮转机制 (RR 调度)
- Spring Boot 实战(3)静态资源配置