ZOJ 1610 Count the Colors (线段树区间染色)
来源:互联网 发布:java计算器课程设计 编辑:程序博客网 时间:2024/04/27 15:02
题意: 不断对区间染色 最后求每种颜色有连续的几段 按字典序输出
分析: 由于是区间染色啦 假如将区间全部看成点
[2,3] 1 [3,4] 2 这样的话 本来3颜色是1 却变成了2 会有问题
解决方式将[l,r]区间更新的时候用[l,r-1]来代替 保持原来的区间的性质
问题就 转化成了线段树区间更新的问题了
代码:
//// Created by TaoSama on 2015-09-16// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 8e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, lastColor;int row[N << 2];map<int, int> mp;#define root 1, n, 1#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1void push_down(int rt) { if(~row[rt]) { row[rt << 1] = row[rt << 1 | 1] = row[rt]; row[rt] = -1; }}void update(int L, int R, int c, int l, int r, int rt) { if(L <= l && r <= R) { row[rt] = c; return; } int m = l + r >> 1; push_down(rt); if(L <= m) update(L, R, c, lson); if(R > m) update(L, R, c, rson);}void query(int l, int r, int rt) { if(l == r) { if(row[rt] != lastColor) ++mp[row[rt]]; lastColor = row[rt]; return; } int m = l + r >> 1; push_down(rt); query(lson); query(rson);}int main() {#ifdef LOCAL freopen("in.txt", "r", stdin);// freopen("out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); while(scanf("%d", &n) == 1) { memset(row, -1, sizeof row); for(int i = 1; i <= n; ++i) { int x, y, c; scanf("%d%d%d", &x, &y, &c); update(x, y - 1, c, 0, 8000, 1); } lastColor = -1; mp.clear(); query(0, 8000, 1); for(map<int, int>::iterator iter = mp.begin(); iter != mp.end(); ++iter) { if(iter->first == -1) continue; printf("%d %d\n", iter->first, iter->second); } puts(""); } return 0;}
0 0
- ZOJ 1610 Count the Colors (线段树区间染色)
- ZOJ 1610Count the Colors 线段树区间染色问题
- ZOJ 1610Count the Colors(线段树+区间染色)
- ZOJ 1610 Count the Colors(线段树区间染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- zoj 1610 Count the Colors(线段树染色)
- zoj 1610 Count the Colors 线段树区间更新——染色问题
- ZOJ 1610 Count the Colors(线段树区间更新)
- zoj 1610 Count the Colors(线段树 区间更新)
- ZOJ 1610 Count the Colors(线段树 区间覆盖)
- ZOJ 1610 Count the Colors (线段树-区间覆盖)
- zoj 1610 Count the Colors (线段树区间覆盖)
- ZOJ 1610 Count the Colors 线段树 染色问题
- ZOJ 1610Count the Colors 线段树_区间更新
- ZOJ 1610 Count the Colors (线段树区间更新)
- zoj 1610 Count the Colors(线段树 区间更新)
- zoj 1610 Count the Colors 线段树 区间更新
- zoj 1610 Count the Colors(线段树)(成段更新染色)
- markdown使用书写
- LeetCode(80)Remove Duplicates from Sorted Array II
- Http的Cache机制
- TestArrayList
- 京东2016校园招聘笔试题
- ZOJ 1610 Count the Colors (线段树区间染色)
- JS用户表单
- Mac下使用自己安装的新版git
- 事件过滤器
- Sublime Text 3 打造舒服的工具(直接运行python)
- git---撤销 git add . 操作
- 自定义EditText:带清除按钮的EditText
- IP切换器源码
- Not saving crash log because we have reached the limit for logs to store on disk.解决办法