↓ 跳过正文
  1. Posts/

codeforces 19 D. Points (离散化+树套树(线段树+set))

·1460 字·3 分钟

题目链接

题意:

在二维坐标平面内进行 n (1 ≤ n ≤ 2·10^5) 次操作。一共有三种类型操作。

1.add x,y 将点 (x,y) 加进坐标系。

2.remove x,y 将点 (x,y) 移除。

3.find x,y 找到点 (x,y) 右上角的点 (xp>x,yp>y)。如果有多个输出 x 最小的。如果还有多个输出 y 最小的。

x,y 均为非负数。以上操作均合法。

思路:没有思路,不会啊,以为要二维线段树什么的,总之是不会做。

大概从中午开始看题解,看了 8 个小时,终于完全搞懂了 orz。

很巧妙地把二维问题转化成了一维问题。

我来说一下大概做法,具体的细节见代码注释:

在 x 轴方向维护一棵线段树,线段树的数组 tree[i] 存储的信息是以 i 节点为根节点的子树所对应的区间能达到的最大的 y 值。线段树的叶子节点上是一个 set,set[i] 是横坐标为 i 时的纵坐标集合,也就是所谓的树套树。

由于 x 很大,但是 n 比较小,所以这里采用 STL 去重的办法离散化。原来链接的《离散化的三种办法》一文在历史迁移中被误删,且 Git 中仅存的版本已经截断,暂不重新发布损坏正文。

对于添加和删除点的操作,我们更新完对应的 set,把相应的 x(离散化后的)在线段树中更新就好(因为线段树的 update 操作是和 set 有关的)。

对于 find 操作,我们首先从线段树中找到(下标大于 x 且最小且集合中存在大于 y 的元素的集合)的下标。

这样我们确定了 x,再 upper_bound 一下找到对应的集合中最小的 y。

初始化由于没有插入 y,所以 tree 可以初始化为 -1,不用建树(反正建了也都是 -1)。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :Sun 04 Sep 2016 07:19:05 PM CST
  4File Name :code/cf/problem/19D.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N=2E5+7;
 32int n,m;
 33int tree[N<<2]; //记录线段树的信息,tree[i]表示的是以i节点为根节点的子树所代表的区间中的点的最大的y值。
 34set<int>se[N];//集合se[i]是横坐标为x的点的纵坐标的集合。
 35struct node   //线段树套平衡树,在x轴的方向上建一棵线段树,线段树的每个叶子节点是一个set。
 36{
 37    int x,y;
 38    char cmd[15];
 39    void input()
 40    {
 41	scanf("%s%d%d",cmd,&x,&y);
 42    }
 43}q[N];
 44int H[N];
 45void PushUp(int rt)
 46{
 47    tree[rt] = max(tree[rt<<1],tree[rt<<1|1]);
 48}
 49void update( int p,int l,int r,int rt)
 50{
 51    if (l==r)
 52    {
 53	if (se[l].size())
 54	    tree[rt] =*(--se[l].end()); //se.end()是指向最后一个元素之后的, *(--se[l].end())只是取了se[l]集合的最后一个元素而已
 55	else tree[rt] = -1;
 56	return ;
 57    }
 58    int m = (l+r)>>1;
 59    if (p<=m)
 60	update(p,lson);
 61    else update(p,rson);
 62    PushUp(rt);
 63}
 64int query(int L,int R,int val,int l,int r,int rt)//返回集合中存在大于val(y)的集合中标号最小的集合的标号(x)
 65{
 66    if (tree[rt]<=val||L>R) return -1;//由于题目要求严格大于,因此查询区间是从h+1到m,当h恰好等于m的时候,L>R,记得判断。
 67    if (l==r) return l;
 68    int m = (l+r)>>1;
 69    int ret = -1;
 70    if (L<=m)
 71	ret = query(L,R,val,lson);
 72    if (ret!=-1)
 73	return ret;
 74    if (R>=m+1)
 75	return query(L,R,val,rson);
 76}
 77int Hash( int x)
 78{
 79    return lower_bound(H,H+m,x)-H;
 80}
 81int main()
 82{
 83	#ifndef  ONLINE_JUDGE
 84	freopen("code/in.txt","r",stdin);
 85  #endif
 86	cin>>n;
 87	m = n;
 88	for ( int i = 0 ; i < n ; i++) q[i].input(),H[i] = q[i].x;
 89	sort(H,H+m); // 离散化
 90	m = unique(H,H+m)-H;//离散化
 91	ms(tree,-1);//初始化最大值。
 92	for ( int i = 0 ; i <  n;  i++)
 93	{
 94	    int h = Hash(q[i].x)+1;//离散化
 95	    if (q[i].cmd[0]=='a')
 96		se[h].insert(q[i].y),update(h,1,m,1);
 97	    else if (q[i].cmd[0]=='r')
 98		se[h].erase(q[i].y),update(h,1,m,1);
 99	    else
100	    {
101		int p = query(h+1,m,q[i].y,1,m,1);//x已经是升序了,从h+1开始的区间寻找可以保证x严格大于,此时只需要在[h+1,m]区间找一个严格大于y的点。
102	        if (p==-1)
103		    puts("-1");
104		else printf("%d %d\n",H[p-1],*se[p].upper_bound(q[i].y)); //由于当x相同时要找y最小的那个,因此最后还要upper_bound找到第一个比q[i].y大的y。
105		//下标问题。。只是因为H的下标是从0开始的。。set的下标是从1开始的。。。H下标从0开始离散化会看着舒服一点。。。
106	    }
107	}
108  #ifndef ONLINE_JUDGE
109  fclose(stdin);
110  #endif
111    return 0;
112}

相关文章

hdu 1754 I Hate It (线段树模板题,炒鸡详细注释版)

