题意:求区间内不同的数的和

离线处理,按查询右端点从小到大排序,从左往右扫一遍。

记录每个数出现的上一个位置,如果该数之前没有出现过,就加上,否则就在上一个位置减去。

#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 树状数组的更多相关文章

  1. HDU - 3874 Necklace (树状数组、离线处理)

    题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...

  2. hdu 3874(树状数组)题解

    Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ba ...

  3. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  4. HDU 2689Sort it 树状数组 逆序对

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  5. hdu 4046 Panda 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 When I wrote down this letter, you may have been ...

  6. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  7. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  8. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  9. hdu 1541 (基本树状数组) Stars

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1541 n个星星的坐标,问在某个点左边(横坐标和纵坐标不大于该点)的点的个数有多少个,输出n行,每行有一个数字 ...

随机推荐

  1. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

  2. MySQL事务隔离级别 解决并发问题

    MySQL事务隔离级别 1. 脏读: 骗钱的手段, 两个窗口或线程分别调用数据库转账表,转账后未提交,对方查看到账后,rollback,实际钱没转. 演示方法: mysql默认的事务隔离级别为repe ...

  3. mariadb多实例实现

    环境:centos7,yum 安装mariadb5.5 mkdir /mysqldb/{3306,3307.3308}/{etc,socket,pid,log,data} -pv chown -R m ...

  4. 爬虫学习(十一)——bs4基础学习

    ba4的介绍: bs4是第三方提供的库,可以将网页生成一个对象,这个网页对象有一些函数和属性,可以快捷的获取网页中的内容和标签 lxml的介绍 lxml是一个文件的解释器,python自带的解释器是: ...

  5. linux 基本命令笔记

    nohup [process]  & 后台挂起命令nohup 挂起& 后台运行 python3 manage.py runserver 0.0.0.0:8080 python -r 递 ...

  6. PHP关闭notice级别的错误提示

    1.在php.ini文件中改动error_reporting改为: error_reporting=E_ALL & ~E_NOTICE 2.如果你不能操作php.ini文件,你可以使用如下方法 ...

  7. 1014-34-首页15-计算原创微博的frame------计算cell的高度---计算 UILabel 的 CGSize 的方法

    一.总体思路: 在控制器中,每次拿到数据模型(请求了数据.加载新微博)的时候,就调用 - (NSArray *)stausFramesWithStatuses:(NSArray *)statuses, ...

  8. POJ:2377-Bad Cowtractors

    传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...

  9. 【转帖】置高并发jdbc连接池

    简单的MySQL连接池 <Resource type="javax.sql.DataSource" name="jdbc/TestDB" factory= ...

  10. fromkeys语法/set集合/深浅拷贝/列表/字典的删除

    fromkeys语法: dic = {"apple":"苹果", "banana":"香蕉"} 返回新字典. 和原来的没 ...