HDU 3874 Necklace 树状数组
题意:求区间内不同的数的和
离线处理,按查询右端点从小到大排序,从左往右扫一遍。
记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int using namespace std; const int MAXN = ; struct node
{
int id;
int l, r;
}; int N, Q;
int pre[MAXN]; //某数之前出现的位置
LL sum[];
LL val[];
int maxL;
node D[];
LL ans[]; int lowbit( int x )
{
return x&(-x);
} LL query( int x )
{
LL res = ;
while ( x > )
{
res += sum[x];
x -= lowbit(x);
}
return res;
} void update( int x, int val )
{
while ( x <= N )
{
sum[x] += val;
x += lowbit(x);
}
return;
} bool cmp( node a, node b )
{
if ( a.r != b.r ) return a.r < b.r;
else if ( a.l != b.r ) return a.l < b.l;
return a.id < b.id;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d", &N );
for ( int i = ; i <= N; ++i )
scanf( "%I64d", &val[i] ); scanf( "%d", &Q );
for ( int i = ; i < Q; ++i )
{
D[i].id = i;
scanf( "%d%d", &D[i].l, &D[i].r );
} memset( pre, -, sizeof(pre) );
memset( sum, , sizeof(sum) ); sort( D, D + Q, cmp ); int cur = ;
for ( int i = ; i < Q; ++i )
{
while ( cur <= D[i].r )
{
if ( pre[ val[cur] ] != - )
update( pre[ val[cur] ], -val[cur] ); update( cur, val[cur] );
pre[ val[cur] ] = cur;
++cur;
}
ans[ D[i].id ] = query( D[i].r ) - query( D[i].l - );
}
for ( int i = ; i < Q; ++i )
printf( "%I64d\n", ans[i] ); }
return ;
}
HDU 3874 Necklace 树状数组的更多相关文章
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- hdu 3874(树状数组)题解
Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ba ...
- HDU 2838 (DP+树状数组维护带权排序)
Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...
- HDU 2689Sort it 树状数组 逆序对
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4046 Panda 树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...
- hdu 5497 Inversion 树状数组 逆序对,单点修改
Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...
- HDU 5493 Queue 树状数组
Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...
- POJ 2352 && HDU 1541 Stars (树状数组)
一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...
- hdu 1541 (基本树状数组) Stars
题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...
随机推荐
- System.IO 二
接着上篇的来 System.IO FileSystemWatcher 指向这个签名的方法 可以监听目录发生了什么事件 例如: static void Main(string[] args) ...
- django中的forms组件(权限信息校验,增删改查)
1.用处 1.用户请求数据验证 2.自动生成错误信息 3.打包用户提交的正确信息 4.如果其中有一个错误了,其他的正确,则保留上次输入的内容 5.自动创建input标签并可以设置样式 6.基于form ...
- 初尝微信小程序2-基本框架
基本框架: .wxml :页面骨架 .wxss :页面样式 .js :页面逻辑 描述一些行为 .json :页面配置 创建一个小程序之后,app.js,app.json,app.wxss是必须的 ...
- 15、SpringBoot------整合swagger2
开发工具:STS 前言: 对外提供一个Api,无论是对开发.测试.维护,都有很大的帮助. 下面我们来实现swagger2. 参考实例:https://blog.csdn.net/weixin_3947 ...
- JQuery 过滤选择器 与属性修改的方法演示比较
文本匹配 在表单输入项里面输入值,根据输入值,点击判断按钮,让对应的复选框选中 <html> <head> <meta http-equiv="Content- ...
- 获取Grid后台动态添加的子项
例:Grid的子项是包含边框的复选框CheckBox //遍历Grid中的子项 foreach (var c in this.grid_box.Children) { Border bd = c as ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- JAVAOOP继承
继承:修饰符 子类 extends 父类{ //类定义部分},不可以使用private和protected修饰类 减少代码量,实现无损替换 必须符合A is a B的关系 宝马 车 狗 ...
- 【c学习-6】
void myFunction4(){ //根据用户字段和密码字段判定是否允许登录 //定义原密码和用户字段 char user[10]={"liupeng"};//设置用户名字段 ...
- JavaScript通过HTML的class来获取HTML元素的方法总结
对于js来说,我想每一个刚接触它的人都应该会抱怨:为什么没有一个通过class来获取元素的方法.尽管现在高版本的浏览器已经支持getElementsByClassName()函数,但是对于低版本浏览器 ...