思路:线段树的入门题目,先染色,最后统计,color=-2表示区间有多种颜色,-1表示未涂颜色
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <cctype>
- #include <queue>
- /*
- #include <list>
- #include <stack>
- #include <set>
- #include <map>
- #include <cmath>
- */
- using namespace std;
- #define ma(a,b) (a)>(b)?(a):(b)
- #define mi(a,b) (a)<(b)?(a):(b)
- #define FF(i,a,b) for(int i=(a);i<=(b);i++)
- #define RR(i,b) for(int i=(0);i<(b);i++)
- #define clr(x) memset(x,0,sizeof(x))
- #define pb push_back
- #define mp make_pair
- #define sz size()
- #define F first
- #define S second
- #define vv vector
- #define ii iterator
- /*
- #include <sstream>
- #include <iterator>
- #define ssm stringstream
- */
- #define cn continue
- #define br break
- typedef int type;
- const int ST = 0;
- #define out(x) cout << #x << "=" << x << endl
- template <class T> void show(T a, int n) { for (int i = ST; i < ST+n; i++) { cout<<a[i]<<' '; } cout<<endl; }
- template <class T> void show(T a, int r, int l) { for (int i = ST; i < ST+r; i++) show(a[i], l); cout<<endl; }
- const int N = 10000, M = 8000;
- struct node
- {
- int l,r,color;
- };
- node a[3*M];
- int n, b[N], num[N];
- void build(int root, int x, int y)
- {
- int len=y-x;
- a[root] = (node){x,y,-1};
- if(len>1)
- {
- int mid=(x+y)/2;
- build(root*2,x,mid);
- build(root*2+1,mid,y);
- }
- }
- void insert(int root, int x1, int x2, int c)
- {
- int mid=(a[root].l+a[root].r)/2;
- if(a[root].color==c)return;
- if(x1<=a[root].l && x2>=a[root].r)
- {
- a[root].color=c;
- }
- else
- {
- //hint: don't forget
- if(a[root].color>=0)
- {
- a[root*2].color=a[root].color;
- a[root*2+1].color=a[root].color;
- }
- a[root].color=-2;
- if(x1<mid)insert(root*2,x1,x2,c);
- if(x2>mid)insert(root*2+1,x1,x2,c);
- }
- }
- void paint(int root)
- {
- if(a[root].color>=-1)
- {
- for(int i=a[root].l;i<a[root].r;i++)
- {
- b[i]=a[root].color;
- }
- }
- else
- {
- paint(root*2);
- paint(root*2+1);
- }
- }
- void cal()
- {
- paint(1);
- for(int i=0;i<M;i++)
- if(b[i]>=0)
- {
- num[b[i]]++;
- while(i+1<M && b[i]==b[i+1])i++;
- }
- //show(b,10);
- }
- void print()
- {
- for(int i=0;i<=M;i++)
- {
- if(num[i]!=0)
- {
- printf("%d %d/n", i, num[i]);
- }
- }
- puts("");
- }
- int main()
- {
- while( scanf("%d", &n) == 1)
- {
- int i,x1,x2,c;
- clr(a);
- memset(b,-1,sizeof(b));
- clr(num);
- build(1,0,M);
- for(i=0;i<n;i++)
- {
- scanf("%d%d%d",&x1,&x2,&c);
- insert(1,x1,x2,c);
- //int(1);
- //ow(b,10);
- //memset(b,-1,sizeof(b));
- //printf("%d %d %d/n", x1,x2,c);
- }
- cal();
- print();
- }
- return 0;
- }