hdu 3627 Giant For
来源:互联网 发布:java基础教程视频教程 编辑:程序博客网 时间:2024/06/04 08:53
Giant For
线段树+离散化。离线处理,由于查询的时候我们需要找到所有满足条件的结果中行最小且列最小的。我们以行建树,然后将行对应的列中的值有set保存下来。首先将数据读入,然后将行从大到小进行排序,维护一个区间中的列最大的值。添加与删除操作很相似,一个是相当于将点的标记为1,另一个就是将标记置为0,所以只需要1个update函数就可以了。然后查询的时候很简单,我们只需要找到行最小的,并且满足最大的列比给定的列大的。搜列的时候直接采用set的lower_bound函数。
/* author : csuchenan PROG : hdu 3627 algorithm: segment tree + set 对一维进行建树,将第二维采用set存储,然后在对第二维进行查询的时候我们可以直接用lower_bound进行查询 查询复杂度为logn * logn,用set能够很快进行查询,并且写起来比较方便*/#include <cstdio>#include <algorithm>#include <set>using namespace std ;const int maxn = 200005 ;struct Tree{ int l , r ; int col ;}tree[maxn*3] ;struct CMD{ char op ; int x , y ;}cmd[maxn] ;int linex[maxn] ;set<int> st[maxn];int n ;typedef pair<int ,int> RES ;#define L(x) (x<<1)#define R(x) (x<<1|1)#define M(x , y) (x + y >>1)void build(int l , int r , int c){ tree[c].l = l ; tree[c].r = r ; tree[c].col = -1 ; if(l == r){ st[l].clear() ; return ; } build(l , M(l , r) , L(c)) ; build(M(l , r) + 1, r , R(c)) ;}void update(int p , int op , int y , int c){ if(tree[c].l == tree[c].r){ if(op==1){ tree[c].col = max(tree[c].col , y) ; st[p].insert(y) ; } else{ st[p].erase(y) ; if(st[p].empty()){ tree[c].col = -1 ; } else tree[c].col = *st[p].rbegin(); } return ; } int m = M(tree[c].l , tree[c].r) ; if(p <= m){ update(p , op , y , L(c)) ; } else{ update(p , op , y , R(c)) ; } tree[c].col = max(tree[L(c)].col , tree[R(c)].col) ;}RES query(int l , int r, int y , int c){ if(tree[c].col < y){ return make_pair(0 , -1) ; } if(tree[c].l == tree[c].r){ set<int> :: iterator it ; it = st[tree[c].l].lower_bound(y) ; return make_pair(tree[c].l , *it) ; } int m = M(tree[c].l , tree[c].r) ; if(r <= m){ return query(l , r , y , L(c)); } else if(l > m){ return query(l , r , y , R(c)) ; } else{ RES p = query(l , m , y , L(c)) ; if(p.second != -1){ return p ; } return query(m + 1 , r , y , R(c)) ; }}int main(){ char str[10] ; int x , y , cas = 1; freopen("test.in" ,"r" , stdin) ; while(scanf("%d" , &n) , n){ for(int i = 0 ; i < n ; i ++){ scanf("%s%d%d" , str , &x , &y) ; linex[i] = x ; cmd[i].op = str[0] ; cmd[i].x = x ; cmd[i].y = y ; } sort(linex , linex + n) ; int len = unique(linex , linex + n) - linex ; build(0 , len - 1 , 1) ; if(cas != 1) printf("\n") ; printf("Case %d:\n" , cas ++) ; for(int i = 0 ; i < n ; i ++){ int pos = lower_bound(linex , linex + len , cmd[i].x) - linex ; if(cmd[i].op == 'a'){ update(pos , 1 , cmd[i].y , 1) ; } else if(cmd[i].op == 'r'){ update(pos , 0 , cmd[i].y , 1) ; } else{ if(pos + 1 > len - 1){ printf("-1\n") ; continue ; } RES t = query(pos + 1 , len - 1 , cmd[i].y + 1 , 1) ; if(t.second == -1){ printf("-1\n") ; } else{ printf("%d %d\n" , linex[t.first] , t.second) ; } } } } return 0 ;}
- hdu 3627 Giant For
- hdu 3627 Giant For
- hdu 3627 Giant For //线段树
- hdoj 3627 Giant For
- (线段树 + 离散化 ) hdu 3627 Giant For
- hdu 3627 Giant For(map+set模拟)
- 【线段树】 HDOJ 3627 Giant For
- 离散对数,giant-step baby-step,拓展giant-step baby-step (poj 2417, hdu 2815)
- 天津大学2010年区域赛1007 Giant For
- ISCE、GIAnt
- 新增Tao插件,Red Giant 经典特效插件 Trapcode Suite 13 for Win/Mac
- HDU 2815 Mod Tree【扩展Baby Step Giant Step解决离散对数问题】
- hdu 2815Mod Tree (数论题)(baby step giant step 小步大步)
- hdu——2815(数论之Baby Step Giant Step解决离散对数问题)
- HDU 2815 Mod Tree 离散对数 扩展Baby Step Giant Step算法
- HDU Fighting for HDU
- Hdu 2815 Mod Tree + Poj 3243 Clever Y 扩展Baby Step Giant Step 解决离散对数问题
- 高次同余方程:poj 3243+poj 2417+hdu 2815 (Baby Step Giant Step 算法)
- <action>标签中的相关属性
- 《wxWidgets》wxSizer 使用 入门
- action标签
- FlexPaper 使用入门——在线PDF显示
- <bean:message>标签
- hdu 3627 Giant For
- char与wchar_t的区别
- 肖陶扩
- 2012hulu笔试题
- 2011年美国留学:美国地理信息系统(GIS)专业研究方向解析
- JavaScript For JCFXBL 服务器端脚本I/O扩展
- object 常用 数学运算
- 《C与指针》读书笔记
- <controller>与 RequestProcessor类处理请求