这个题目的大意就是,求某个点所在的最长连续区间
区间合并此类题目的通常做法都是开3个变量,区间最大值,从左往右的连续区间长度,从右往左的连续区间长度。
然后更新的时候更新到点就行了。
查询的时候,只需要知道,某个点所在的连续区间,一定是由某个线段的左儿子的从右往左的连续区间和右儿子的从左往右的连续区间构成的
/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 50005
#define eps 1e-11
#define L(x) x<<1
#define R(x) x<<1|1
using namespace std;
struct node
{int left, right, mid;int llen, rlen, len;
}tree[4 * MAXN];
stack<int>st;
void make_tree(int s, int e, int C)
{tree[C].left = s;tree[C].right = e;tree[C].mid = (s + e) >> 1;tree[C].len = tree[C].llen = tree[C].rlen = e - s + 1;if(s == e) return;make_tree(s, tree[C].mid, L(C));make_tree(tree[C].mid + 1, e, R(C));
}
void up(int C)
{tree[C].len = max(tree[L(C)].rlen + tree[R(C)].llen, max(tree[L(C)].len, tree[R(C)].len));tree[C].llen = tree[L(C)].llen;tree[C].rlen = tree[R(C)].rlen;if(tree[L(C)].llen == tree[C].mid - tree[C].left + 1) tree[C].llen += tree[R(C)].llen;if(tree[R(C)].rlen == tree[C].right - tree[C].mid) tree[C].rlen += tree[L(C)].rlen;
}
void update(int p, int v, int C)
{if(tree[C].left == tree[C].right){tree[C].len = tree[C].llen = tree[C].rlen = v;return;}if(tree[C].mid >= p) update(p, v, L(C));else update(p, v, R(C));up(C);
}
int query(int p, int C)
{if(tree[C].left == tree[C].right || tree[C].len == 0 || tree[C].len == tree[C].right - tree[C].left + 1){return tree[C].len; //查询时,如果区间连续长度为0或者连续长度为区间长度,直接返回即可}if(tree[C].mid >= p){if(p >= tree[C].mid - tree[L(C)].rlen + 1) return tree[L(C)].rlen + tree[R(C)].llen;else return query(p, L(C));}else{if(p <= tree[R(C)].llen + tree[C].mid) return tree[R(C)].llen + tree[L(C)].rlen;else return query(p, R(C));}
}
int main()
{int n, m, x;char s[5];while(scanf("%d%d", &n, &m) != EOF){make_tree(1, n, 1);while(!st.empty()) st.pop();while(m--){scanf("%s", s);if(s[0] == 'R'){if(!st.empty()){update(st.top(), 1, 1);st.pop();}}else if(s[0] == 'D'){scanf("%d", &x);update(x, 0, 1);st.push(x);}else{scanf("%d", &x);printf("%d\n", query(x, 1));}}}return 0;
}