HDU 4288 && Codeforces 85D (线段树+离散化+离线处理)
来源:互联网 发布:网络直播培训 编辑:程序博客网 时间:2024/06/11 03:49
首先,为什么要离散化和离线处理?因为输入的数字会很大,可以达到10^9,但输入的N最多为10^5,即做多添加100000个数(不删除任何数),所以把输入的值进行排序,离散成1~100000,同时这也可以知道要建多大的线段树。
离散化的时候,经常要用到去重操作和查找操作,一般查找是用二分查找的。通过这题学习到了两个新的STL。
1)unique:去重 2)lower_bound:返回第一个比需要查找的数大的或者相等的数组下标(相当于二分查找)。具体实现在代码中。
最后,这题的pushUp操作是精髓,自己就是想不出来,唉。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define SIZE 100005#define ls l,mid,rt<<1#define rs mid+1,r,rt<<1|1using namespace std;typedef __int64 Int;Int sum[SIZE<<2],ans[SIZE<<2][5];Int arr[SIZE],der[SIZE];char op[SIZE][5];int n;void pushUp(int rt){ for(int i=0; i<5; i++) ans[rt][i] = ans[rt<<1][i] + ans[rt<<1|1][(i-sum[rt<<1]%5+5)%5];}void build(int l,int r,int rt){ sum[rt] = 0; for(int i=0; i<5; i++)ans[rt][i] = 0; if(l == r)return; int mid = (l + r) >> 1; build(ls); build(rs);}void update(int l,int r,int rt,int pos,int add){ sum[rt] += add; if(l == r) { ans[rt][1] = add==1?der[pos]:0; return; } int mid = (l + r) >> 1; if(pos <= mid) update(ls,pos,add); else update(rs,pos,add); pushUp(rt);}int main(){ while(~scanf("%d",&n)) { int len = 0; for(int i=0; i<n; i++) { scanf("%s",op[i]); if(op[i][0] != 's') { scanf("%I64d",&arr[i]); der[len++] = arr[i]; } } if(n == 1) { if(op[0][0] == 's') puts("0"); continue; } sort(der,der+len); len = unique(der,der+len)-der; build(1,len,1); int pos; for(int i=0; i<n; i++) { pos = lower_bound(der,der+len,arr[i])-der; if(op[i][0] == 's') printf("%I64d\n",ans[1][3]); else if(op[i][0] == 'a') update(1,len,1,pos,1); else update(1,len,1,pos,-1); } } return 0;}
- HDU 4288 && Codeforces 85D (线段树+离散化+离线处理)
- HDU 4288 Coder 【线段树+离线处理+离散化】
- hdu 4288 线段树+离散化+离线处理
- hdu 4288 线段树+离线+离散化
- hdu3333 线段树+离散化+离线处理
- hdu3333(线段树离散化离线处理)
- Codeforces Round #345 (Div. 1) D. Zip-line LIS 离线 离散化 线段树
- codeforces 703D 树状数组 + 离线处理 + 离散化
- Codeforces 19D(线段树+离散化)
- HDU 3333 树状数组(线段树) + 离散化 + 离线处理
- hdu4288--Coder--线段树--离线处理+离散化+想法!
- hdu3333 (线段树+离散化+离线处理)
- HDU 4288 线段树 + 离线处理
- CodeForces 19D 离散化+线段树+set
- Codeforces 12D.Ball (非递归线段树+离散化)
- CodeForces 85D Sum of Medians(线段树 + 离散化)
- coderforces round 19D线段树+离散化处理
- Codeforces Beta Round #19 D. Points 线段树+离散化离散化
- CLR线程池的作用与原理介绍
- 【Linux操作系统分析】计算机是怎样工作的
- latex 如何使用bib产生多种文献格式
- dimension reduce(梯度下降)self-organizing maps(自组织映射)
- MC新手入门(五十)------服务器连接 一
- HDU 4288 && Codeforces 85D (线段树+离散化+离线处理)
- 关于NavigationBar的View的一些设置
- 努力成为优秀的工程师
- [x264] fps与timebase, num与den
- Spring中scope属性singleton和prototype使用的区别
- 项目中AppDelegate详解
- Windows多线程编程
- 使用4G的U盘和UltraISO工具安装Win8系统
- tomcat javaweb开发基础( 9 servlet(2))