标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
//1085422276 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<queue> #include<cmath> #include<map> #include<bitset> #include<set> #include<vector> #include<stack> ///#include<bits/stdc++.h> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)); #define memfy(a) memset(a,-1,sizeof(a)) #define TS printf("111111\n"); #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a,b) scanf("%d%d",&a,&b) #define mod 530600414 #define eps 0.0000000001 #define inf 1000000000.0 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } //**************************************** #define maxn 500000+5 struct ss { int l,r,L,R,v; }tr[maxn]; int n,m,lflag,rflag; void pushup(int k) { if(tr[k<<1].L+tr[k<<1|1].R==(tr[k].r-tr[k].l+1)) { tr[k].L=tr[k].R=(tr[k].r-tr[k].l+1); } else { if(tr[k<<1].L>=(tr[k<<1].r-tr[k<<1].l+1)) tr[k].L=tr[k<<1].L+tr[k<<1|1].L; else tr[k].L=tr[k<<1].L; if(tr[k<<1|1].R>=tr[k<<1|1].r-tr[k<<1|1].l+1) tr[k].R=tr[k<<1|1].R+tr[k<<1].R; else tr[k].R=tr[k<<1|1].R; } } void build(int k,int s,int t) { tr[k].l=s; tr[k].r=t; tr[k].L=tr[k].R=t-s+1; if(s==t) { return ; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); } void update(int k,int x,int c) { if(tr[k].l==x&&tr[k].r==x) { tr[k].L=tr[k].R=c;return ; } int mid=(tr[k].l+tr[k].r)>>1; if(x<=mid)update(k<<1,x,c); else update(k<<1|1,x,c); pushup(k); } int ask(int k,int x) { if(tr[k].l==tr[k].r&&tr[k].l==x) { if(tr[k].L)rflag=lflag=1; return tr[k].L; } int mid=(tr[k].l+tr[k].r)>>1; int A=0,B=0; if(x<=mid) { A=ask(k<<1,x); } else { B=ask(k<<1|1,x); } int ans=A+B; if(A && rflag) { ans+=tr[k<<1|1].L; if(tr[k<<1|1].L<tr[k<<1|1].r-tr[k<<1|1].l+1) rflag=0; } if(B && lflag) { ans+=tr[k<<1].R; if(tr[k<<1].R<tr[k<<1].r-tr[k<<1].l+1) lflag=0; } return ans; } int main() { char ch[321]; int x; while(scanf("%d%d",&n,&m)!=EOF) { stack<int >q; int last=0; build(1,1,n); FOR(i,1,m) { scanf("%s",ch); if(ch[0]==‘D‘) {cin>>x; q.push(x); update(1,x,0); } else if(ch[0]==‘Q‘) { cin>>x; rflag=lflag=0; cout<<ask(1,x)<<endl; } else { last=q.top(); q.pop(); update(1,last,1); } } } return 0; }
HDU 1540 Tunnel Warfare 线段树:单点更新,区间合并
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4843726.html