Zoj1610 count the colors classical line segment tree dyeing problem

Source: Internet
Author: User

Here are N values of X, Y, and C, which means that the interval [x, y] is colored in C, but the color will be covered, after the dyeing operation is complete, ask you how many segments each color has and output the color ID and segment count CNT


Classic problems, but I'm a bit confused. I didn't go to anyone else's. This method should be the most traditional and common, and the conventional open array record. Maybe the great gods have a higher-end method.



#include<iostream>#include<cstdio>#include<list>#include<algorithm>#include<cstring>#include<string>#include<queue>#include<stack>#include<map>#include<vector>#include<cmath>#include<memory.h>#include<set>#include<cctype>#define ll long long#define LL __int64#define eps 1e-8#define inf 0xfffffff//const LL INF = 1LL<<61;using namespace std;//vector<pair<int,int> > G;//typedef pair<int,int > P;//vector<pair<int,int> > ::iterator iter;////map<ll,int >mp;//map<ll,int >::iterator p;const int N = 8000 + 5;typedef struct Node {int l,r;int col;};Node tree[N * 4];int color[N];int ans[N];void init() {memset(color,-1,sizeof(color));memset(ans,0,sizeof(ans));}void build(int l,int r,int id) {tree[id].l = l;tree[id].r = r;tree[id].col = -1;if(l == r)return;int mid = (l + r)/2;build(l,mid,id<<1);build(mid+1,r,id<<1|1);}void cal(int id) {if(tree[id].col > -1) {tree[id<<1].col = tree[id].col;tree[id<<1|1].col = tree[id].col;tree[id].col = -1;}}void update(int l,int r,int id,int col) {if(tree[id].col == col)return;if(l <= tree[id].l && r >= tree[id].r) { tree[id].col = col;return;}cal(id);int mid = (tree[id].l + tree[id].r)/2;if(r <= mid)update(l,r,id<<1,col);else if(l > mid)update(l,r,id<<1|1,col);else {update(l,mid,id<<1,col);update(mid+1,r,id<<1|1,col);}}void query(int l,int r,int id) {if(tree[id].col > -1) {for(int i=tree[id].l;i<=tree[id].r;i++)color[i] = tree[id].col;return;}if(tree[id].l == tree[id].r)return;int mid = (tree[id].l + tree[id].r)/2;if(r <= mid)query(l,r,id<<1);else if(l > mid)query(l,r,id<<1|1);else {query(l,mid,id<<1);query(mid+1,r,id<<1|1);}}int main() {int n;while(scanf("%d",&n) == 1) {int q = n;init();build(1,N,1);int x,y,c;while(q--) {scanf("%d %d %d",&x,&y,&c);x++;update(x,y,1,c);}query(1,N,1);for(int i=1;i<N;i++)if(color[i - 1] != color[i])if(color[i] > -1)ans[color[i]]++;for(int i=0;i<N;i++)if(ans[i])printf("%d %d\n",i,ans[i]);puts("");}return 0;}


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.