线段树 + 区间更新: HDU 4893 Wow! Such Sequence!
Wow! Such Sequence!#
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2234 Accepted Submission(s): 657
Problem Description
Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox.
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 F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.
Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.
Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.
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 F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.
Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.
Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.
Input
Input contains several test cases, please process till EOF.
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| < 231, all queries will be valid.
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| < 231, all queries will be valid.
Output
For each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query.
Sample Input
1 1
2 1 1
5 4
1 1 7
1 3 17
3 2 4
2 1 5
Sample Output
0
22
Author
Fudan University
Source
【题目分析】
这题相比于裸的线段树区间更新有了一些难度。
我们在每个结点中设一个fib,表示离sum最近的fibnacci数,每次区间更新时,就将sum的值更新为fib。fib的值只有在单点更新的过程中才会改变,也就是说当sum值改变的时候fib才改变,因为当sum变为fib后,离sum最近的fibnacci数还是fib值。
lazy----记录该点以下的孩子结点是否需要更新。
我用long long ,然后用了%lld输入,不知道杭电不支持%lld,debug了半天,TLE了20多次,后来改%lld为%I64d就过了,哭死QAQ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | //Memory Time // K MS #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<iomanip> #include<string> #include<climits> #include<cmath> #define MAX 100010 #define LL long long using namespace std; LL n,m; LL ans; LL f[60]; struct Tree { LL l,r; LL sum,fib; bool lazy; }; Tree tree[MAX*3]; LL Find(LL x) { if (x<=0) return 1; LL ldis,rdis; for ( int i=0;i<59;++i) { if (f[i]<=x&&f[i+1]>=x) { ldis=x-f[i]; rdis=f[i+1]-x; return ldis<=rdis?f[i]:f[i+1]; } } } void pushup(LL x) { LL tmp=x<<1; tree[x].sum=tree[tmp].sum+tree[tmp+1].sum; tree[x].fib=tree[tmp].fib+tree[tmp+1].fib; } void pushdown(LL x) { if (!tree[x].lazy) return ; tree[x].lazy=0; if (tree[x].l==tree[x].r) return ; LL tmp=x<<1; tree[tmp].lazy=tree[tmp+1].lazy=1; tree[tmp].sum=tree[tmp].fib; tree[tmp+1].sum=tree[tmp+1].fib; } void build(LL l,LL r,LL x) { tree[x].l=l,tree[x].r=r; tree[x].sum=0,tree[x].fib=1,tree[x].lazy=0; if (l==r) return ; LL tmp=x<<1; LL mid=(l+r)>>1; build(l,mid,tmp); build(mid+1,r,tmp+1); pushup(x); } void add(LL x,LL k,LL num) { if (tree[x].l==tree[x].r) { tree[x].sum+=num; tree[x].fib=Find(tree[x].sum); return ; } if (tree[x].lazy) pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if (k<=mid) add(tmp,k,num); else if (k>mid) add(tmp+1,k,num); pushup(x); } void change(LL l,LL r,LL x) { if (r<tree[x].l||l>tree[x].r) return ; if (l<=tree[x].l&&r>=tree[x].r) { tree[x].sum=tree[x].fib; tree[x].lazy=1; return ; } if (tree[x].lazy)pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if (r<=mid) change(l,r,tmp); else if (l>mid) change(l,r,tmp+1); else { change(l,mid,tmp); change(mid+1,r,tmp+1); } pushup(x); } void query(LL l,LL r,LL x) { if (r<tree[x].l||l>tree[x].r) return ; if (l<=tree[x].l&&r>=tree[x].r) { ans+=tree[x].sum; return ; } if (tree[x].lazy) pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if (r<=mid) query(l,r,tmp); else if (l>mid) query(l,r,tmp+1); else { query(l,mid,tmp); query(mid+1,r,tmp+1); } pushup(x); } int main() { // freopen("cin.txt","r",stdin); // freopen("cout.txt","w",stdout); f[0]=f[1]=1; for ( int i=2;i<60;++i) f[i]=f[i-1]+f[i-2]; while ( scanf ( "%I64d %I64d" ,&n,&m)!=EOF) { build(1,n,1); LL a,b,c; while (m--) { scanf ( "%I64d %I64d %I64d" ,&a,&b,&c); if (a==1) add(1,b,c); else if (a==2) { ans=0; query(b,c,1); printf ( "%I64d\n" ,ans); } else change(b,c,1); } } return 0; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3884474.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
ACM/数据结构
标签:
线段树 + 区间更新
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?