HDU 1199 Color the Ball

来源:互联网 发布:三次元绅士软件 编辑:程序博客网 时间:2024/06/06 01:12
Problem Description
There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.
 

Input
First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.
 

Output
Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".
 

Sample Input
31 4 w8 11 w3 5 b
 

Sample Output
8 11

线段树动态建立节点

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 400005;int n, m, l, r;char s[10];struct node{LL l, lr, r, rl, ml, mr;int flag;};struct ST{LL L[maxn], R[maxn], n, t;node f[maxn];void build(){n = 1 << 30;n = n + n - 1;L[1] = R[1] = 0;t = 1;f[1].l = 1;f[1].r = n;f[1].lr = f[1].rl = -1;f[1].ml = f[1].mr = -1;f[0].l = f[0].r = 0;f[0].lr = f[0].rl = -1;f[0].ml = f[0].mr = -1;f[0].flag = f[1].flag = 0;}int newnode(LL l, LL r){t++;L[t] = R[t] = 0;f[t].l = l; f[t].r = r;f[t].lr = f[t].mr = -1;f[t].rl = f[t].ml = -1;f[t].flag = 0;return t;}void get(LL &a, LL&b, LL l, LL r){if (l < 0 || r < 0) return;if (a < 0) { a = l, b = r; return; }if (r - l>b - a) { a = l, b = r; return; }if (r - l == b - a && l < a) { a = l, b = r; return; }}void update(LL x, LL l, LL r, LL lx, LL rx){LL mid = (l + r) >> 1;if (f[lx].lr == f[lx].r&&f[rx].lr > 0) f[x].lr = f[rx].lr;else f[x].lr = f[lx].lr;if (f[rx].rl == f[rx].l&&f[lx].rl > 0) f[x].rl = f[lx].rl;else f[x].rl = f[rx].rl;f[x].ml = f[x].mr = -1;get(f[x].ml, f[x].mr, f[lx].ml, f[lx].mr);get(f[x].ml, f[x].mr, f[rx].ml, f[rx].mr);get(f[x].ml, f[x].mr, f[lx].rl, f[rx].lr);}void add(LL x, LL l, LL r, LL ll, LL rr, LL v){if (ll <= l&&r <= rr){f[x].flag = v ? v: -1;if (v){f[x].mr = f[x].lr = f[x].r;f[x].ml = f[x].rl = f[x].l;}else{f[x].mr = f[x].lr = -1;f[x].ml = f[x].rl = -1;}}else{LL mid = (l + r) >> 1;if (f[x].flag){if (!L[x]) L[x] = newnode(l, mid);if (!R[x]) R[x] = newnode(mid + 1, r);if (f[x].flag > 0){f[L[x]].mr = f[L[x]].lr = f[L[x]].r;f[L[x]].ml = f[L[x]].rl = f[L[x]].l;f[R[x]].mr = f[R[x]].lr = f[R[x]].r;f[R[x]].ml = f[R[x]].rl = f[R[x]].l;}else{f[L[x]].lr = f[L[x]].rl = f[R[x]].lr = f[R[x]].rl = -1;f[L[x]].ml = f[L[x]].mr = f[R[x]].ml = f[R[x]].mr = -1;}f[L[x]].flag = f[R[x]].flag = f[x].flag;f[x].flag = 0;}if (ll <= mid){if (!L[x]) L[x] = newnode(l, mid);add(L[x], l, mid, ll, rr, v);}if (rr > mid){if (!R[x]) R[x] = newnode(mid + 1, r);add(R[x], mid + 1, r, ll, rr, v);}update(x, l, r, L[x], R[x]);}}void insert(LL l, LL r, LL x){add(1, 1, n, l, r, x);}bool solve(){if (f[1].ml >= 0) return true;return false;}}st;int main(){while (scanf("%d", &n) != EOF){st.build();for (int i = 1; i <= n; i++){scanf("%d%d%s", &l, &r, s);if (s[0] == 'w') st.insert(l, r, 1);else st.insert(l, r, 0);}if (st.solve()) printf("%lld %lld\n", st.f[1].ml, st.f[1].mr);else printf("Oh, my god\n");} return 0;}


0 0
原创粉丝点击