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行,每行有一个数字 ...
随机推荐
- ImportError: No module named images
[问题] 在使用学习wxPython时,一个Dem抱有如题所示错误 [解决] images 只不过是wxpython自带demo中的一个文件 体验wxpython IN action的时候Import ...
- SQL按时间段统计(5分钟统计一次访问量为例,oracle统计)
需求:统计当天的访问量,每五分钟采集一次 表结构中有日期字段,类型TIMESTAMP 如果,统计是采用每秒/分钟/小时/天/周/月/年,都非常容易实现,只要to_char日期字段然后group by分 ...
- AngularJS 外部文件中的控制器其他实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- rbg的代码
不得不赞rbg的代码,写的是真的好,各种异常都考虑到了,至少常见的异常没有了. 还有selective search的代码,也是很赞. 而edgebox的代码则不行啊,demo写的太死,而且代码里只能 ...
- 5.vue解决动态img :src绑定
前言: 因为静态资源在vue中是需要经过编译的, 所以动态拼接的图片地址,在:src的时候不经过编译. 就会发生图片404,找不到资源. 那么本地图片资源如何动态的绑定呢? 实践: 其实,真相往往就是 ...
- java基础 静态 static 问在多态中,子类静态方法覆盖父类静态方法时,父类引用调用的是哪个方法?
多态 package com.swift.jiekou; public class Jicheng_Tuotai_jingtai_diaoyong { public static void main( ...
- WebStrome react-native代码智能提示
1.clone到本地 git clone https://github.com/virtoolswebplayer/ReactNative-LiveTemplate 2,添加ReactNative. ...
- codeforces757E. Bash Plays with Functions(狄利克雷卷积 积性函数)
http://codeforces.com/contest/757/problem/E 题意 Sol 非常骚的一道题 首先把给的式子化一下,设$u = d$,那么$v = n / d$ $$f_r(n ...
- Windows下MySQL数据库的安装与关闭开机自启动
我在学习java,安装数据库时找了很多教程,现在在这里总结一下我安装数据库的过程,我安装的是mysql-5.6.42-winx64版本的. 数据官方下载地址:https://dev.mysql.com ...
- Shell学习——Shell分类:登录shell和非登陆shell 交互shell和非交互shell
1.从两个不同维度来划分,是否交互式,是否登录 2.交互式shell和非交互式shell 交互式模式:在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shel ...