标签:name define car 边界值 self lse eof dir scribe
InputThe 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.
OutputOutput 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
题解:解一:线段树区间合并
通过数组记录各个节点左or右孩子的最长连续区间,再通过区间的合并将该节点区间的最大值更新到一个数组中,然后进行查询
解二:记录节点左孩子区间的最右区间边界值L和右孩子区间的最左区间边界值R,查询的时候计算出的 R-L+1 即为最长区间值
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <list> #include <deque> #include <queue> #include <stack> #include <cstdlib> #include <cstdio> #include <cmath> #include <iomanip> #define ull unsigned long long #define ll long long #define pb push_back #define mem(sum,x) memset(sum,x,sizeof(sum)) #define rep(i,start,end) for(int i=start;i<=end;i++) #define per(i,end,start) for(int i=end;i>=start;i--) #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; const int mod = 998244353; const int mxn = 2e5 +7; int _,n,m,t,k,u,v,ans,cnt,ok,lim; ll w[mxn] , cost[mxn] , far[mxn] , siz[mxn]; char ch; #define lc now<<1 #define rc now<<1|1 string str ; struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4]; int lmx[mxn] , rmx[mxn] , num[mxn] ; void pushup(int l,int r,int now) { lmx[now] = lmx[lc] ; rmx[now] = rmx[rc]; int mid = (l+r)>>1; if(lmx[now]==mid-l+1) lmx[now]+=lmx[rc]; if(rmx[now]==r-mid) rmx[now]+=rmx[lc]; num[now] = max( rmx[lc]+lmx[rc] , max(num[lc],num[rc]) ); } void build(int l,int r,int now) { if(l==r){ lmx[now] = rmx[now] = num[now] = 1 ; return ; } int mid = (l+r)>>1; build(l,mid,lc);build(mid+1,r,rc);pushup(l,r,now); } void up(int l,int r,int k,int now,int lim) { if(l==r){ if(l==k){ num[now] = lmx[now] = rmx[now] = (lim?0:1); } return ; } int mid = (l+r)>>1; if(k<=mid) up(l,mid,k,lc,lim); if(k>mid) up(mid+1,r,k,rc,lim); pushup(l,r,now); } int ask(int l,int r,int k,int now) { if(num[now] == r-l+1 || !num[now]) return num[now] ; int mid = (l+r)>>1; if(k<=mid) { if(k>=mid-rmx[lc]+1) return ask(l,mid,k,lc) + ask(mid+1,r,mid+1,rc); else return ask(l,mid,k,lc); } else { if(k<=mid+1+lmx[rc]-1) return ask(l,mid,mid,lc) + ask(mid+1,r,k,rc); else return ask(mid+1,r,k,rc); } } int main() { while(cin>>n>>m) { stack<int>s; build(1,n,1); while(m--) { cin>>ch; if(ch==‘D‘){ cin>>k; s.push(k); up(1,n,k,1,1); } else if(ch==‘Q‘){ cin>>k; cout<<ask(1,n,k,1)<<endl; } else { up(1,n,s.top(),1,0);s.pop(); } } } }
Tunnel Warfare HDU - 1540 (区间合并)
标签:name define car 边界值 self lse eof dir scribe
原文地址:https://www.cnblogs.com/Shallow-dream/p/12828962.html