1、Codeforces 272C Dima and Staircase

解题思路:

区间更新

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>using namespace std;#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1typedef long long ll;const ll mod = 1e9 + 7;
const ll INF = 0x7fffffff;
const ll maxn = 1e5 + 10;ll n, m, w, h, ans;
ll a[maxn];
ll tree[4*maxn], lazy[4*maxn];void build(ll low, ll high, ll _id);
void push_up(ll _id);
void update(ll Left, ll Right, ll val, ll low, ll high, ll _id);
void push_down(ll _id);
ll query(ll Left, ll Right, ll low, ll high, ll _id);int main()
{
#ifdef __AiR_Hfreopen("in.txt", "r", stdin);
#endif // __AiR_Hscanf("%I64d", &n);for (ll i = 1; i <= n; ++i) {scanf("%I64d", &a[i]);}build(1, n, 1);scanf("%I64d", &m);while (m--) {scanf("%I64d %I64d", &w, &h);ans = query(1, w, 1, n, 1);printf("%I64d\n", ans);update(1, w, ans + h, 1, n, 1);}return 0;
}ll query(ll Left, ll Right, ll low, ll high, ll _id)
{if (low == Left && high == Right) {if (lazy[_id]) {return lazy[_id];}return tree[_id];}push_down(_id);ll mid = (low + high) >> 1;if (Right <= mid) {return query(Left, Right, lson);} else if (Left > mid) {return query(Left, Right, rson);} else {return max(query(Left, mid, lson), query(mid + 1, Right, rson));}push_up(_id);
}void update(ll Left, ll Right, ll val, ll low, ll high, ll _id)
{if (low == Left && high == Right) {tree[_id] = lazy[_id] = val;return;}push_down(_id);ll mid = (low + high) >> 1;if (Right <= mid) {update(Left, Right, val, lson);} else if (Left > mid) {update(Left, Right, val, rson);} else {update(Left, mid, val, lson), update(mid + 1, Right, val, rson);}push_up(_id);
}void push_down(ll _id)
{if (lazy[_id]) {tree[_id] = lazy[_id];tree[_id<<1] = lazy[_id], tree[_id<<1|1] = lazy[_id];lazy[_id<<1] = lazy[_id], lazy[_id<<1|1] = lazy[_id];lazy[_id] = 0;}
}void build(ll low, ll high, ll _id)
{if (low == high) {tree[_id] = a[low];return;}ll mid = (low + high) >> 1;build(lson), build(rson);push_up(_id);
}void push_up(ll _id)
{tree[_id] = max(tree[_id<<1], tree[_id<<1|1]);
}

2、Codeforces 356A Knight Tournament

解题思路:

参考:http://www.cnblogs.com/dashuzhilin/p/4535178.html

刚开始写按照题目所给次序更新(写起来好麻烦的样子...调了好久), TLE on test 37 了...QAQ

后来看题解发现只要按照相反次序更新就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 3e5 + 10;int n, m, cnt = 0;
int l[maxn], r[maxn], x[maxn];
int lazy[4*maxn];void update(int Left, int Right, int val, int low, int high, int _id);
void push_down(int _id);
void query(int low, int high, int _id);
void push_up(int _id);int main()
{
#ifdef __AiR_Hfreopen("in.txt", "r", stdin);
#endif // __AiR_Hscanf("%d %d", &n, &m);for (int i = 1; i <= m; ++i) {scanf("%d %d %d", &l[i], &r[i], &x[i]);}for (int i = m; i >= 1; --i) {if (l[i] < x[i]) {update(l[i], x[i] - 1, x[i], 1, n, 1);}if (r[i] > x[i]) {update(x[i] + 1, r[i], x[i], 1, n, 1);}}query(1, n, 1);printf("\n");return 0;
}void query(int low, int high, int _id)
{if (lazy[_id]) {for (int i = low; i <= high; ++i) {printf(++cnt == 1 ? "%d" : " %d", lazy[_id]);}return;}if (low == high) {printf(++cnt == 1 ? "0" : " 0");return;}int mid = (low + high) >> 1;query(lson), query(rson);
}void update(int Left, int Right, int val, int low, int high, int _id)
{if (low == Left && Right == high) {lazy[_id] = val;return;}push_down(_id);int mid = (low + high) >> 1;if (Right <= mid) {update(Left, Right, val, lson);} else if (Left > mid) {update(Left, Right, val, rson);} else {update(Left, mid, val, lson), update(mid + 1, Right, val, rson);}
}void push_down(int _id)
{if (lazy[_id]) {lazy[_id<<1] = lazy[_id], lazy[_id<<1|1] = lazy[_id];lazy[_id] = 0;}
}

