题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少。
析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好。
用线段树的话,就维护三个值分别是左端点连续右端点连续,全连续的最长的区别,然后用线段树维护就好。
代码如下:
set过:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 5e4 + 10; const int mod = 1000000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } set<int> destroy; stack<int> st; int main(){ while(scanf("%d %d", &n, &m) == 2){ destroy.clear(); while(!st.empty()) st.pop(); destroy.insert(0); destroy.insert(n+1); while(m--){ char s[5]; int x; scanf("%s", s); if(s[0] == ‘D‘){ scanf("%d", &x); st.push(x); destroy.insert(x); } else if(s[0] == ‘R‘){ if(st.empty()) continue; destroy.erase(st.top()); st.pop(); } else{ scanf("%d", &x); if(destroy.count(x)){ printf("0\n"); continue; } set<int> :: iterator it1 = destroy.lower_bound(x); set<int> :: iterator it2 = it1--; printf("%d\n", *it2-*it1-1); } } } return 0; }
线段树:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 5e4 + 10; const int mod = 1000000007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } //0 - left 1 - right 2 - all int sum[maxn<<2][3]; void push_up(int rt, int len){ int l = rt<<1, r = rt<<1|1; sum[rt][0] = sum[l][0]; sum[rt][1] = sum[r][1]; sum[rt][2] = max(max(sum[l][2], sum[r][2]), sum[l][1]+sum[r][0]); if(sum[l][0] == len-len/2) sum[rt][0] += sum[r][0]; if(sum[r][1] == len/2) sum[rt][1] += sum[l][1]; } void build(int l, int r, int rt){ sum[rt][0] = sum[rt][1] = sum[rt][2] = r - l + 1; if(l == r) return ; int m = l + r >> 1; build(lson); build(rson); } void update(int M, int val, int l, int r, int rt){ if(l == r){ sum[rt][0] = sum[rt][1] = sum[rt][2] = val; return ; } int m = l + r >> 1; if(M <= m) update(M, val, lson); else update(M, val, rson); push_up(rt, r-l+1); } int query(int M, int l, int r, int rt){ if(l == r || sum[rt][2] == 0 || sum[rt][2] == r-l+1) return sum[rt][2]; int m = l + r >> 1; if(M <= m){ if(M > m - sum[rt<<1][1]) return query(M, lson) + sum[rt<<1|1][0]; return query(M, lson); } if(M < m+1 + sum[rt<<1|1][0]) return query(M, rson) + sum[rt<<1][1]; return query(M, rson); } stack<int> st; int main(){ while(scanf("%d %d", &n, &m) == 2){ build(1, n, 1); char s[10]; while(!st.empty()) st.pop(); while(m--){ scanf("%s", s); int x; if(s[0] == ‘D‘){ scanf("%d", &x); st.push(x); update(x, 0, 1, n, 1); } else if(s[0] == ‘R‘){ if(st.empty()) continue; update(st.top(), 1, 1, n, 1); st.pop(); } else{ scanf("%d", &x); printf("%d\n", query(x, 1, n, 1)); } } } return 0; }
时间: 2024-11-04 14:42:00