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
has a beautiful necklace. The necklace is made up of N magic balls.
Each ball has a beautiful value. The balls with the same beautiful value
look the same, so if two or more balls have the same beautiful value,
we just count it once. We define the beautiful value of some interval
[x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value
from the xth ball to the yth ball and the same value is ONLY COUNTED
ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1,
F(2,4)=3, F(2,6)=6.
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.
6
1 2 3 4 3 5
3
1 2
3 5
2 6
6
1 1 1 2 3 5
3
1 1
2 4
3 5
7
14
1
3
6
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- #define N 1000005
- #define M 50005
- #define numq 200005
- #define ll long long
- int Last[N];
- ll shusz[M];
- int gaga[M];
- ll ans[numq];
- struct Q {
- int l ;
- int r;
- int id;
- bool operator < (const Q a) const
- {
- return r<a.r;
- }
- }qq[numq];
- int lb(int i)
- {
- return i&(-i);
- }
- void add(int j , int t)
- {
- for(int i =j ;i < M ;i+=lb(i))
- {
- shusz[i]+=t;
- }
- }
- ll sum (int x)
- {
- ll ans = ;
- for(int i = x ; i > ; i-=lb(i))
- {
- ans+=shusz[i];
- }
- return ans;
- }
- ll sum(int x , int y)
- {
- ll ans = sum(y)-sum(x-);//注意是x-1
- return ans;
- }
- int main()
- {
- int T ;
- scanf("%d",&T);
- for(int i = ;i < T ; i++)
- {
- int n;
- scanf("%d",&n);
- int tm;
- for(int j = ; j <= n ; j++)
- {
- scanf("%d",&tm);
- gaga[j] = tm;
- }
- int m ;
- scanf("%d",&m);
- for(int j = ;j <= m ;j++)
- {
- int l , r ;
- scanf("%d%d",&l,&r);
- qq[j].l = l ;
- qq[j].r = r;
- qq[j].id = j;
- }
- memset(Last,-,sizeof(Last));
- memset(ans,,sizeof(ans));
- memset(shusz,,sizeof(shusz));
- sort(qq+,qq+m+);
- int cur = ;//记录扫描到第几个询问
- for(int j = ; j <= n ; j++)//扫描n个点
- {
- if(Last[gaga[j]] != -)
- add(Last[gaga[j]], -gaga[j]);
- Last[gaga[j]] = j;
- add(j, gaga[j]);
- while(j == qq[cur].r)
- {
- ans[qq[cur].id] = sum(qq[cur].l, qq[cur].r);
- cur++;
- }
- }
- for(int j = ; j <= m; j++)
- printf("%lld\n", ans[j]);
- }
- return ;
- }
Necklace(树状数组+离线操作)的更多相关文章
- HDU3874Necklace(树状数组+离线操作)
此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...
- HDU---4417Super Mario 树状数组 离线操作
题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...
- bzoj 1878 SDOI2009树状数组 离线操作
本来想写2120的,结果想起来了这个 我们先对于询问左端点排序,用树状数组存区间字母个数,对于每种字母, 第一次出现的位置记录为1,剩下的记录为0,然后记录下,每种颜色 后面第一个和他相同颜色的位置 ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SPOJ3267--D-query (树状数组离线操作)
题意查询区间 [l,r]内有多少个不同的数字 先把所有询问按 右端点进行排序,然后离线操作.如果该位置的数字 已经出现过那么把前一个位置-1,当前位置+1.扫一遍输出. #include <cs ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- 【树状数组】Bzoj1878[SDOI2009] HH的项链
Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此, 他的项链变 ...
随机推荐
- ReactNative布局样式总结
flex number 用于设置或检索弹性盒模型对象的子元素如何分配空间 flexDirection enum('row', 'row-reverse' ,'column','column-rever ...
- [置顶]
MVC输出缓存(OutputCache参数详解)
1.学习之前你应该知道这些 几乎每个项目都会用到缓存,这是必然的.以前在学校时做的网站基本上的一个标准就是1.搞定增删改查2.页面做的不要太差3.能运行(ps真的有这种情况,答辩验收的时候几个人在讲台 ...
- Sum of odd and even elements
Given an integer N, you have to print the sum of odd numbers and even numbers form 1 to N Input:Firs ...
- Qt编写导航按钮
做各种各样的界面的时候,经常需要做一排按钮用于切换到对应界面,俗称导航按钮或者导航菜单,参照过各种各样的主界面导航布局,特意编写导航按钮自定义控件,结合各种情况,继承自QPushButton.已集成在 ...
- centos7 系统安装问题汇总
centos7 系统安装问题汇总: 1.使用u盘 安装centos7时,一直提示:'.../dev/root does not exist,could not boot' 解决方法: 2.不能将原来 ...
- oracle12c_安装3——部署
数据库安装后需要根据实际情况修改相关参数. 1.生成pfile以防万一. SQL> create pfile from spfile; 2.修改内存参数 只要设置MEMORY_MAX_TARGE ...
- 【Java框架型项目从入门到装逼】第八节 - 用EasyUI绘制主界面
1.引入资源包 在上一节中,我们把基本的框架都搭好了,用了Spring,SPringMVC.这一节,我们先来画页面,前端框架采用EasyUI来实现. easyui是一种基于jQuery的用户界面插件集 ...
- Box布局
import sys from PyQt4 import QtCore, QtGui class MainWindow(QtGui.QWidget): def __init__(self, paren ...
- cassandra的gc调优
我们用的是cassandra3.7官方的docker镜像,在生产环境发现有一个小时一次停顿的现象.我猜测是java gc的原因,于是看了cassandra的gc日志,果然发现有每小时长达300ms-2 ...
- 【STL深入理解】vector
这篇文章不打算讲述vector的基本用法,而是总结一下近期我大量阅读C++经典书籍时遇到的一些关于vector的容易忽略的知识点,特意将它们记录下来,以便以后查阅. 1.v[0]和v.at(0)的区别 ...