zoukankan      html  css  js  c++  java
  • “科大讯飞杯”第十七届同济大学程序设计预选赛暨高校网络友谊赛 H-时空栈

    题目链接

    分析

    将时间(t)离散化,按(t)建线段树,维护每个时间(t)的栈的大小(s[t])

    • 在时间(t)入栈一个数即为将区间([t,n])(1)
    • 在时间(t)出栈即为将区间([t,n])(1)
    • 查询时间(t)的栈顶元素, 找到时间(t)之前的最后的某个时间(t1)满足(s[t1]<s[t]),那么在(t1+1)时刻必定有一个入栈操作,且入栈的数即为时间(t)的栈顶元素。

    Code

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    #include<sstream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<bitset>
    #include<queue>
    #include<cmath>
    #include<stack>
    #include<set>
    #include<map>
    #define rep(i,x,n) for(int i=x;i<=n;i++)
    #define per(i,n,x) for(int i=n;i>=x;i--)
    #define sz(a) int(a.size())
    #define rson mid+1,r,p<<1|1
    #define pii pair<int,int>
    #define lson l,mid,p<<1
    #define ll long long
    #define pb push_back
    #define mp make_pair
    #define se second
    #define fi first
    using namespace std;
    const double eps=1e-8;
    const int mod=1e9+7;
    const int N=2e5+10;
    const int inf=1e9;
    int n,m;
    int b[N],op[N],t[N],v[N];
    set<pii>q;
    int mn[N<<2],tag[N<<2];
    void pd(int p,int k){
    	mn[p]+=k;
    	tag[p]+=k;
    }
    void up(int dl,int dr,int l,int r,int p,int k){
    	if(l==dl&&r==dr){
    		mn[p]+=k;
    		tag[p]+=k;
    		return;
    	}
    	pd(p<<1,tag[p]);pd(p<<1|1,tag[p]);tag[p]=0;
    	int mid=l+r>>1;
    	if(dr<=mid) up(dl,dr,lson,k);
    	else if(dl>mid) up(dl,dr,rson,k);
    	else up(dl,mid,lson,k),up(mid+1,dr,rson,k);
    	mn[p]=min(mn[p<<1],mn[p<<1|1]);
    }
    int qy(int x,int l,int r,int p){
    	if(l==r) return mn[p];
    	pd(p<<1,tag[p]);pd(p<<1|1,tag[p]);tag[p]=0;
    	int mid=l+r>>1;
    	if(x<=mid) return qy(x,lson);
    	else return qy(x,rson);
    }
    int qy(int dl,int dr,int l,int r,int p,int k){
    	if(l==r){
                  if(mn[p]<k) return l;
                  else return 0;
    	}
    	pd(p<<1,tag[p]);pd(p<<1|1,tag[p]);tag[p]=0;
    	int mid=l+r>>1;
    	if(l==dl&&r==dr){
                  if(mn[p<<1|1]<k) return qy(mid+1,dr,rson,k);
                  else if(mn[p<<1]<k) return qy(dl,mid,lson,k);
                  else return 0;
    	}
    	if(dr<=mid) return qy(dl,dr,lson,k);
            else if(dl>mid) return qy(dl,dr,rson,k);
            else return max(qy(dl,mid,lson,k),qy(mid+1,dr,rson,k));
    }
    int main(){
    	//ios::sync_with_stdio(false);
    	//freopen("in","r",stdin);
    	scanf("%d",&n);
    	rep(i,1,n){
    		scanf("%d%d",&op[i],&t[i]);
    		if(op[i]==0) scanf("%d",&v[i]);
    		b[++m]=t[i];
    	}
    	sort(b+1,b+m+1);
    	m=unique(b+1,b+m+1)-b-1;
    	rep(i,1,n){
    		t[i]=lower_bound(b+1,b+m+1,t[i])-b;
    	}
    	rep(i,1,n){
    		if(op[i]==0){
    			q.insert(mp(t[i],v[i]));
    			up(t[i],m,1,m,1,1);
    		}else if(op[i]==1){
    			up(t[i],m,1,m,1,-1);
    		}else{
    			int pos=qy(1,t[i],1,m,1,qy(t[i],1,m,1));
    			auto it=q.lower_bound(mp(pos+1,0));
    			printf("%d
    ",it->se);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    poj1330 Nearest Common Ancestors
    poj3237 Tree
    spoj2798 QTREE3 Query on a tree again!
    spoj913 QTREE2 Query on a treeⅡ
    自动类型转换
    js "+"连接符号
    js parseFloat
    js字符串与数字的运算
    js prompt
    js数组排序
  • 原文地址:https://www.cnblogs.com/xyq0220/p/12883682.html
Copyright © 2011-2022 走看看