·1717 字·4 分钟
hdu 1754 题目链接 题意:单点更新,区间查询最大值。 思路:线段树。 一开始借鉴了 clj 的 pointer 写法,wjmzbmr’s code 直接 MLE,看来也许只能在 cf 上用。 下面是 MLE 的代码: 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月18日 星期四 18时40分24秒 4File Name :code/hdu/1754.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27using namespace std; 28const double eps = 1E-8; 29const int dx4[4]={1,0,0,-1}; 30const int dy4[4]={0,-1,1,0}; 31const int inf = 0x3f3f3f3f; 32const int N=2E5+7; 33int a[N],n,m; 34int _max( int x,int y) 35{ 36 if (x==-1||y==-1) 37 return x==-1?y:x; 38 return a[x]>a[y]?x:y; 39} 40struct Tree 41{ 42 Tree *pl,*pr; 43 int l,r,mx; 44 void update() 45 { 46 mx = _max(pl->mx,pr->mx); 47 } 48 Tree(int l,int r) : 49 l(l),r(r) 50 { 51 if ( l + 1 == r) 52 { 53 mx = l ; 54 return ; 55 } 56 pl = new Tree(l,(l+r)>>1); 57 pr = new Tree((l+r)>>1,r); 58 update(); 59 } 60 void change(int p,int x) 61 { 62 if (p < l|| p>=r) return; 63 if (l+1==r) 64 { 65 a[l] = x; 66 return ; 67 } 68 pl->change(p,x); 69 pr->change(p,x); 70 update(); 71 } 72 int queryMax(int L,int R) 73 { 74 if (L <= l && r <= R) return mx; 75 if (L>=r || l >=R) 76 return -1; 77 return _max(pl->queryMax(L,R),pr->queryMax(L,R)); 78 } 79}*rt; 80int main() 81{ 82 #ifndef ONLINE_JUDGE 83 freopen("code/in.txt","r",stdin); 84 #endif 85 while (~scanf("%d%d",&n,&m)) 86 { 87 ms(a,0); 88 for ( int i = 0 ; i < n ; i++) scanf("%d",a+i); 89 rt = new Tree(0,n); 90 while (m--) 91 { 92 char opt[3]; 93 int x,y; 94 scanf("%s %d %d",opt,&x,&y); 95 x--; 96 if (opt[0]=='Q') 97 printf("%d\n",a[rt->queryMax(x,y)]); 98 else rt->change(x,y); 99 } 100 } 101 #ifndef ONLINE_JUDGE 102 fclose(stdin); 103 #endif 104 return 0; 105} 关于线段树的理解,见代码注释:

线段树学习笔记

·141 字·1 分钟
嘛,终于下定决心搞定线段树了。 之前几次都是被lazy标记卡住,这次大概不会了吧2333 放一些学习资料,最后比较zkw线段树和普通线段树的区别。 codeforces上非递归线段树讲解 (其实就是zkw吧) 线段树进阶(各种花式技巧) 找到了一篇非常赞的tutorial(含lazy标记) 链接

hdu 5842 || 2016 ccpc 网络赛 1011 Lweb and String(set)

·385 字·1 分钟
hdu 5842题目链接 题意:给一个只由小写字母组成的字符串,每个字符映射到一个数字,问映射之后的最长上升子序列的长度。。 思路:上来写nlogn的LIS是我无脑了。。。wa了之后想了下。。其实只要统计不同的字母数就好了啊。。。set一下

hdu 2609 How many (字符串的最小表示法+set)

hdu 2609 题目链接 题意:给出n个循环字符串,问有多少种。 思路:将每个字符串换成最小表示,然后set存一下即可。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月13日 星期六 02时44分21秒 4File Name :code/hdu/2609.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#include <ctime> 20#define fst first 21#define sec second 22#define lson l,m,rt<<1 23#define rson m+1,r,rt<<1|1 24#define ms(a,x) memset(a,x,sizeof(a)) 25typedef long long LL; 26#define pi pair < int ,int > 27#define MP make_pair 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=1E4+7; 34int n; 35char s[N][105]; 36set<string>se; 37int minRep(char *s) 38{ 39 int n = strlen(s); 40 int i = 0 ; 41 int j = 1 ; 42 int k = 0 ; 43 while (i<n&&j<n&&k<n) 44 { 45 int t = s[(i+k)%n] - s[(j+k)%n]; 46 if (t==0) k++; 47 else 48 { 49 if (t>0) 50 i+=k+1; 51 else j+=k+1; 52 if (i==j) j++; 53 k = 0 ; 54 } 55 } 56 return i<j?i:j; 57} 58int main() 59{ 60 #ifndef ONLINE_JUDGE 61 freopen("code/in.txt","r",stdin); 62 #endif 63 while (~scanf("%d",&n)) 64 { 65 ms(s,0); 66 se.clear(); 67 // cout<<"n:"<<n<<endl; 68 char tmp[105]; 69 for ( int i = 0 ; i < n; i++) 70 { 71 scanf("%s",tmp); 72// cout<<"tmp:"<<tmp<<endl; 73 int k = minRep(tmp); 74// cout<<"k:"<<k<<endl; 75 int cnt = 1; 76 int len = strlen(tmp); 77 for ( int j = k ; cnt <= len ; j++,cnt++) 78 s[i][cnt-1] = tmp[j%len]; 79 se.insert(string(s[i])); 80 } 81// for ( int i = 0 ; i < n; i++) cout<<"s[i]:"<<s[i]<<endl; 82// 83 int ans = se.size(); 84 printf("%d\n",ans); 85 } 86 #ifndef ONLINE_JUDGE 87 fclose(stdin); 88 #endif 89 return 0; 90}