3、ZOJ 2301 Color the Ball

离散化很优美啊!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
#define lson low, mid, _id << 1
#define rson mid + 1, high, _id << 1 | 1typedef long long ll;
typedef pair<int, int> pii;
struct Node { int x, y, z; };const int INF = 0x7fffffff;
const int maxn = 1e6;
int N;
int l[maxn], r[maxn], key[maxn];
char cmd[2];
int tree[maxn];
vector<int> v;
vector<pii> ans;int id(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
void update(int Left, int Right, int color, int low, int high, int _id);
void query(int low, int high, int _id);
int real(int x) { return v[x - 1]; }int main() {
#ifdef __AiR_Hfreopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_Hwhile (scanf("%d", &N) != EOF) {if (N <= 0) { printf("Oh, my god\n"); continue; }v.clear(); memset(tree, -1, sizeof(tree)); ans.clear();for (int i = 0; i < N; ++i) {scanf("%d %d %s", &l[i], &r[i], cmd);if (cmd[0] == 'w') { key[i] = 1; }else { key[i] = -1; }v.push_back(l[i]); v.push_back(r[i]);if (l[i] > 1) { v.push_back(l[i] - 1); } v.push_back(r[i] + 1);}sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());for (int i = 0; i < N; ++i) { update(id(l[i]), id(r[i]), key[i], 1, (int)v.size(), 1); }query(1, (int)v.size(), 1);if ((int)ans.size() == 0) { printf("Oh, my god\n"); continue; }int Max = real(ans[0].second) - real(ans[0].first), ans_l = ans[0].first, ans_r = ans[0].second;int l_t = ans[0].first, r_t = ans[0].second;for (int i = 1; i < (int)ans.size(); ++i) {if (ans[i].first - 1 == ans[i - 1].second) {r_t = ans[i].second;} else {l_t = ans[i].first; r_t = ans[i].second;}if (real(r_t) - real(l_t) > Max) { Max = real(r_t) - real(l_t); ans_l = l_t; ans_r = r_t; }}printf("%d %d\n", real(ans_l), real(ans_r));}
#ifdef __AiR_Hprintf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_Hreturn 0;
}void query(int low, int high, int _id) {if (tree[_id]) {if (tree[_id] == 1) { ans.push_back(make_pair(low, high)); }return;}int mid = low + (high - low) / 2;query(lson); query(rson);
}void update(int Left, int Right, int color, int low, int high, int _id) {if (tree[_id] == color) { return; }if (low == Left && high == Right) { tree[_id] = color; return; }int mid = low + (high - low) / 2;if (tree[_id]) { tree[_id << 1] = tree[_id]; tree[_id << 1 | 1] = tree[_id]; tree[_id] = 0; }if (Left > mid) { update(Left, Right, color, rson); }else if (Right <= mid) { update(Left, Right, color, lson); }else { update(Left, mid, color, lson); update(mid + 1, Right, color, rson); }
}

4、HDU 5306 Gorgeous Sequence

参考:

《区间最值操作与历史最值问题》杭州学军中学 吉如一

http://www.cnblogs.com/shenben/p/6641984.html

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
#define lc _id << 1
#define rc _id << 1 | 1
#define lson low, mid, lc
#define rson mid + 1, high, rctypedef long long ll;
typedef pair<int, int> pii;const int INF = 0x7fffffff;
const int maxn = 1e6 + 10;
int T, n, m, cmd, x, y, t;
int a[maxn], Max[maxn * 4], second[maxn * 4], num[maxn * 4];
ll sum[maxn * 4];inline void push_up(int _id) {sum[_id] = sum[lc] + sum[rc]; Max[_id] = max(Max[lc], Max[rc]); num[_id] = 0;if (Max[_id] == Max[lc]) { num[_id] += num[lc]; }if (Max[_id] == Max[rc]) { num[_id] += num[rc]; }second[_id] = max(second[lc], second[rc]);if (Max[lc] != Max[rc]) { second[_id] = max(second[_id], min(Max[lc], Max[rc])); }
}
inline void push_down(int _id) {if (Max[_id] < Max[lc]) { sum[lc] += (ll)(Max[_id] - Max[lc]) * num[lc]; Max[lc] = Max[_id]; }if (Max[_id] < Max[rc]) { sum[rc] += (ll)(Max[_id] - Max[rc]) * num[rc]; Max[rc] = Max[_id]; }
}
void build(int low, int high, int _id) {if (low == high) { Max[_id] = a[low]; sum[_id] = a[low]; num[_id] = 1; second[_id] = 0; return; }int mid = (low + high) / 2; build(lson); build(rson); push_up(_id);
}
int query_max(int Left, int Right, int low, int high, int _id) {if (low == Left && high == Right) { return Max[_id]; }int mid = (high + low) / 2; push_down(_id);if (Right <= mid) { return query_max(Left, Right, lson); }else if (Left >= mid + 1) { return query_max(Left, Right, rson); }else { return max(query_max(Left, mid, lson), query_max(mid + 1, Right, rson)); }
}
ll query_sum(int Left, int Right, int low, int high, int _id) {if (low == Left && high == Right) { return sum[_id]; }int mid = (high + low) / 2; push_down(_id);if (Right <= mid) { return query_sum(Left, Right, lson); }else if (Left >= mid + 1) { return query_sum(Left, Right, rson); }else { return query_sum(Left, mid, lson) + query_sum(mid + 1, Right, rson); }
}
void update(int Left, int Right, int key, int low, int high, int _id) {if (key >= Max[_id]) { return; }if (low == high) { sum[_id] += (ll)key - Max[_id]; Max[_id] = key; return; }if (Left == low && Right == high && key > second[_id]) {sum[_id] += (ll)num[_id] * (key - Max[_id]); Max[_id] = key; return;}int mid = (low + high) / 2; push_down(_id);if (Right <= mid) { update(Left, Right, key, lson); }else if (Left >= mid + 1) { update(Left, Right, key, rson); }else { update(Left, mid, key, lson); update(mid + 1, Right, key, rson); }push_up(_id);
}int main() {
#ifdef __AiR_Hfreopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_Hscanf("%d", &T);while (T--) {scanf("%d %d", &n, &m);for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); }build(1, n, 1);while (m--) {scanf("%d %d %d", &cmd, &x, &y);if (cmd == 1) { printf("%d\n", query_max(x, y, 1, n, 1)); }else if (cmd == 2) { printf("%I64d\n", query_sum(x, y, 1, n, 1)); }else {scanf("%d", &t); update(x, y, t, 1, n, 1);}}}
#ifdef __AiR_Hprintf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_Hreturn 0;
}

5、Codeforces 444C DZY Loves Colors

参考:

《小清新线段树》吉如一

http://blog.csdn.net/kalilili/article/details/46038733

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>using namespace std;#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
#define lc _id << 1
#define rc _id << 1 | 1
#define lson low, mid, lc
#define rson mid + 1, high, rctypedef long long ll;
typedef pair<int, int> pii;const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int lazy[maxn * 4];
ll add[maxn * 4], sum[maxn * 4];
ll t;
int n, m, l, r, x, type;inline void push_up(int _id) {sum[_id] = sum[lc] + sum[rc];if (lazy[lc] == lazy[rc]) { lazy[_id] = lazy[lc]; }
}
void build(int low, int high, int _id) {if (low == high) { lazy[_id] = low; return; }int mid = low + (high - low) / 2; build(lson); build(rson);
}
inline void push_down(int _id, int low, int mid, int high) {if (lazy[_id] == 0) { return; }add[lc] += add[_id]; sum[lc] += add[_id] * (mid - low + 1);add[rc] += add[_id]; sum[rc] += add[_id] * (high - mid);add[_id] = 0;lazy[lc] = lazy[_id]; lazy[rc] = lazy[_id]; lazy[_id] = 0;
}
void update(int Left, int Right, int key, int low, int high, int _id) {if (low == high) { sum[_id] += abs(lazy[_id] - key); lazy[_id] = key; return; }if (Left == low && high == Right && lazy[_id]) {t = abs(lazy[_id] - key); add[_id] += t; lazy[_id] = key;sum[_id] += t * (high - low + 1); return;}int mid = low + (high - low) / 2; push_down(_id, low, mid, high);if (Right <= mid) { update(Left, Right, key, lson); }else if (Left >= mid + 1) { update(Left, Right, key, rson); }else { update(Left, mid, key, lson); update(mid + 1, Right, key, rson); }push_up(_id);
}
ll query(int Left, int Right, int low, int high, int _id) {if (Left == low && Right == high) { return sum[_id]; }int mid = low + (high - low) / 2; push_down(_id, low, mid, high);if (Right <= mid) { return query(Left, Right, lson); }else if (Left >= mid + 1) { return query(Left, Right, rson); }else { return query(Left, mid, lson) + query(mid + 1, Right, rson); }
}int main() {
#ifdef __AiR_Hfreopen("in.txt", "r", stdin);
//    freopen("out2.txt", "w", stdout);
#endif // __AiR_Hscanf("%d %d", &n, &m);build(1, n, 1);while (m--) {scanf("%d %d %d", &type, &l, &r);if (type == 1) {scanf("%d", &x); update(l, r, x, 1, n, 1);} else {printf("%I64d\n", query(l, r, 1, n, 1));}}
#ifdef __AiR_Hprintf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_Hreturn 0;
}

线段树 2017.4.20相关推荐

  1. HDU 6194 String String String (后缀数组+线段树, 2017 ACM/ICPC Asia Regional Shenyang Online)

    Problem 求字符串 S 中严格出现 k 次的子串个数 k≥1k\ge 1 |S|≤105|S|\le 10^5 ∑|S|≤2×106\sum |S| \le 2\times 10^6 Idea ...

  2. HDU 1166 敌兵布阵(线段树:点更新,区间求和)

    HDU 1166 敌兵布阵(线段树:点更新,区间求和) http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意: 给你n个整数,然后给你多条命令,每条命令如 ...

  3. 洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]

    P2826 [USACO08NOV]光开关Light Switching 题目描述 Farmer John tries to keep the cows sharp by letting them p ...

  4. 数据结构二之线段树Ⅱ——KiKi‘s K-Number,ball,The Child and Sequence,「雅礼集训 2017 Day1」市场,Atlantis

    值域线段树+势能线段树+扫描线 KiKi's K-Number ball The Child and Sequence 「雅礼集训 2017 Day1」市场 Atlantis KiKi's K-Num ...

  5. 2017西安交大ACM小学期数据结构 [线段树]

    Problem B+ 发布时间: 2017年7月1日 02:08   最后更新: 2017年7月1日 02:10   时间限制: 1000ms   内存限制: 64M 描述 给定一个长度为n的序列a1 ...

  6. 洛谷P2073 送花 [2017年6月计划 线段树01]

    P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...

  7. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面: Consider an array AA with n elements . Each of its element ...

  8. J - [永不止步-2017]_区间第K大-线段树维护

    J - [永不止步-2017]_区间第K大 把线段树的结点的数据域设置为vector类型即可别的操作为区间更新模板 思路就是这样runtime error暂时没改对 #include<bits/ ...

  9. 20.CF817F MEX Queries 线段树(Lazy标记练习)

    20.CF817F MEX Queries 离散化+区间覆盖+区间反转线段树 个人Limitの线段树题单题解主目录:Limitの线段树题单 题解目录_HeartFireY的博客-CSDN博客 要求维护 ...

  10. 2017年ICPC西安邀请赛A、XOR(线段树套线性基 + 思维)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 题目传送门 Problem 给你 nnn 和 nnn 个整数的数组 aaa,以及kkk和qqq,有 q ...

最新文章

  1. [JavaScript]为JS处理二进制数据提供可能性的WEB API
  2. C#的常见算法(面试)(转)
  3. php-fpm 负荷高,记录简单处理服务器php-fpm占用过多的问题(主题影响负载)
  4. 动态规划:斐波那契数列里面的东西?
  5. 本地消息表实现机制讲解
  6. ListableBeanFactory接口
  7. 使用php,使用 PHP
  8. ajax失败的原因,使用https协议失败的ajax请求失败的可能原因但http工作
  9. spring jdbcTemplate 插入对象返回主键值
  10. 面试开发可以用python_Python开发工程师面试题(五)
  11. python 朋友圈leetcode_利特代码0547。朋友圈[python],LeetCode0547FriendCircles,Python
  12. android连接指定的wifi
  13. Si9000射频线阻抗计算
  14. 两个队列实现一个栈(Java)
  15. 一文读懂什么是MRO采购
  16. MATLAB App Designer 计算器的设计
  17. 固态硬盘为什么比机械硬盘快
  18. weui组件库的下载及使用
  19. 微信小程序获取rich-text(富文本)渲染内容高度,rich-text(富文本)里img 样式设置
  20. 中软国际首届嘉年华晚会 创新网络年会PK“春晚”

热门文章

  1. 【大宝的犀牛】飞利浦RQ370剃须刀建模教程
  2. HALCON 18.11 Progress 发布说明
  3. 周公解梦|做梦的解释|鬼压床|为什么会做梦
  4. ibm3650m2服务器java_通过IBM 3650 M2服务器的ServerGuide工具配置RAID图文教程
  5. 串口服务器与串口协议转换器,串口服务器和串口转以太网模块有什么不同(示例代码)...
  6. FileUpload1.PostedFile.FileName取不到完整路径
  7. perl 教程网站 记录
  8. 想回味Windows95?模拟器+浏览器搞定
  9. 网吧服务器维护工具,某某网吧专用维护工具(网吧维护管理助手)V5.1 最新版
  10. 基于自适应惯性权重的樽海鞘群算法