↓ Skip to main content
  1. Posts/

poj 2828 Buy Tickets (线段树单点更新,逆序插入)

·836 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

poj 2828 题目链接

题意:n个人,每个人有一个rp值(用来区分不同的人),还有一个pos[i],表示当第i个人来排队的时候插入到第pos[i]个人的后面(也就是排在位置pos[i]+1)

现在问最后的序列,从1到n输出n个人的rp值

思路:第二道线段树,并不会做,看了题解。

比较关键的一点是:按照顺序的话,当后来的一个人插入到前面,那么之前很多人排好的位置将发生改变,这个代价是巨大的。

由于越后来的人越容易确定位置,因此正确的做法是倒序处理。

对于第i个人,我们把他放在从前往后数的第i个空的位置即可。

接下来需要做的就是线段树处理,线段树存储的信息是某一个区间中空位置的个数。

感觉是一道很好的题目,对于新手来说并不是很容易,不过理解了这道题目以后,感觉进一步理解线段树,有点开心。

代码实现
 1/*************************************************************************
 2	> File Name: code/poj/2828.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com
 5	> Created Time: 2015年10月29日 星期四 09时00分08秒
 6 ************************************************************************/
 7#include<iostream>
 8#include<iomanip>
 9#include<cstdio>
10#include<algorithm>
11#include<cmath>
12#include<cstring>
13#include<string>
14#include<map>
15#include<set>
16#include<queue>
17#include<vector>
18#include<stack>
19#include<cctype>
20#define yn hez111qqz
21#define j1 cute111qqz
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))
25using namespace std;
26const int dx4[4]={1,0,0,-1};
27const int dy4[4]={0,-1,1,0};
28typedef long long LL;
29typedef double DB;
30const int inf = 0x3f3f3f3f;
31const int N=2E5+7;
32int pos[N],val[N];
33int n;
34int tree[N<<2]; //tree[i]存储的是以i为根节点的子树对应的区间中空位置的数量。
35int ans[N];
36void PushUp(int rt)
37{
38    tree[rt] = tree[rt<<1]+tree[rt<<1|1]; //一段区间空位置的数量等于两端子区间中空位置的数量的和。
39}
40void build(int l,int r,int rt)
41{
42    if (l==r)
43    {
44	tree[rt] = 1; //初始的空位置的数量为区间长度r-l+1
45	return ;
46    }
47    int m = (l+r)>>1;
48    build(lson);
49    build(rson);
50    PushUp(rt);
51}
52void update (int p,int val,int l,int r,int rt)
53{
54    if (l==r) //终于找到了该点的归属。。。
55    {
56	ans[l] = val;
57	tree[rt]--;
58	return ;
59    }
60    int m = (l+r)>>1;
61    if (p<=tree[rt<<1]) update(p,val,lson); //如果左子树所代表的区间中空位置的数目够的话就放左边
62    else update(p-tree[rt<<1],val,rson);//否则放右边,一共需要p个空位置,左边提供了tree[rt<<1]个,还需要右边提供p-tree[rt<<1]个。
63    PushUp(rt);
64}
65int main()
66{
67  #ifndef  ONLINE_JUDGE
68   freopen("code/in.txt","r",stdin);
69  #endif
70   while (~scanf("%d",&n))
71    {
72	for ( int i = 0 ; i < n ;i++) scanf("%d %d",&pos[i],&val[i]);
73	build(1,n,1);
74	for ( int i = n-1 ; i >= 0 ; i--)
75	{
76	    pos[i]++;
77	    update(pos[i],val[i],1,n,1);
78	}
79	for ( int i =  1 ; i <= n-1 ; i++) printf("%d ",ans[i]);
80	printf("%d\n",ans[n]);
81    }
82 #ifndef ONLINE_JUDGE
83  fclose(stdin);
84  #endif
85	return 0;
86}

Related

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

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

hdoj 2795 Billboard

·486 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:一个尺寸为wh的方格。要按顺序放放n个尺寸为1wi的纸条。问每一个纸条回被放在哪里。如果有多个,放在最上面(编号小) 思路:把没横行能放的最大长度看做一个序列建树。由于h比n大很多。。多出来的没用。。直接取较小值就行。

hdu 1754 I Hate It (线段树)

·746 words·2 mins
I Hate It # **Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 53991 Accepted Submission(s): 21180 ** Problem Description 很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。 这让很多学生很反感。