POJ 2528 Mayor's posters
来源:互联网 发布:韩寒新作品2018 知乎 编辑:程序博客网 时间:2024/06/07 10:33
#ifdef _DEBUG#pragma warning(disable : 4996)#endif#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <functional>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#define CLEAR(a, b) memset(a, b, sizeof(a))#define CLOSE() ios::sync_with_stdio(false)#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)#define PF(a) printf("%d\n", a)#define SF(a) scanf("%d", &a)#define SFF(a, b) scanf("%d%d", &a, &b)#define FOR(i, a, b) for(int i = a; i < b; ++i)#define LL long long#define MAXN 10005#define MAXM 100005#define MOD 1000007#define INF 2147483648using namespace std;//-------------------------CHC------------------------------////POJ 2528 Mayor's posters//思路:由于区间更新后都PushDown到了最底层,所以查询也得到最底层逐个计数,比较耗时。#define Lson cur<<1, l, mid#define Rson cur<<1|1, mid+1, rint n, m;int low[MAXN], high[MAXN];int col[MAXN << 4];int cnt[MAXN << 4];vector<int> v;void PushDown(int cur) {col[cur << 1] = col[cur << 1 | 1] = col[cur];col[cur] = 0;}void Update(int L, int R, int c, int cur, int l, int r) {if (L <= l && r <= R) {col[cur] = c;//printf("col = %d\n", col[cur]);return;}if (col[cur]) PushDown(cur);int mid = (l + r) >> 1;if (L <= mid) Update(L, R, c, Lson);if (R > mid) Update(L, R, c, Rson);}void Query(int cur, int l, int r) {if (l == r) {if(col[cur]) cnt[col[cur]]++;//printf("col = %d\n", col[cur]);return;}if(col[cur]) PushDown(cur);int mid = (l + r) >> 1;Query(Lson);Query(Rson);}void Hash() {v.clear();FOR(i, 0, n) v.push_back(low[i]), v.push_back(high[i]);sort(v.begin(), v.end()); int t = v.size();FOR(i, 1, t) {if (v[i] - v[i - 1] > 1) v.push_back(v[i - 1] + 1);}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());//for (auto i : v) printf("%d ", i); puts("");m = v.size();FOR(i, 0, n) {int L, R;L = find(v.begin(), v.end(), low[i]) - v.begin();R = find(v.begin(), v.end(), high[i]) - v.begin();//printf("(%d, %d)\n", L + 1, R + 1);Update(L+1, R+1, i + 1, 1, 1, m);}}int main() {int T;SF(T);while (T--) {CLEAR(col, 0);CLEAR(cnt, 0);SF(n);FOR(i, 0, n) SFF(low[i], high[i]);Hash();Query(1, 1, m);int ans = 0;FOR(i, 1, m + 1) if (cnt[i]) ans++;PF(ans);}return 0;}仅仅是更改了查询方式,就快了很多了。
存在部分点,没有pushdown到叶子节点。
(还是有点不清楚,感觉并没有太多的点能直接返回的诶)
void query(int cur, int l, int r) {if (res[cur]) cnt[res[cur]] = 1;else {if (l == r) return;int mid = Mid(l, r);PushDown(cur);query(lson);query(rson);}}
阅读全文
0 0
- POJ 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's Posters
- Mayor's posters poj 2528
- poj 2528 Mayor's posters
- Poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 - Mayor's posters
- POJ-2528-Mayor's posters
- poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- poj 2528 Mayor's posters
- POJ 2528 Mayor's posters
- POJ 2528 Mayor's posters
- Poj 2528 Mayor's posters
- Poj 2528 Mayor's posters
- Android,debug正常运行,用其他途径打开程序就会崩溃,且没报异常!
- haut 1280: 诡异的迷宫(多次bfs)
- POI对docx模板进行文字、图片替换
- Docker 简介
- ngrx/store
- POJ 2528 Mayor's posters
- python 3.6.0 scrapy框架安装方法及疑难解决
- Intermediate Algorithm Scripting FreeCodeCamp中级算法
- java 读取excel 文件 Unable to recognize OLE stream 错误
- TCP_NODELAY 和 TCP_CORK
- ARKit从入门到精通(11)-ARKit开发常见问题及解决方案
- 梯度爆炸与梯度消失
- Android Studio 新建文件自动加入SVN版本控制
- 单链表的数组实现(未完)