求点所在的最长连续区间
发布时间:2020-12-13 21:27:01 所属栏目:PHP教程 来源:网络整理
导读:http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18628????Accepted Submission(s): 7171 Problem Description During the War
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel WarfareTime Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Problem Description
During the War of Resistance Against Japan,tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking,villages connected by tunnels lay in a line. Except the two at the ends,every village was directly connected with two neighboring ones.
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated,restoration of connection must be done immediately!
?
?
Input
The first line of the input contains two positive integers n and m (n,m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below: D x: The x-th village was destroyed. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself. R: The village destroyed last was rebuilt.
?
?
Output
Output the answer to each of the Army commanders’ request in order on a separate line.
?
?
Sample Input
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
?
?
Sample Output
1 0 2 4
?
?
Source
POJ Monthly
?
?
Recommend
LL
?https://blog.csdn.net/chudongfang2015/article/details/52133243
//#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,root*2+1); } tree[root].minn = min(tree[root*2].minn,tree[root*2+1].minn); } void update_max(int x,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,root*2); } else { update_max(x,root*2+1); } tree[root].maxn = max(tree[root*2].maxn,tree[root*2+1].maxn); } void queryl(int l,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,root*2); if(r > mid) queryl(l,root*2+1); } void queryr(int l,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,root*2); if(r > mid) queryr(l,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,1); s.push(x); } else if(str[0] == ‘Q‘) { sum = 0 ; int x; scanf("%d",&x); mi = INF,ma = -INF; queryl(1,1); queryr(x,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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |