ZOJ1610 Count the Colors 线段树

正常区间修改,然后最后一起暴力查一遍就行了。

区间修改有0,需要用-1做lzy标记....

 1 #include <cstdio>
 2 #include <map>
 3 #include <algorithm>
 4 using namespace std;
 5 int col[40000],lzy[40000],vec[10000],cnt[10000];
 6 int n;
 7 void build(int k,int l,int r)
 8 {
 9     col[k] = -1;
10     lzy[k] = -1;
11     if (l == r)
12     {
13         return;
14     }
15     int mid = l + r >> 1;
16     build(k << 1,l,mid);
17     build(k << 1 | 1,mid + 1,r);
18 }
19 void down(int k,int l,int r)
20 {
21     if (l == r)
22         return;
23     lzy[k << 1] = lzy[k];
24     lzy[k << 1 | 1] = lzy[k];
25     col[k << 1] = lzy[k];
26     col[k << 1 | 1] = lzy[k];
27     lzy[k] = -1;
28 }
29 void change(int k,int l,int r,int x,int y,int c)
30 {
31     if (x <= l && r <= y)
32     {
33         lzy[k] = c;
34         col[k] = c;
35         return;
36     }
37     if (lzy[k] != -1)
38         down(k,l,r);
39     int mid = l + r >> 1;
40     if (x <= mid) change(k << 1,l,mid,x,y,c);
41     if (y >= mid + 1) change(k << 1 | 1,mid + 1,r,x,y,c);
42 }
43 int query(int k,int l,int r,int x)
44 {
45     if (l == r)
46         return col[k];
47     if (lzy[k] != -1)
48         down(k,l,r);
49     int mid = l + r >> 1;
50     if (x <= mid)
51         return query(k << 1,l,mid,x);
52     if (x >= mid + 1)
53         return query(k << 1 | 1,mid + 1,r,x);
54 }
55 int main()
56 {
57     while (scanf("%d",&n) > 0)
58     {
59         build(1,1,8001);
60         for (int i = 0;i <= 8000;i++)
61             cnt[i] = 0;
62         int tx,ty,tc;
63         for (int i = 1;i <= n;i++)
64         {
65             scanf("%d%d%d",&tx,&ty,&tc);
66             tx++;
67             change(1,1,8001,tx,ty,tc);
68         }
69         for (int i = 0;i <= 8000;i++)
70             vec[i] = query(1,1,8001,i + 1);
71         if (vec[0] != -1)
72             cnt[vec[0]]++;
73         for (int i = 1;i <= 8000;i++)
74             if (vec[i] != -1 && vec[i] != vec[i - 1])
75                 cnt[vec[i]]++;
76         for (int i = 0;i <= 8000;i++)
77             if (cnt[i] != 0)
78                 printf("%d %d\n",i,cnt[i]);
79         printf("\n");
80     }
81     return 0;
82 }

原文地址:https://www.cnblogs.com/iat14/p/12188892.html

时间: 2024-10-22 13:02:47

ZOJ1610 Count the Colors 线段树的相关文章

ZOJ-1610 Count the Colors ( 线段树 )

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different

Count the Colors(线段树之区间成段更新)

萌萌哒的传送门 这道题目是线段树区间成段更新的应用,我们只需在建立线段树时从原来的左右儿子不相连,改为相连即可以解决此类问题. 如从原来的[l,mid] , [mid + 1,r] 改为 [l,mid],[mid,r]即可; /********************* * zoj1610 * * 线段树的区间成段更新 * 延迟标记 * *********************/ #include <iostream> #include <cstring> #include &l

ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)

Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can s

ZOJ 1610 Count the Colors (线段树成段更新)

题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以对于区间(L, R)我们只要让左端点+1即可按照正常的线段树操作来做. #include<bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; co

ZOJ 1610 Count the Colors(线段树,但暴力未必不行)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different color

ZOJ - 1610 Count the Colors 线段树区间修改

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones. Your task is counting the segments of different colors you can see at last. InputThe first line of each data set contains exactly o

ZOJ1610 Count the Colors 经典线段树染色问题

题意,给你n个  x,y,c,意思就是区间[x,y]被染成C色,但是颜色会被覆盖的,染色操作完成以后 问你每种颜色有多少段 并输出颜色编号id跟段数cnt 经典问题,不过写的有点撮吧,没去看别人的,这个方法应该是最传统的最普通的,常规的开数组记录,也许大神们有更高端的方法 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring>

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

ZOJ1610_Count the Colors(线段树/成段更新)

解题报告 题意: 一根长度8000的线段上染色,求染完之后,每个颜色在线段上有多少个间断的区间. 思路: 区间问题用线段树,成段的更新区间,最后把所有的区间下压到叶子结点,统计叶子结点的颜色. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int lz[32000],_hash[10000],color[10000],cnt; void push_down(in