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

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

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

#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. C#定义常量的两种方法

    在C#中定义常量的方式有两种,一种叫做静态常量(Compile-time constant),另一种叫做动态常量(Runtime constant).前者用“const”来定义,后者用“readonl ...

  2. 前端HTML之表单

    1.列表标签 1.1无序列表<ul>,当中每一层都是<li>    <ul> <li>张三</li> <li>李四</li ...

  3. 其它内置函数(zip等)

      python内置函数 截止到python版本3.6.2,python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数     Built-in Functio ...

  4. 记一次EBS正式环境补丁安装的过程

    因菏泽能源上线需求,需要在8009上修复集团8000环境上已经修复的所有补丁程序,修复前做应用及数据库层备份,完成修复后解决并发管理器无法启动的问题.此为概述. 应用层备份 应用层的备份采用直接压缩备 ...

  5. Win10上Anaconda环境下python3.6安装和使用pyinstaller

    一.安装步骤 1. 电脑是win10,安装的Python3.6 2. 在Scripts文件夹下执行pip install pyinstaller, 安装成功后下载pyinstaller安装包,解压之后 ...

  6. linux下csv导出文件中文乱码问题

    近日在服务器端通过导出csv文件,将数据从linux服务器端保存到windows桌面端,以便用户可以通过excel打开使用数据. 但是在使用excel打开csv文件时,出现了中文乱码的情况,但是使用记 ...

  7. Redis ---------- key的操作

    key命名规则 除空格和\n,其他都可以 select   db -index选择数据库 例1  select   5 例2 type 查看数据key类型 type name 例3  keys pat ...

  8. <Docker学习>2.Centos7安装docker

    Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10. CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存储层驱动)无 ...

  9. Pandas 基本技巧

    1.数据查看和转置 import numpy as np import pandas as pd # 导入numpy.pandas模块 # 数据查看.转置 df = pd.DataFrame(np.r ...

  10. 使用postMan测试insert或者update接口

    URL : http://localhost:8099/orderVoice/updateAgentLogin?access_token=7f10e803-f886-47df-b3dc-9ed307d ...