HDU4893线段树单点跟新+二分+laz标记
原题http://acm.hdu.edu.cn/showproblem.php?pid=4893
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1932 Accepted Submission(s): 591
After some research, Doge found that the box is maintaining a sequence an of n numbers internally, initially all numbers are zero, and there are THREE "operations":
1.Add d to the k-th number of the sequence.
2.Query the sum of ai where l ≤ i ≤ r.
3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.
4.Play sound "Chee-rio!", a bit useless.
Let F 0 = 1,F 1 = 1,Fibonacci number Fn is defined as F n = F n - 1 + F n - 2 for n ≥ 2.
Nearest Fibonacci number of number x means the smallest Fn where |F n - x| is also smallest.
Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:
1 k d - "add"
2 l r - "query sum"
3 l r - "change to nearest Fibonacci"
1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, |d| < 2 31, all queries will be valid.
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <vector>
#include <map>
using namespace std;
#define int64 __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 200000;
struct node{int64 l;int64 r;int64 sum;bool laz;//int64 mid(){// return (l+r)/2;//}
};
struct node tree[maxn<<2];
set<int64> ss;
int64 f[10000];void PushUp(int64 rt){if(tree[rt<<1].laz==1 && tree[rt<<1|1].laz==1){//如果两个子节点都不需要更新,那么父节点也不需要跟新tree[rt].laz = 1;}else{tree[rt].laz = 0;}
}
void getf(){int i;f[0] = 1;f[1] = 1;ss.insert(f[0]);ss.insert(f[1]);for(i=2;i<=73;i++){f[i] = f[i-1]+f[i-2];ss.insert(f[i]);}
}void build(int64 l,int64 r,int64 rt){tree[rt].l = l;tree[rt].r = r;tree[rt].laz = 0;if(tree[rt].l == tree[rt].r){tree[rt].sum = 0;return ;}int64 m = (tree[rt].l+tree[rt].r)/2;build(lson);build(rson);tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
}void update(int64 a,int64 b,int64 rt){if(tree[rt].l == tree[rt].r){tree[rt].sum+=b;tree[rt].laz = 0;return ;}int64 m = (tree[rt].l+tree[rt].r)/2;if(a <= m){update(a,b,rt<<1);}else{update(a,b,rt<<1|1);}tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;PushUp(rt);
}void update2(int64 L,int64 R,int64 rt){if(tree[rt].laz == 1){return ;}if(tree[rt].l == tree[rt].r){tree[rt].laz = 1;set<int64>::iterator it1,it2;int64 l,r;it2=it1=ss.lower_bound(tree[rt].sum);//获得》=tree[rt].sum这个数在斐波那契数列中的地址l = *it2;if(it1 != ss.begin()){it1--;}r = *it1;tree[rt].sum = (tree[rt].sum-*it1)>((*it2)-tree[rt].sum)?l:r;return ;}int64 m = (tree[rt].l + tree[rt].r)/2;if(L<=m){update2(L,R,rt<<1);}if(R > m){update2(L,R,rt<<1|1);}tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;PushUp(rt);
}int64 query(int64 L,int64 R,int64 rt){if(L<=tree[rt].l && tree[rt].r<=R){return tree[rt].sum;}int64 m = (tree[rt].l+tree[rt].r)/2;int64 ret = 0;if(L <= m){ret+=query(L,R,rt<<1);}if(R > m){ret+=query(L,R,rt<<1|1);}//else//{// ret+=query(L,R,rt<<1);// ret+=query(L,R,rt<<1|1);//}return ret;
}int main(){int64 n,m;int64 op,a,b;ss.clear();getf();while(~scanf("%I64d%I64d",&n,&m)){build(1,n,1);while(m--){scanf("%I64d",&op);if(op == 2){scanf("%I64d%I64d",&a,&b);printf("%I64d\n",query(a,b,1));}else if(op == 1){scanf("%I64d%I64d",&a,&b);update(a,b,1);}else if(op == 3){scanf("%I64d%I64d",&a,&b);update2(a,b,1);}}}return 0;
}
HDU4893线段树单点跟新+二分+laz标记相关推荐
- HDUOJ----1166敌兵布阵(线段树单点更新)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU - 1166敌兵布阵+HDU-1754 I Hate It (线段树单点更新——累加/最大值)
线段树单点更新,模板题 HDU1166 敌兵布阵 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- 【原创】tyvj1038 忠诚 计蒜客 管家的忠诚 线段树(单点更新,区间查询)...
[原创]tyvj1038 忠诚 & 计蒜客 管家的忠诚 & 线段树(单点更新,区间查询) 最简单的线段树之一,中文题目,不翻译.... 注释讲的比较少,这已经是最简单的线段树,如果看不 ...
- FZU 2297 Number theory【线段树/单点更新/思维】
Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide. Input First l ...
- CDOJ 1073 线段树 单点更新+区间查询 水题
H - 秋实大哥与线段树 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit S ...
- CodeforcesBeta Round #19 D. Points 离线线段树 单点更新 离散化
题目链接: http://codeforces.com/contest/19/problem/D 题意: 有三种操作"add x y"往平面上添加(x,y)这个点,"re ...
- 线段树——单点更新(二)
HDU 4217 Data Structure? http://acm.hdu.edu.cn/showproblem.php?pid=4217 CZ做的一道题目,我帮忙看了看. 题意:给定N个数(1- ...
- Ocean的礼物(线段树单点修改)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s Memory ...
- P3834 【模板】可持久化线段树 2(整体二分做法)
P3834 [模板]可持久化线段树 2(主席树) 我们详细讲讲这个整体二分如何求区间第k小 我们都知道二分可以求出区间里某个想要的值,如果有很多询问,我们对每个询问都进行二分,复杂度就是O(QNlog ...
最新文章
- iOS 9 通用链接(Universal Links)
- @CreatedDate@CreatedBy@LastModifiedBy@LastModifiedDate
- java中类与类之间的关系
- Java 中使用反射来创建对象、调用方法
- 豪投10亿!华为放话:3年培养100万AI人才!网友神回应了
- 【Python】Pandas高效加载JSON文件
- CSDN怎么换行?添加空行
- 该内存不能为read
- 在Ubuntu 20.04上安装MySQL教程
- 微信小程之打卡小程序开发
- 信息系统开发与管理第一遍总结
- JEECG3.8 全套实战视频全部开放,免费下载!
- rk3328 android10 Debug串口打印信息
- 酷睿i7 9750h相当于什么水平 i79750h属于哪个档次
- 计算机在职英语,我是社会在职人员,能考什么样的英语?
- AIR - 网页系统回到桌面应用
- Zigbee 设置信道,PANID,发射功率现对z-stack里几个网络参数的设置以及如何获取总结一下。
- 用Twig启动WordPress开发:入门
- 软件测试的完整案例分析,软件测试案例分析完整版
- Web应用多账号系统设计及微信扫码登录实现