http://acm.hdu.edu.cn/showproblem.php?pid=1540
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18628 Accepted Submission(s): 7171
//#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdio.h> #include <queue> #include <stack>; #include <map> #include <set> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f #define mod 1000000007 #define PI acos(-1) using namespace std; typedef long long ll ; const int N = 50009; ll sum ; int n , m ; int a[50009]; char str[5]; int ma , mi ; struct node { int l , r ; int maxn , minn ; }tree[N<<2]; void build(int l ,int r , int root) { tree[root].l = l , tree[root].r = r ; if(l == r) { tree[root].maxn = 0 ; tree[root].minn = n+1 ; return ; } int mid = (l + r)>>1 ; build(l , mid , root*2); build(mid+1 , r , root*2+1); tree[root].maxn = max(tree[root*2].maxn , tree[root*2+1].maxn) ; tree[root].minn = min(tree[root*2].minn , tree[root*2+1].minn) ; } void update_min(int x , int val , int root) { if(tree[root].l == tree[root].r) { tree[root].minn = val ; return ; } int mid = (tree[root].r + tree[root].l) >> 1 ; if(x <= mid) { update_min(x , val , root*2); } else { update_min(x , val , root*2+1); } tree[root].minn = min(tree[root*2].minn , tree[root*2+1].minn); } void update_max(int x , int val , int root) { if(tree[root].l == tree[root].r) { tree[root].maxn = val ; return ; } int mid = (tree[root].r + tree[root].l) >> 1 ; if(x <= mid) { update_max(x , val , root*2); } else { update_max(x , val , root*2+1); } tree[root].maxn = max(tree[root*2].maxn , tree[root*2+1].maxn); } void queryl(int l , int r , int root) { if(tree[root].l >= l && tree[root].r <= r) { ma = max(ma , tree[root].maxn); return ; } int mid = ( tree[root].l + tree[root].r ) >> 1 ; if(l <= mid) queryl(l , r , root*2); if(r > mid) queryl(l , r , root*2+1); } void queryr(int l , int r , int root) { if(tree[root].l >= l && tree[root].r <= r) { mi = min(mi , tree[root].minn); return ; } int mid = (tree[root].l + tree[root].r) >> 1 ; if(l <= mid) queryr(l , r , root*2); if(r > mid) queryr(l , r , root*2+1); } int main() { while(~scanf("%d%d" , &n , &m)) { stack<int>s; build(1 , n , 1); int l = 0 ; for(int i = 0 ; i < m ; i++) { scanf("%s" , str); if(str[0] == ‘D‘) { int x ; scanf("%d" , &x); update_max(x , x , 1); update_min(x , x , 1); s.push(x); } else if(str[0] == ‘Q‘) { sum = 0 ; int x; scanf("%d" , &x); mi = INF , ma = -INF; queryl(1 , x , 1); queryr(x , n , 1); if(mi == ma) cout << 0 << endl ; else cout << mi - ma - 1 <<endl ; } else { int x ; x = s.top(); s.pop(); update_max(x , 0 , 1); update_min(x , n+1 , 1); } } } return 0; }
原文:https://www.cnblogs.com/nonames/p/11607997.html