hdu 4027 Can you answer these queries? 线段树 懒惰标记 单点更新妙用
来源:互联网 发布:nfc世界网络银行商城 编辑:程序博客网 时间:2024/05/20 18:52
题意:把一组舰队看成是线段上的端点,有一种秘密武器,每次可以攻击一个区间上的船,然后他们的防御力x减低为sqrt(x).
做法:最多只能有7次攻击有效。所以用带点更新吧,一遇到区间防御力总和为len(区间中含有舰船的个数),就停止更新,因为这个区间所有的船的防御力已经只剩1了,这么一来,最后的时间复杂度不会太高
#include <cstdio>#include <cstring>#include <cmath>#define max(a, b) ((a) > (b) ? (a) : (b))#define left l, m, x << 1#define right m + 1, r, x << 1 | 1//有效攻最多七下。。。typedef __int64 LL;const int LMT = 100003;LL sum[LMT << 2];struct __node{ int l, r, len;}node[LMT << 2];inline LL get(void){ LL res = 0; char ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); while(ch >= '0' && ch <= '9') { res = res * 10 + ch - '0'; ch = getchar(); } return res;}void build(int l, int r, int x){ node[x].l = l; node[x].r = r; node[x].len = r - l + 1; if(l == r) { sum[x] = get(); return ; } int m = (l + r) >> 1; build(left); build(right); sum[x] = sum[x << 1] + sum[x << 1 | 1];}LL query(int L, int R, int x){ if (L <= node[x].l && node[x].r <= R) return sum[x]; int m = (node[x].l + node[x].r) >> 1; LL res = 0; if (L <= m) res += query(L, R, x << 1); if (R > m) res += query(L, R, x << 1 | 1); return res;}void update(int L, int R, int x){ if(sum[x] == node[x].len) return; if(node[x].l == node[x].r) { sum[x] = (LL)sqrt(1.0 * sum[x]); return; } int m = (node[x].l + node[x].r) >> 1; if (L <= m) update(L, R, x << 1); if (R > m) update(L, R, x << 1 | 1); sum[x] = sum[x << 1] + sum[x << 1 | 1];}int main(void){ int n, q, ord, l, r, I = 1; while(~scanf("%d", &n)) { build(1, n, 1); scanf("%d", &q); printf("Case #%d:\n", I++); while(q--) { scanf("%d%d%d", &ord, &l, &r); if(l > r) { l = l ^ r; r = l ^ r; l = l ^ r; } if(ord) printf("%I64d\n", query(l, r, 1)); else update(l, r, 1); } printf("\n"); } return 0;}
- hdu 4027 Can you answer these queries? 线段树 懒惰标记 单点更新妙用
- hdu 4027 Can you answer these queries?(线段树)(单点更新 但需要标记记录剪枝)
- HDU 4027 Can you answer these queries?(线段树单点更新)
- HDU 4027 Can you answer these queries?(线段树单点更新+询问+神坑)
- HDU4027:Can you answer these queries?(线段树单点更新)
- Can you answer these queries?(线段树之单点更新)
- Can you answer these queries?(单点更新+标记)
- HDU 4027 Can you answer these queries? 【线段树】
- HDU 4027 Can you answer these queries 线段树
- hdu 4027(线段树)Can you answer these queries?
- HDU 4027 线段树 Can you answer these queries?
- hdu 4027 线段树 Can you answer these queries?
- HDU 4027 Can you answer these queries?(线段树)
- hdu 4027 Can you answer these queries? 线段树
- hdu 4027 Can you answer these queries? 线段树
- hdu-4027-Can you answer these queries?-线段树
- hdu 4027 Can you answer these queries? 线段树
- HDU 4027 Can you answer these queries? (线段树)
- 黑马程序员----JAVA基础之 BeanUtils
- ios 加速传感器UIAccelerometer
- 软件设计模式系列之二 FACTORY METHOD(工厂方法)
- JQuery操作Dom
- 欧拉函数
- hdu 4027 Can you answer these queries? 线段树 懒惰标记 单点更新妙用
- 从微信谈起,如何优化互联网APP心跳机制
- 决策树(decisions tree)和ID3算法
- uva 11461(水题)
- WINCE音量设置(系统音按键音)
- JQuery基本过滤选择器与表单对象过滤器
- 怎么在vs2005中加入wince5 模拟器
- AsyncQueryHandler学习
- 空(标识)接口serializable