博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4893 线段树裸题
阅读量:4620 次
发布时间:2019-06-09

本文共 3124 字,大约阅读时间需要 10 分钟。

Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2512    Accepted Submission(s): 751
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 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.
 
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| < 2
31, 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

题意不说了,非常easy的线段树题目了。就是打标记改点求段,改动段时因为极限次数不多。直接暴力更新到点,

好久没写线段树了,错了好多次,写的好傻逼。

代码:

/* ***********************************************Author :rabbitCreated Time :2014/8/4 14:58:15File Name :11.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;ll fib[100];struct node{ ll l,r; ll sum,flag;}a[800300];ll m,n;void pushup(ll t){ if(a[t].l==a[t].r)return; a[t].sum=a[2*t].sum+a[2*t+1].sum; a[t].flag=a[2*t].flag&a[2*t+1].flag;}void build(ll t,ll l,ll r){// cout<<"hhh "<
<<" "<
<
>1; if(fib[mid] > x) id = mid, r = mid-1; else l = mid+1; } if(x-fib[id-1] <= fib[id]-x) return fib[id-1]; return fib[id];}void update2(ll t,ll l,ll r){ if(a[t].flag)return; if(a[t].l==a[t].r){ a[t].sum=Find(a[t].sum); a[t].flag=1; //cout<<"ddd "<
<<" "<
<<" "<
<
mid)update2(2*t+1,l,r); pushup(t);}ll getsum(ll t,ll l,ll r){ if(a[t].l>=l&&a[t].r<=r)return a[t].sum; ll mid=(a[t].l+a[t].r)/2; ll ans=0; if(l<=mid)ans+=getsum(2*t,l,r); if(r> mid) ans+=getsum(2*t+1,l,r); return ans;}int main(){ // freopen("data.in","r",stdin); // freopen("data.out","w",stdout); fib[0]=1;fib[1]=1; for(ll i=2;i<=90;i++)fib[i]=fib[i-1]+fib[i-2]; while(~scanf("%I64d%I64d",&n,&m)){ // cout<<"ddd "<

转载于:https://www.cnblogs.com/cxchanpin/p/7083822.html

你可能感兴趣的文章
转 Merkle Tree(默克尔树)算法解析
查看>>
网络编程基础之socket编程
查看>>
各种浏览器的user-agent和
查看>>
Restful levels
查看>>
Phonegap移动开发:布局总结(一) 全局
查看>>
Java 变参函数的实现
查看>>
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>
angularjs实现首页轮播图
查看>>
Git 对象 和checkout 和stash的笔记
查看>>
团队项目总结2-服务器通信模型和顺序图
查看>>
hdu 1085 Holding Bin-Laden Captive!
查看>>
[周记]8.7~8.16
查看>>
递归定义
查看>>
kindeditor 代码高亮设置
查看>>
图的邻接表存储
查看>>