# kuangbin带我飞 专题七 线段树专题（一波线段树水题，难题还不会做）

1413人阅读 评论(0)

HDU 1166

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       50000+5
#define   MAXN      100000+5
#define   lson      2*i
#define   rson      2*i+1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int c[MAX];
int a[MAX];

int sum(int x){
int ans=0;
while(x>0){
ans+=c[x];
x-=lowbit(x);
}
return ans;
}

while(x<=MAX){
c[x]+=d;
x+=lowbit(x);
}
}
int main(){
int T;
scanf("%d",&T);
for(int t=1;t<=T;t++){
int n;
scanf("%d",&n);
mem0(c);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
string s;
printf("Case %d:\n",t);
while(cin>>s){
int a,b;
if(s[0]=='Q'){
scanf("%d%d",&a,&b);
printf("%d\n",sum(b)-sum(a-1));
}
else if(s[0]=='A'){
scanf("%d%d",&a,&b);
}
else if(s[0]=='S'){
scanf("%d%d",&a,&b);
}
else if(s[0]=='E') break;
}
}
return 0;
}


HDU 1754

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       200000+5
#define   MAXN      100000+5
#define   lson      2*i
#define   rson      2*i+1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int maxv[MAX*12];
int a[MAX*3];
int l,r;
int n,m;

void build(int i,int L,int R){
int mid=L+(R-L)/2;
if(L==R) maxv[i]=a[L];
else{
build(lson,L,mid);
build(rson,mid+1,R);
maxv[i]=max(maxv[lson],maxv[rson]);
}
}

void update(int i,int L,int R,int l,int r){
int mid=L+(R-L)/2;
if(L==R){
maxv[i]=r;
return;
}
if(l<=mid) update(lson,L,mid,l,r);
else update(rson,mid+1,R,l,r);
maxv[i]=max(maxv[lson],maxv[rson]);
}

int query(int i,int L,int R){
int mid=L+(R-L)/2;
int ans=0;
int a=0,b=0;
if(l<=L&&R<=r) return maxv[i];
if(l<=mid) a=query(lson,L,mid);
if(r>mid) b=query(rson,mid+1,R);
return ans=max(a,b);
}
int main(){
while(~scanf("%d%d",&n,&m)){
mem0(maxv); mem0(a);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
build(1,1,n);
while(m--){
char c;
getchar();
scanf("%c%d%d",&c,&l,&r);
if(c=='Q') printf("%d\n",query(1,1,n));
else{
update(1,1,n,l,r);
}
}
}
return 0;
}


poj  3468

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       1000000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

LL sum[MAX<<2];
void PushUp(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void PushDown(int rt,int m){
}
}

void build(int l,int r,int rt){
if(l==r){
scanf("%lld",&sum[rt]);
return ;
}
int m=l+(r-l)/2;
build(lson);
build(rson);
PushUp(rt);
}

void update(int L,int R,LL c,int l,int r,int rt){
if(L<=l&&r<=R){
sum[rt]+=c*(r-l+1);
}
else{
PushDown(rt,r-l+1);
int m=l+(r-l)/2;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
PushUp(rt);
}
}

