hdu 3874 Necklace(bit树+事先对查询区间右端点排序)
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.
Input
Output
Sample Input
Sample Output
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
typedef __int64 LL;
int N;
LL A[],elem[],ans[];
int lowbit(int x){ return x&-x; }
LL sum(int id) bit树
{
LL ret=;
while(id>){ ret+=elem[id]; id-=lowbit(id); }
return ret;
}
void add(int id,LL val)
{
while(id<=N){ elem[id]+=val; id+=lowbit(id); }
}
int sign[];
struct node
{
int le,ri,id;
bool operator < (const node& t) const { return ri<t.ri; } //对右端点排序
}qes[];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
memset(elem,,sizeof(elem));
for(int i=;i<=N;i++)
{
scanf("%I64d",&A[i]);
sign[A[i]]=;
}
int M;
cin>>M;
for(int i=;i<=M;i++) scanf("%d%d",&qes[i].le,&qes[i].ri),qes[i].id=i; //输入
sort(qes+,qes++M);
int pos=;
for(int i=;i<=M;i++)
{
while(pos<=qes[i].ri) //更新
{
if(sign[A[pos]]) add(sign[A[pos]],-A[pos]); //之前出现过
sign[A[pos]]=pos; //更新到当前位置
add(pos,A[pos]); //插入
pos++;
}
ans[qes[i].id]=sum(qes[i].ri)-sum(qes[i].le-); //得到答案
}
for(int i=;i<=M;i++) printf("%I64d\n",ans[i]);
}
return ;
}
hdu 3874 Necklace(bit树+事先对查询区间右端点排序)的更多相关文章
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU - 3874 Necklace (线段树 + 离线处理)
欢迎參加--每周六晚的BestCoder(有米! ) Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/3 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- HDU 3874 Necklace 区间查询的离线操作
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3874 对需要查询的区间按右端点排序,然后从左到右依次加入序列中的元素,同时更新,更新的方法是,把上一次出 ...
- 洛谷1972 HH的项链 树状数组查询区间内不同的数的数量
题目链接:https://www.luogu.com.cn/problem/P1972 题意大致是:给定一个序列长度为n,给出m个查询区间,要求响应是区间内不同的数的个数.为此我们考虑到树状数组的区间 ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和 离线处理,按查询右端点从小到大排序,从左往右扫一遍. 记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去. #include <cst ...
- hdu 3874 Necklace(线段树)
这道题目和我之前做过的一道3xian大牛出的题目很像,不过总的来说还是要简单一点儿. 计算区间内的值的时候如果两个值相等,只能计算其中一个. 这道题需要将所有的问题输入之后再计算,首先,对所有问题的右 ...
- HDU 3874 离线段树
在所有数字的统计范围,,对于重复统计只有一次 离线段树算法 排序终点坐标.然后再扫,反复交锋.把之前插入树行被删除 #include "stdio.h" #include &quo ...
随机推荐
- Tomcat查看用户名密码
在非安装版的tomcat中,可以在{解压路径}/conf/tomcat_users.xml 配置文件中找到,也可以自己添加新的用户
- WebFrom模拟MVC
如: aspx前台 这样写生成页面时不会产生新的html标签,用控件则会产生新的html标签 <h1><%= title %></h1> <p> ...
- sprig——jar包
Struts. Hibernate.Spring这类的框架给我们开发带来非常大的好处,让我们更加快速.有效的开发.所以我们在开发中通常都会用到各种框架,每个框架 都有很多jar包,每个jar都有各自不 ...
- mysql 自己定义存储过程和触发器
mysql 自己定义存储过程和触发器 --存储过程示范 DROP PROCEDURE IF EXISTS PRO_TEST; CREATE PROCEDURE PRO_TEST(IN NUM_IN I ...
- kaggle之泰坦尼克的沉没
Titanic 沉没 参见:https://github.com/lijingpeng/kaggle 这是一个分类任务,特征包含离散特征和连续特征,数据如下:Kaggle地址.目标是根据数据特征预测一 ...
- 不同浏览器对URL最大长度的限制(转)
1.今天碰到一个bug,window.open后面的页面,接收参数不全,导致后台报错.实验了一下.发现是使用get方法请求服务器时,URL过长所致 微软官方的说明: http://support.mi ...
- WinForm的TreeView实现Win7 Areo效果
新建一个继承自TreeView的控件类,代码如下: using System; using System.Windows.Forms; using System.Drawing; using Syst ...
- .net 地址栏传中文乱码 的解决方法
1.设置web.config文件. <system.web> ...... <globalization requestEncoding="gb2312" res ...
- js-String
1.一个字符串可以使用单引号或双引号 2.查找 字符串使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置 如果没找到对应的字符函数返回-1 lastIndexOf() 方法在字符串 ...
- dispatch_group_t
最近在写的模块有这样一个问题,要保证所有block里面的东西全都回来之后再执行某一个 例如我要做完所有的数据库操作再刷新界面,数据库的内容很多,所有用到了group dispatch_group_t ...