Ural 1019. Line Painting

Source: Internet
Author: User

Source: Ural 1019. Line Painting

Question: The longest section is all white intervals.

Train of Thought: Merge and discretization of segment update segments in a line segment tree. This corresponds to a segment. So each time it is not m + 1, it is l M and M r. In addition, I added the 0 and 10 ^ 9 points.

Each section (L, R) records the number of lines between L and R.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 50010;int sum[maxn<<2], lsum[maxn<<2], rsum[maxn<<2], co[maxn<<2];struct node{int x, v, id;}c[maxn*2];int num[maxn*2];char s[maxn][5];void pushup(int l, int r, int rt){int k = r-l+1;int m = (l+r) >> 1;lsum[rt] = lsum[rt<<1];rsum[rt] = rsum[rt<<1|1];if(lsum[rt] == num[m]-num[l])lsum[rt] += lsum[rt<<1|1];if(rsum[rt] == num[r]-num[m])rsum[rt] += rsum[rt<<1];sum[rt] = max(sum[rt<<1], sum[rt<<1|1]);sum[rt] = max(sum[rt], rsum[rt<<1]+lsum[rt<<1|1]);}void build(int l, int r, int rt){sum[rt] = lsum[rt] = rsum[rt] = num[r]-num[l];co[rt] = -1;if(l + 1 == r)return;int m = (l+r) >> 1;build(l, m, rt<<1);build(m, r, rt<<1|1);//pushup(l, r, rt);}void pushdown(int l, int r, int rt){if(co[rt] != -1){int k = r-l+1;int m = (l + r) >> 1;sum[rt<<1] = co[rt]*(num[m]-num[l]);lsum[rt<<1] = co[rt]*(num[m]-num[l]);rsum[rt<<1] = co[rt]*(num[m]-num[l]);sum[rt<<1|1] = co[rt]*(num[r]-num[m]);lsum[rt<<1|1] = co[rt]*(num[r]-num[m]);rsum[rt<<1|1] = co[rt]*(num[r]-num[m]);co[rt<<1] = co[rt<<1|1] = co[rt];co[rt] = -1;}}int query(int l, int r, int rt, int a){if(l + 1 == r)return r;pushdown(l, r, rt);int m = (l+r) >> 1;//printf("*****%d %d %d %d %d\n", l, r, sum[rt<<1], sum[rt<<1|1], a);if(sum[rt<<1] >= a)return query(l, m, rt<<1, a);else if(rsum[rt<<1] + lsum[rt<<1|1] >= a)return query(m, r, rt<<1|1, a-rsum[rt<<1]);elsereturn query(m, r, rt<<1|1, a);}void update(int x, int y, int l, int r, int rt, int c){if(x == l && y == r){sum[rt] = lsum[rt] = rsum[rt] = c ? (num[r]-num[l]) : 0;co[rt] = c;return;}pushdown(l, r, rt);int m = (l+r) >> 1;if(y <= m)update(x, y, l, m, rt<<1, c);else if(x >= m)update(x, y, m, r, rt<<1|1, c);else{update(x, m, l, m, rt<<1, c);update(m, y, m, r, rt<<1|1, c);}pushup(l, r, rt);}bool cmp1(node a, node b){return a.x < b.x;}bool cmp2(node a, node b){return a.id < b.id;}int main(){int q;while(scanf("%d", &q) != EOF){for(int i = 0; i < q*2; i++){scanf("%d", &c[i].x);c[i].id = i;if(i&1)scanf("%s", s[(i-1)/2]);}sort(c, c+2*q, cmp1);int now = c[0].x, n = 2;num[1] = 0;for(int i = 0; i < q*2; i++){if(now != c[i].x){n++;now = c[i].x;}c[i].v = n;num[n] = now;}num[++n] = 1000000000;//for(int i = 1; i <= n; i++)//printf("***%d\n", num[i]);//printf("%d\n", n);sort(c, c+2*q, cmp2);build(1, n, 1);for(int i = 0; i < q; i++){int a = c[i<<1].v;int b = c[i<<1|1].v;int color = 1;if(s[i][0] == 'b')color = 0;update(a, b, 1, n, 1, color);//printf("***%d %d %d\n", a, b, sum[1]);}int ans = query(1, n, 1, sum[1]);printf("%d %d\n", num[ans]-sum[1], num[ans]);}return 0;}


 

Ural 1019. Line Painting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.