LL query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return sum[rt];
PushDown(rt,r-l+1);
int m=l+(r-l)/2;
LL ans=0;
if(L<=m) ans+=query(L,R,lson);
if(R>m) ans+=query(L,R,rson);
return ans;
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--){
char ch;
int a,b;
LL c;
getchar();
scanf("%c",&ch);
if(ch=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,1,n,1));
}
else{
scanf("%d%d%lld",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
return 0;
}


poj 2528

X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 5, X[ 4 ] = 6， X[ 5 ] = 10

（然而poj这么写了会WA，不这么离散反而能AC，不造为啥，反正知道这个方法是对的就行了）

<span style="font-size:12px;">#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       20000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int l[MAX],r[MAX];
int x[MAX<<3];
int root[MAX<<4];
int vis[MAX<<2];
int cnt;

void PushDown(int rt){
root[rt<<1]=root[rt<<1|1]=root[rt];
root[rt]=-1;
}

void update(int l,int r,int rt,int c,int L,int R){
if(L<=l&&r<=R){
root[rt]=c;
return;
}
if(root[rt]!=-1) PushDown(rt);
int m=l+(r-l)/2;
if(L<=m) update(lson,c,L,R);
if(R>m) update(rson,c,L,R);
}

int Bsearch(int a,int b,int xx){
int l=a,r=b;
while(l<=r){
int mid=(l+r)/2;
if(x[mid]<xx) l=mid+1;
else r=mid-1;
}
return l;
}

void query(int l,int r,int rt){
if(l==r){
if(!vis[root[rt]]){
cnt++;
vis[root[rt]]=1;
}
return;
}
if(root[rt]!=-1) PushDown(rt);
int m=l+(r-l)/2;
query(lson);
query(rson);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int nn=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&l[i],&r[i]);
x[++nn]=l[i];
x[++nn]=r[i];
}
sort(x+1,x+nn+1);
int mm=1;
for(int i=2;i<=nn;i++){
if(x[i]!=x[i-1]) x[++mm]=x[i];
}
mem1(root);
mem0(vis);
for(int i=1;i<=n;i++){
int L=Bsearch(1,mm,l[i]);
int R=Bsearch(1,mm,r[i]);
update(1,mm,1,i,L,R);
}
cnt=0;
query(1,mm,1);
printf("%d\n",cnt);
}
return 0;
}
</span>

hdu 1698

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       100000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int sum[MAX<<2];
int col[MAX<<2];

void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void pushdown(int rt,int m){
if(col[rt]){
col[rt<<1]=col[rt<<1|1]=col[rt];
sum[rt<<1]=col[rt]*(m-(m>>1));
sum[rt<<1|1]=col[rt]*(m>>1);
col[rt]=0;
}
}

void build(int l,int r,int rt){
if(l==r){
sum[rt]=1;
return;
}
int m=l+(r-l)/2;
build(lson);
build(rson);
pushup(rt);
}

