hdu--2795--又是线段树

应该就是算 线段树的 单点更新吧.

但一开始给了我一个错觉 是二维线段树  我也是醉了

tree[root].x// x = L || R表示root这个结点表示的是L - > R这个区间

tree[root].leftLen//表示 root这个结点所在的区间现在还存在的最长连续格子数

更让人郁闷的是 我用cin cout写 即使写上了cin.sync_with_stdio(false)还是TLE。。。

我也是无语了  以前在Hdu上都是有效的啊...

然后 我改成scanf  printf就过了...

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int size = 200010;
 6 struct data
 7 {
 8     int L;
 9     int R;
10     int leftLen;
11 }tree[size*4];
12
13 void build( int root , int L , int R , int w )
14 {
15     int Mid = ( R + L ) >> 1;
16     tree[root].L = L;
17     tree[root].R = R;
18     tree[root].leftLen = w;
19     if( L==R )
20     {
21         return ;
22     }
23     build( root<<1 , L , Mid , w );
24     build( root<<1|1 , Mid+1 , R , w );
25 }
26
27 void pushUp( int root )
28 {
29     tree[root].leftLen = max( tree[root<<1].leftLen , tree[root<<1|1].leftLen );
30 }
31
32 void update( int root , int w )
33 {
34     int ans;
35     if( tree[root].L == tree[root].R )
36     {
37         tree[root].leftLen -= w;
38          cout << tree[root].L << endl;
39          return ;
40     }
41     if( tree[root<<1].leftLen >= w )
42          update( root<<1 , w );
43     else if( tree[root<<1|1].leftLen >= w )
44          update( root<<1|1 , w );
45      pushUp( root );
46 }
47
48 int main()
49 {
50     cin.sync_with_stdio(false);
51     int h , w , n;
52     while( cin >> h >> w >> n )
53     {
54         h = min( h , n );
55         build( 1 , 1 , h , w );
56         while( n-- )
57         {
58             cin >> w;
59             if( tree[1].leftLen < w )
60                 cout << -1 << endl;
61             else
62                 update( 1 , w );
63         }
64     }
65     return 0;
66 }

我这边就还是贴 tle的代码了 还是比较喜欢cin cout的风格 =_=

today:

  你平静地像一抹夕阳

  他们都忘了你朝阳时的模样

时间: 2024-08-12 21:26:35

hdu--2795--又是线段树的相关文章

HDU 2795 Billboard(线段树啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Problem Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements

HDU 2795 Billboard (线段树 单点更新 区间求最大值)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一块h*w 的广告版,有n块1*w[i]的广告,就着放广告尽量在顶上,尽量先放左边的原则,问在第几行能把广告放下,如果放不下,就打印-1: 思路:我们可以根据每一行建树,每一个子叶表示每一行的容量,而节点存放子节点的最大值,然后从最顶到底,快速查找能存放下广告的一行. 总之还是简单的线段树问题,难点在于抽象模型. #include <iostream> #include <cs

HDU 2795 Billboard(线段树,单点更新)

D - Billboard Crawling in process... Crawling failed Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2795 Appoint description: bupt_admin_13 (2015-07-28) System Crawler (2015-08-18) Description

HDU 2795 Billboard (线段树+贪心)

手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/article/details/77488127 URL: http://acm.hdu.edu.cn/showproblem.php?pid=2795题目大意:有一个h*w的木板 (h, w<=1e9), 现在有n (n<=2e5)张1*xi的海报要贴在木板上,按1~n的顺序每次贴海报时会选择最上的一排的最左边贴 (海报不能互相覆盖), 求每张海报会被贴在哪一行

ACM学习历程—HDU 2795 Billboard(线段树)

Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in th

HDU 2795 &lt;Billboard&gt; &lt;线段树单点更新&gt;

Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest programming competitions, changes in th

[HDU] 2795 Billboard [线段树区间求最值]

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11861    Accepted Submission(s): 5223 Problem Description At the entrance to the university, there is a huge rectangular billboard of s

HDU 2795 Billboard (线段树单点更新 &amp;&amp; 求区间最值位置)

题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明了每一次贴都尽量选择靠上靠左的位置,那既然这样,我们以1~h建立线段树,给每一个叶子节点赋值初值 w 表示当前行最大能够容纳宽度为 w 的公告纸,那么对于某一输入 wi 只要在线段树的尽量靠左的区间找出能够容纳这张公告的位置(即叶子节点)然后减去 wi 即可,需要对query()函数进行一点改造,将

hdu 2795 Billboard 线段树+二分

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid