hdu 4614 Vases and Flowers
2013-08-05 14:54
337 查看
周末UA做了这场多校的vjudge版本。
我去陪爸妈去山沟沟转了,回来听说有个这个线段树,想敲敲。
多年不做线段树,好恶心><
这个是我每次都用的线段树定义,根据题目不同,会更改一些标记。
根据这个题来说,cover = 0表示该区间的位置都为空,cover=1表示该区间都不为空,cover=-1表示部分为空(借鉴于zoj线段覆盖那道题)
sum表示该区间里为空的位置长度和。
lid表示该区间最左边为空的位置号,rid为最右边为空的位置号。若sum = 0,则lid rid都为-1。
操作1:
输入a ,b
比较麻烦,求左端点很简单,直接查询下就行了。求出左端点lpos。若为-1输出Can not balabala。。
求右端点的时候我是先求出0~lpos中间的空位置val,然后求从0~n-1中空位置大于等于val+b的位置。方便分。
因为纠结一点,不知道如何在区间[l,r]中查找空位置为d的位置(就我目前维护的标记来说),但是查找[0,r]的位置会很easy~懂吧?
操作2:
求个sum即可
出错地方:查左端点的时候,如果左边查不到,那么右边得查,我直接左边差不到就返回了。要知道如果[l,r]区间中有sum>0的位置,但是空位置不一定是在需要的区间中。
比如区间[0, 9]中有空位置[0,1]和[5,6],但是呢,我需要的是[3, 6]中的空位置,显然mid后,左边[0,5]有空位置,但是是找不到对应的在[3,6]的空位置的。改过就过了。
时间:C++,400+MS,估计是用指针了,慢了,但是真心不习惯用数组替代,唉唉唉。
我去陪爸妈去山沟沟转了,回来听说有个这个线段树,想敲敲。
多年不做线段树,好恶心><
struct Tnode{ // 一维线段树 int l,r; int cover; int lid, rid; int sum; int len() { return r - l;} int mid() { return MID(l,r);} bool in(int ll,int rr) { return l >= ll && r <= rr; } void lr(int ll,int rr){ l = ll; r = rr;} };
这个是我每次都用的线段树定义,根据题目不同,会更改一些标记。
根据这个题来说,cover = 0表示该区间的位置都为空,cover=1表示该区间都不为空,cover=-1表示部分为空(借鉴于zoj线段覆盖那道题)
sum表示该区间里为空的位置长度和。
lid表示该区间最左边为空的位置号,rid为最右边为空的位置号。若sum = 0,则lid rid都为-1。
操作1:
输入a ,b
比较麻烦,求左端点很简单,直接查询下就行了。求出左端点lpos。若为-1输出Can not balabala。。
求右端点的时候我是先求出0~lpos中间的空位置val,然后求从0~n-1中空位置大于等于val+b的位置。方便分。
因为纠结一点,不知道如何在区间[l,r]中查找空位置为d的位置(就我目前维护的标记来说),但是查找[0,r]的位置会很easy~懂吧?
操作2:
求个sum即可
出错地方:查左端点的时候,如果左边查不到,那么右边得查,我直接左边差不到就返回了。要知道如果[l,r]区间中有sum>0的位置,但是空位置不一定是在需要的区间中。
比如区间[0, 9]中有空位置[0,1]和[5,6],但是呢,我需要的是[3, 6]中的空位置,显然mid后,左边[0,5]有空位置,但是是找不到对应的在[3,6]的空位置的。改过就过了。
时间:C++,400+MS,估计是用指针了,慢了,但是真心不习惯用数组替代,唉唉唉。
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define FORD(i,s,t) for(int i=(s-1); i>=t; i--)
#define BUG puts("here!!!")
#define STOP system("pause")
#define file_r(x) freopen(x, "r", stdin)
#define file_w(x) freopen(x, "w", stdout)
using namespace std;
const int MAX = 50010;
struct Tnode{ // 一维线段树 int l,r; int cover; int lid, rid; int sum; int len() { return r - l;} int mid() { return MID(l,r);} bool in(int ll,int rr) { return l >= ll && r <= rr; } void lr(int ll,int rr){ l = ll; r = rr;} };
Tnode node[MAX<<2];
void Update_sum(int t)
{
node[t].sum = node[R(t)].sum + node[L(t)].sum;
if( node[L(t)].lid != -1 )
node[t].lid = node[L(t)].lid;
else
node[t].lid = node[R(t)].lid;
node[t].rid = max(node[L(t)].rid, node[R(t)].rid);
if( node[t].sum == 0 )
node[t].cover = 1;
else
if( node[t].sum == node[t].len() )
node[t].cover = 0;
else
node[t].cover = -1;
}
void Build(int t,int l,int r)
{
node[t].lr(l,r);
node[t].lid = l;
node[t].rid = r;
node[t].sum = node[t].len();
node[t].cover = 0;
if( node[t].len() == 1 )
return ;
int mid = MID(l,r);
Build(L(t),l,mid);
Build(R(t),mid,r);
}
void update_cover(int t) {
if( node[t].cover == -1 )
return ;
if( node[t].cover == 1 ) {
node[t].sum = 0;
node[t].lid = -1;
node[t].rid = -1;
} else {
node[t].sum = node[t].len();
node[t].lid = node[t].l;
node[t].rid = node[t].r;
}
}
void push_cover(int t) {
if( node[t].len() == 1 ) return ;
if( node[t].cover != -1 ) {
node[L(t)].cover = node[R(t)].cover = node[t].cover;
update_cover(L(t));
update_cover(R(t));
}
}
void Update(int t,int l,int r,int val)
{
push_cover(t);
if( node[t].in(l,r) )
{
node[t].cover = val;
update_cover(t);
return ;
}
if( node[t].len() == 1 ) return ;
int mid = node[t].mid();
if( l < mid ) Update(L(t),l,r,val);
if( r > mid ) Update(R(t),l,r,val);
Update_sum(t);
}
int Query(int t,int l,int r)
{
push_cover(t);
if( node[t].in(l,r) ) return node[t].sum;
if( node[t].len() == 1 ) return 0;
int mid = node[t].mid();
int ans = 0;
if( l < mid ) ans += Query(L(t),l,r);
if( r > mid ) ans += Query(R(t),l,r);
return ans;
}
int Querylid(int t,int l,int r)
{
push_cover(t);
if( node[t].in(l,r) && node[t].sum > 0 )
return node[t].lid;
if( node[t].len() == 1 ) return -1;
int mid = node[t].mid();
if( l < mid && node[L(t)].sum > 0 ) {
int pos = Querylid(L(t),l,r);
if( pos != -1 )
return pos;
}
if( r > mid && node[R(t)].sum > 0 )
return Querylid(R(t),l,r);
return -1;
}
int Queryrid(int t,int l, int r, int val)
{
push_cover(t);
if( node[t].sum <= val ) return node[t].rid;
if( node[t].len() == 1 ) return -1;
int mid = node[t].mid();
if( l < mid && node[L(t)].sum >= val )
return Queryrid(L(t), l, r, val);
if( r > mid && node[R(t)].sum > 0 )
return Queryrid(R(t), l, r, val - node[L(t)].sum);
return -1;
}
int main() {
int ncases, n, m;
int opera, a, b;
scanf("%d", &ncases);
while( ncases-- ) {
scanf("%d%d", &n, &m);
if( m > 0 )
Build(1, 0, n);
while( m-- ) {
scanf("%d%d%d", &opera, &a, &b);
if( opera == 1 ) {
int lpos = Querylid(1, a, n);
if( lpos == -1 ) {
puts("Can not put any one.");
continue;
}
int val = Query(1, 0, lpos);
int rpos = Queryrid(1, 0, n, b + val);
printf("%d %d\n", lpos, rpos-1);
Update(1, lpos, rpos, 1);
}
if( opera == 2 ) {
if( b > n )
b = n - 1;
int ans = Query(1, a, b+1);
Update(1, a, b+1, 0);
printf("%d\n", b - a + 1 - ans);
}
}
puts("");
}
return 0;
}
相关文章推荐
- HDU 4614 Vases and Flowers
- 【HDU】4614 Vases and Flowers 线段树
- HDU-4614:Vases and Flowers(线段树+二分)
- HDU 4614 Vases and Flowers (2013多校2 1004 线段树)
- hdu 4614 Vases and Flowers(线段树:成段更新)
- hdu 4614 Vases and Flowers(线段树+二分)
- 【HDU-4614】Vases and Flowers(线段树双查询)
- HDU - 4614 Vases and Flowers(线段树+回溯)
- HDU 4614 Vases and Flowers
- HDU 4614 Vases and Flowers (2013多校第二场线段树)
- hdu 4614 Vases and Flowers(线段树+二分)
- hdu 4614 Vases and Flowers
- hdu 4614 Vases and Flowers 线段树
- HDU 4614 Vases and Flowers (2013多校第二场线段树)
- hdu-4614-Vases and Flowers-线段树
- hdu 4614 Vases and Flowers(线段树+二分)
- HDU-4614 Vases and Flowers (线段树区间更新)
- HDU-4614-Vases and Flowers(线段树)
- hdu 4614 Vases and Flowers
- HDU 4614 Vases and Flowers(线段树)#by zh