HDU-4614 Vases and Flowers 线段树区间更新

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614

  线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了。。。代码写得比较挫><

 //STATUS:C++_AC_359MS_1728KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int c[N<<],sum[N<<];
int T,n,m,a,b,tot,w; void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
if(c[rt<<]==c[rt<<|] && c[rt<<]!=-)c[rt]=c[rt<<];
else c[rt]=-;
} void pushdown(int rt,int l,int r,int mid)
{
c[rt<<]=c[rt<<|]=c[rt];
sum[rt<<]=(mid-l+)*(!c[rt]);
sum[rt<<|]=(r-mid)*(!c[rt]);
} void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=;
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
sum[rt]=r-l+;
} void update(int l,int r,int rt,int val)
{
if(a<=l && r<=b){
c[rt]=val;
sum[rt]=(r-l+)*(!val);
return ;
}
int mid=(l+r)>>;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(a<=mid)update(lson,val);
if(b>mid)update(rson,val);
pushup(rt);
} int query_sum(int l,int r,int rt)
{
if(a<=l && r<=b){
return sum[rt];
}
int mid=(l+r)>>,tot=;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(a<=mid)tot+=query_sum(lson);
if(b>mid)tot+=query_sum(rson);
return tot;
} void queryw(int l,int r,int rt)
{
if(l==r){
w=l;
tot-=sum[rt];
return;
}
int mid=(l+r)>>;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(sum[rt<<]>=tot)queryw(lson);
else if(mid<a || (sum[rt<<]<tot && sum[rt<<|])){
tot-=sum[rt<<];
queryw(rson);
}
else queryw(lson);
pushup(rt);
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,op,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
n--;
mem(c,);
build(,n,);
while(m--){
scanf("%d%d%d",&op,&x,&y);
if(op==){
tot=y;
int t;
if(x>){
a=,b=x-;
t=query_sum(,n,);
tot+=t;
}
else t=;
a=x;
queryw(,n,);
if(tot==y)
printf("Can not put any one.\n");
else {
int en=w;
a=x;tot=t+;
queryw(,n,);
printf("%d %d\n",w,en);
a=w,b=en;
update(,n,,);
}
}
else {
a=x,b=y;
printf("%d\n",b-a+-query_sum(,n,));
update(,n,,);
}
}
putchar('\n');
}
return ;
}
上一篇:[TCP/IP]TCP的三次握手和四次挥手


下一篇:TCP半开连接与半闭连接