void update(int l,int r,int rt,int L,int R,int c){
if(L<=l&&r<=R){
col[rt]=c;
sum[rt]=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int m=l+(r-l)/2;
if(L<=m) update(lson,L,R,c);
if(R>m) update(rson,L,R,c);
pushup(rt);
}

int main(){
int t;
scanf("%d",&t);
int kase=0;
while(t--){
int n,q;
scanf("%d%d",&n,&q);
mem0(col);
build(1,n,1);
while(q--){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(1,n,1,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",++kase,sum[1]);
}
return 0;
}


zoj 1610

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       8000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int col[MAX<<2];
int vis[MAX<<2];
int ans[MAX<<2];
int colour[MAX<<2];

void pushdown(int rt){
if(col[rt]!=-1){
col[rt<<1]=col[rt<<1|1]=col[rt];
col[rt]=-1;
}
}

void update(int l,int r,int rt,int L,int R,int c){
if(L<=l&&r<=R){
col[rt]=c;
return;
}
pushdown(rt);
int m=l+(r-l)/2;
if(L<=m) update(lson,L,R,c);
if(R>m) update(rson,L,R,c);
}

void query(int l,int r,int rt){
if(l==r){
if(col[rt]!=-1){
ans[l]=col[rt];
}
return;
}
pushdown(rt);
int m=l+(r-l)/2;
query(lson);
query(rson);
}
int main(){
int n;
while(~scanf("%d",&n)){
mem1(col);
mem0(vis);
mem1(ans);
mem0(colour);
for(int i=0;i<n;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(1,8000,1,a+1,b,c);
}
query(1,8000,1);
for(int i=1;i<=8000;i++){
if(ans[i]!=-1){
if(i==1) colour[ans[i]]++;
else if(ans[i]!=ans[i-1]) colour[ans[i]]++;
}
}
for(int i=0;i<=8000;i++){
if(colour[i]) printf("%d %d\n",i,colour[i]);
}
printf("\n");
}
return 0;
}

poj 3264

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       50000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int a[MAX];
int maxv[MAX<<2];
int minv[MAX<<2];
int _max,_min;

void pushup(int rt){
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}

void build(int l,int r,int rt){
if(l==r){
scanf("%d",&maxv[rt]);
minv[rt]=maxv[rt];
return;
}
int m=l+(r-l)/2;
build(lson);
build(rson);
pushup(rt);
}

void query(int l,int r,int rt,int L,int R){
if(L<=l&&r<=R){
_max=max(_max,maxv[rt]);
_min=min(_min,minv[rt]);
return ;
}
int m=l+(r-l)/2;
if(L<=m) query(lson,L,R);
if(R>m) query(rson,L,R);
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--){
int a,b;
scanf("%d%d",&a,&b);
_max=0;
_min=INF;
query(1,n,1,a,b);
printf("%d\n",_max-_min);
}
return 0;
}


hdu 4027

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       100000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

LL sum[MAX<<2];
LL ans;

void pushup(int rt){
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void build(int l,int r,int rt){
if(l==r){
scanf("%I64d",&sum[rt]);
return;
}
int m=l+(r-l)/2;
build(lson);
build(rson);
pushup(rt);
}

void update(int l,int r,int rt,int L,int R){
if(sum[rt]==(LL)(r-l+1)) return;
if(l==r){
sum[rt]=sqrt(double(sum[rt]));
return;
}
int m=l+(r-l)/2;
if(L<=m) update(lson,L,R);
if(R>m) update(rson,L,R);
pushup(rt);
}

void query(int l,int r,int rt,int L, int R){
if(L<=l&&r<=R){
ans+=sum[rt];
return;
}
int m=l+(r-l)/2;
if(L<=m) query(lson,L,R);
if(R>m) query(rson,L,R);
}
int main(){
int n;
int kase=0;
while(~scanf("%d",&n)){
kase++;
printf("Case #%d:\n",kase);
build(1,n,1);
int q;
scanf("%d",&q);
while(q--){
int t,a,b;
scanf("%d%d%d",&t,&a,&b);
if(a>b) swap(a,b);
if(t==0){
update(1,n,1,a,b);
}
else{
ans=0;
query(1,n,1,a,b);
printf("%I64d\n",ans);
}
}
printf("\n");
}
return 0;
}


hdu 1394

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>

using namespace std;
#define   MAX       10000+5
#define   MAXN      100000+5
#define   lson      l,m,rt<<1
#define   rson      m+1,r,rt<<1|1
#define   lrt       rt<<1
#define   rrt       rt<<1|1
#define   LL        long long
#define   ull       unsigned long long
#define   mem0(x)   memset(x,0,sizeof(x))
#define   mem1(x)   memset(x,-1,sizeof(x))
#define   meminf(x) memset(x,INF,sizeof(x))
#define   lowbit(x) (x&-x)

const int    mod   = 1000000007;
const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const int    INFF  = 1e9;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-10;

//读入外挂
int ret=0;
char tmp;
while(!isdigit(tmp=getchar()));
do{
ret=(ret<<3)+(ret<<1)+tmp-'0';
}while(isdigit(tmp=getchar()));
return ret;
}

int a[MAX];
int sum[MAX<<2];

void pushup(int rt){
sum[rt]=sum[lrt]+sum[rrt];
}

void update(int p,int l,int r,int rt){
if(l==r){
sum[rt]++;
return;
}
int m=l+(r-l)/2;
if(p<=m) update(p,lson);
else update(p,rson);
pushup(rt);
}

int query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R) return sum[rt];
int m=l+(r-l)/2;
int ans=0;
if(L<=m) ans+=query(L,R,lson);
if(R>m) ans+=query(L,R,rson);
return ans;
}
int main(){
int n;
while(~scanf("%d",&n)){
int cnt=0;
mem0(sum);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
cnt+=query(a[i],n-1,0,n-1,1);
update(a[i],0,n-1,1);
}
int mini=INF;
for(int i=0;i<n;i++){
cnt+=(n-1-2*a[i]);
mini=min(mini,cnt);
}
printf("%d\n",mini);
}
return 0;
}


hdu 2795

int maxv[MAX<<2];
int h,w,n;
int cnt;

void pushup(int rt){
maxv[rt]=max(maxv[lrt],maxv[rrt]);
}

void build(int l,int r,int rt){
if(l==r){
maxv[rt]=w;
return;
}
int m=l+(r-l)/2;
build(lson);
build(rson);
pushup(rt);
}

