您的位置:首页 > 产品设计 > UI/UE

hdu 4893 Wow! Such Sequence! 水线段树。

2014-07-30 23:03 399 查看


Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2019 Accepted Submission(s): 607



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.

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.

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

2014 Multi-University Training Contest
3

初始每个值都是0,然后有3种操作,第一种是给一个数加上一个数,第二中操作是求l到r的和,第三种操作是把l-r的数变成最近的斐波那契数。

用线段树维护三个值,lazy来区分当前区间段要求的是斐波那契数列的和还是原始数列的和,sf,表示如果当前区间是斐波那契的sum,sy表示当前区间是原始数的sum.

斐波那契数在 long long 范围下只有93个,可推断数的范围在long long 内,因为初始值都为0.

知道这些,剩下的就是比较裸的线段树了。

/***********************************************\
|Author: YMC
|Created Time: 2014-7-29 15:21:06
|File Name: 1007.cpp
|Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
#define maxn 100005
struct Tree{
int l,r;
ll sf,sy;
int lazy;
}tree[maxn<<2];
ll fb[100] ;
ll cal(ll x){
if(x <= 1) return 1;
for(int i=0;i<93;++i){
if(fb[i] > x){
if(i >= 1){
//return fb[i-1];
if(x-fb[i-1]<=fb[i]-x) return fb[i-1];
else return fb[i];
}
else return 1;
}
}
return fb[92];
}
void pushup(int rt){
tree[rt].sf = tree[L(rt)].sf + tree[R(rt)].sf;
tree[rt].sy = tree[L(rt)].sy + tree[R(rt)].sy;
if(tree[rt].l == tree[rt].r) return ;
else {
if(tree[L(rt)].lazy == tree[R(rt)].lazy){
tree[rt].lazy = tree[L(rt)].lazy;
} else {
tree[rt].lazy == -1;
}
}
}
void build(int l,int r,int rt){
tree[rt].l = l;tree[rt].r = r;
tree[rt].lazy = 1;
if(l == r){
tree[rt].sf = 1;
tree[rt].sy = 0;
return ;
}
int mid = (l+r)>>1;
build(l,mid,L(rt));
build(mid+1,r,R(rt));
pushup(rt);
}
void add(int a,ll b,int rt){
int l = tree[rt].l,r = tree[rt].r;
if(l == r){
if(tree[rt].lazy == 1){
tree[rt].sy += b;
tree[rt].sf = cal(tree[rt].sy);
} else {
tree[rt].sy = tree[rt].sf + b;
tree[rt].sf = cal(tree[rt].sy);
tree[rt].lazy = 1;
}
return ;
}
if(tree[rt].lazy != -1) {
tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
tree[rt].lazy = -1;
}
int mid = (tree[rt].l+tree[rt].r)>>1;
if(a <= mid) add(a,b,L(rt));
else add(a,b,R(rt));
pushup(rt);
}
void update(int x,int y,int rt){
int l = tree[rt].l,r = tree[rt].r;
if(l >= x && y >= r){
tree[rt].lazy = 2;
return ;
}
if(tree[rt].lazy != -1){
tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
tree[rt].lazy = -1;
}
int mid = (l+r)>>1;
if(y <= mid) update(x,y,L(rt));
else if(x >= mid+1) update(x,y,R(rt));
else {
update(x,mid,L(rt));
update(mid+1,y,R(rt));
}
pushup(rt);
}
ll query(int x,int y,int rt){
ll ans = 0;
int l = tree[rt].l,r = tree[rt].r;
if(l >= x && y >= r){
if(tree[rt].lazy == 1) return tree[rt].sy;
if(tree[rt].lazy == 2) return tree[rt].sf;
}
if(tree[rt].lazy!=-1){
tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
tree[rt].lazy = -1;
}
int mid = (l + r )>>1;
if(mid >= y) ans += query(x,y,L(rt));
else if(mid + 1 <= x) ans += query(x,y,R(rt));
else {
ans += query(x,mid,L(rt));
ans += query(mid+1,y,R(rt));
}
return ans;
}
void pre(){
fb[1] = 1;fb[2] = 1;
for(int i=3;i<93;++i){
fb[i] = fb[i-1] + fb[i-2];
}
}
int main() {
//freopen("input.txt","r",stdin);
pre();
int n,m;
int a,b;
ll c;
while(~scanf("%d%d",&n,&m)){
build(1,n,1);
while(m--){
scanf("%d%d%lld",&a,&b,&c);
if(a == 1){
add(b,c,1);
} else if (a == 2){
printf("%I64d\n",query(b,c,1));
} else {
update(b,c,1);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: