这题跟ZOJ 3606的解题思路很相似。

题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9);2.从集合中删去一个数x(保证这个数存在);3.查询集合中所有位置满足i%5==3的数a[i]的和,集合中的数按升序排列。给你一共N个操作,输出每次查询的和。

做法:因为操作只有10^5个,所以将所有查询中的数保存下来,排序之后离散化。每个数对应一个“位置”,通过标记这个“位置”是否已用来表示该数是否在集合中。

线段树节点记录两个信息:

cnt:这一段“位置”中含有多少个数,pushUp的时候cnt[rt] = cnt[ rt << 1 ] + cnt[ rt << 1 | 1 ];

sum[5]:sum[i]表示这个区间中,从左到右编号,位置模5为i的所有数的和。

pushUp的时候,sum[rt][i] = sum[lc][i], sum[rt][ (i+cnt[lc])%5 ]+= sum[rc][i]

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define lc rt << 1
#define rc rt << 1 | 1 using namespace std; const int MAXN = ; struct Qry
{
int op;
int val;
} qq[MAXN]; struct node
{
LL sum[];
int cnt;
}; int Q;
int num[MAXN];
int cnt;
node Tr[ MAXN << ]; void build( int l, int r, int rt )
{
for ( int i = ; i < ; ++i ) Tr[rt].sum[i] = ;
Tr[rt].cnt = ; if ( l == r ) return;
int m = ( l + r ) >> ;
build( lson );
build( rson );
return;
} void PushUp( int rt )
{
Tr[rt].cnt = Tr[lc].cnt + Tr[rc].cnt;
for ( int i = ; i < ; ++i ) Tr[rt].sum[i] = Tr[lc].sum[i];
for ( int i = ; i < ; ++i ) Tr[rt].sum[ (i+Tr[lc].cnt)% ] += Tr[rc].sum[i];
return;
} void Update( int x, int op, int l, int r, int rt )
{
if ( l == x && x == r )
{
for ( int i = ; i < ; ++i ) Tr[rt].sum[i] = ;
if ( op == )
{
Tr[rt].cnt = ;
Tr[rt].sum[] = num[x];
}
else if ( op == ) Tr[rt].cnt = ;
return;
}
int m = ( l + r ) >> ;
if ( x <= m ) Update( x, op, lson );
else Update( x, op, rson );
PushUp( rt );
return;
} int main()
{
while ( scanf( "%d", &Q ) == )
{
cnt = ;
for ( int i = ; i < Q; ++i )
{
char tmp[];
scanf( "%s", tmp );
if ( tmp[] == 'a' )
{
qq[i].op = ;
scanf( "%d", &qq[i].val );
num[cnt++] = qq[i].val;
}
else if ( tmp[] == 'd' )
{
qq[i].op = ;
scanf( "%d", &qq[i].val );
num[cnt++] = qq[i].val;
}
else qq[i].op = ;
} sort( num + , num + cnt );
cnt = unique( num + , num + cnt ) - num;
build( , cnt - , ); for ( int i = ; i < Q; ++i )
{
if ( qq[i].op == )
printf( "%I64d\n", Tr[].sum[] );
else
{
int x = lower_bound( num + , num + cnt, qq[i].val ) - num;
Update( x, qq[i].op, , cnt - , );
}
}
}
return ;
}

HDU 4288 Coder ( 离散化 + 离线 + 线段树 )的更多相关文章

  1. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  2. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  3. hdu 4417 Super Mario 离线线段树

    思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...

  4. HDU 3333 Turing Tree 离线 线段树/树状数组 区间求和单点修改

    题意: 给一个数列,一些询问,问你$[l,r]$之间不同的数字之和 题解: 11年多校的题,现在属于"人尽皆知傻逼题" 核心思想在于: 对于一个询问$[x,R]$ 无论$x$是什么 ...

  5. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  6. 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)

    求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  9. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

随机推荐

  1. python读取mat文件

    一.mat文件 mat数据格式是Matlab的数据存储的标准格式.在Matlab中主要使用load()函数导入一个mat文件,使用save()函数保存一个mat文件.对于文件 load('data.m ...

  2. mingw libgcc_s_sjlj-1.dll is missing

    习惯了在linux环境下工作,编译wingdows平台程序采用mingw工具.编译完,运行exe程序,弹出错误信息: libgcc_s_sjlj-1.dll is missing 百度了一下,原来是编 ...

  3. spring中使用i18n(国际化)

    简单了解i18n i18n(其来源是英文单词internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬件 ...

  4. BFS算法入门--POJ3984

    迷宫问题–POJ3984 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22008 Accepted: 12848 Descri ...

  5. intellij idea 添加动态 user library(java.lang.VerifyError)【转】

    使用IDEA的时候有时要用到eclipse的user library,由于两个IDE导入library的方式不同导致我们找不到导入user library的方法. 查IDEA的官方文档,找到方法如下: ...

  6. windows下的node.js和npm的安装步骤详解

    一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资 ...

  7. 千锋教育Vue组件--vue基础的方法

    课程地址: https://ke.qq.com/course/251029#term_id=100295989 <!DOCTYPE html> <html> <head& ...

  8. CF961E Tufurama 树状数组

    E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...

  9. POJ:3616-Milking Time

    Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12324 Accepted: 5221 Descrip ...

  10. Aizu:0005-GCD and LCM

    GCD and LCM Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which comp ...