void query(int a,int l,int r,int rt){
if(maxv[rt]<a) return;
if(cnt!=-1) return;
if(l==r){
maxv[rt]-=a;
cnt=l;
return;
}
int m=l+(r-l)/2;
query(a,lson);
query(a,rson);
pushup(rt);
}

int main(){
while(~scanf("%d%d%d",&h,&w,&n)){
if(h>n) h=n;
build(1,n,1);
while(n--){
int a;
scanf("%d",&a);
cnt=-1;
query(a,1,h,1);
printf("%d\n",cnt);
}
}
return 0;
}

poj 2828

int pos[MAX],val[MAX];
int sum[MAX<<2];
int col[MAX<<2];
int n;

void pushup(int rt){
sum[rt]=sum[lrt]+sum[rrt];
}

void build(int l,int r,int rt){
if(l==r){
sum[rt]=1;
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
}

void update(int p,int v,int l,int r,int rt){
if(l==r){
sum[rt]=0;
col[rt]=v;
return;
}
mid;
if(p<=sum[lrt]) update(p,v,lson);
else update(p-sum[lrt],v,rson);
pushup(rt);
}

void query(int l,int r,int rt){
if(l==r){
printf("%d",col[rt]);
if(l!=n) printf(" ");
else printf("\n");
return;
}
mid;
query(lson);
query(rson);
}

int main(){
while(~scanf("%d",&n)){
for(int i=0;i<n;i++) scanf("%d%d",&pos[i],&val[i]);
build(1,n,1);
for(int i=n-1;i>=0;i--) update(pos[i]+1,val[i],1,n,1);
query(1,n,1);
}
return 0;
}


poj  3667

int cover[MAX<<2],sum[MAX<<2],lsum[MAX<<2],rsum[MAX<<2];

void pushdown(int rt,int m){
if(cover[rt]!=-1){
cover[lrt]=cover[rrt]=cover[rt];
if(cover[rt]==0){
lsum[lrt]=rsum[lrt]=sum[lrt]=m-(m>>1);
lsum[rrt]=rsum[rrt]=sum[rrt]=m>>1;
}
else{
lsum[lrt]=rsum[lrt]=sum[lrt]=lsum[rrt]=rsum[rrt]=sum[rrt]=0;
}
cover[rt]=-1;
}
}

void pushup(int rt,int m){
lsum[rt]=lsum[lrt];
rsum[rt]=rsum[rrt];
if(lsum[rt]==m-(m>>1)) lsum[rt]+=lsum[rrt];
if(rsum[rt]==m>>1) rsum[rt]+=rsum[lrt];
sum[rt]=max(rsum[lrt]+lsum[rrt],max(sum[lrt],sum[rrt]));
}

void build(int l,int r,int rt){
cover[rt]=-1;
sum[rt]=lsum[rt]=rsum[rt]=r-l+1;
if(l==r) return;
mid;
build(lson);
build(rson);
}

void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&r<=R){
cover[rt]=c;
if(cover[rt]==0) sum[rt]=lsum[rt]=rsum[rt]=r-l+1;
else sum[rt]=lsum[rt]=rsum[rt]=0;
return;
}
pushdown(rt,r-l+1);
mid;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
pushup(rt,r-l+1);
}

int query(int a,int l,int r,int rt){
if(l==r) return 1;
pushdown(rt,r-l+1);
mid;
if(sum[lrt]>=a) return query(a,lson);
else if(rsum[lrt]+lsum[rrt]>=a) return m-rsum[lrt]+1;
else query(a,rson);
}

int main(){
int n,m;
scanf("%d%d",&n,&m);
build(1,n,1);
mem1(cover);
while(m--){
int op;
scanf("%d",&op);
if(op==1){
int a;
scanf("%d",&a);
if(sum[1]<a) printf("0\n");
else{
int pos=query(a,1,n,1);
printf("%d\n",pos);
update(pos,pos+a-1,1,1,n,1);
}
}
else{
int a,b;
scanf("%d%d",&a,&b);
update(a,a+b-1,0,1,n,1);
}
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：137822次
• 积分：4413
• 等级：
• 排名：第7058名
• 原创：314篇
• 转载：5篇
• 译文：0篇
• 评论：46条
最新评论