Codeforces Round #FF (Div. 1) C. DZY Loves Fibonacci Numbers

2024-08-24 12:08

本文主要是介绍Codeforces Round #FF (Div. 1) C. DZY Loves Fibonacci Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 用线段树维护

记录三个值,一个是区间和,一个是该区间 1 , 2 位置的斐波那契数列前两项大小(因为两个斐波那契数列相加得到的还是一个斐波那契数列,且该数列只与两个首项和项数有关,另外斐波那契数列前N 项和  与  该斐波那契数列第N+2减去第二项  的值 相同)

该线段树的pushdown有一些改变,直接利用已有的update函数将懒标记下传,下传时改变的区间是传入pushdown的区间大小(虽然不太明白这个姿势有何好处,但是觉得特别方便)


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62using namespace std;typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;template<class T>
inline bool read(T &n)
{T x = 0, tmp = 1; char c = getchar();while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();if(c == EOF) return false;if(c == '-') c = getchar(), tmp = -1;while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();n = x*tmp;return true;
}
template <class T>
inline void write(T n)
{if(n < 0){putchar('-');n = -n;}int len = 0,data[20];while(n){data[len++] = n%10;n /= 10;}if(!len) data[len++] = 0;while(len--) putchar(data[len]+48);
}
//-----------------------------------
#define MID(x,y) (x+((y-x)>>1))const int MOD=1000000009;
const int MAXN=300010;struct Node
{int l,r;ll v,x,y;
}t[MAXN<<2];int n,m,q,l,r;
ll f[MAXN];inline ll fib(ll a,ll b,int n)
{if(n==0)return a;if(n==1)return b;return (a*f[n-1]%MOD+b*f[n]%MOD)%MOD;
}void update(int id,int l,int r,ll a,ll b);
void build(int id,int l,int r)
{t[id].l=l,t[id].r=r;t[id].v=t[id].x=t[id].y=0;if(l==r){read(t[id].v);return;}build(id<<1,l,MID(l,r));build(id<<1|1,MID(l,r)+1,r);t[id].v=(t[id<<1].v+t[id<<1|1].v)%MOD;
}void pushdown(int id)
{if (t[id].x||t[id].y){update(id<<1,t[id].l,t[id].r,t[id].x,t[id].y);update(id<<1|1,t[id].l,t[id].r,t[id].x,t[id].y);t[id].x=0;t[id].y=0;}
}void update(int id,int l,int r,ll a,ll b)
{
//    cout<<id<<"->("<<t[id].l<<","<<t[id].r<<") -> "<<MID(t[id].l,t[id].r)<<"-> (" <<l<<","<<r<<")"<<endl;if(l<=t[id].l && t[id].r<=r){ll na,nb;na=fib(a,b,t[id].l-l);nb=fib(a,b,t[id].l-l+1);a=na;b=nb;t[id].x=(t[id].x+a)%MOD;t[id].y=(t[id].y+b)%MOD;t[id].v=(t[id].v+fib(a,b,t[id].r-t[id].l+2)-b)%MOD;return;}pushdown(id);int mid=MID(t[id].l,t[id].r);if(l<=mid)update(id<<1,l,r,a,b);if(r>mid)update(id<<1|1,l,r,a,b);t[id].v=(t[id<<1].v+t[id<<1|1].v)%MOD;
}ll query(int id,int l,int r)
{
//    cout<<id<<"->("<<t[id].l<<","<<t[id].r<<") -> "<<MID(t[id].l,t[id].r)<<"-> (" <<l<<","<<r<<")"<<endl;if(l<=t[id].l && t[id].r<=r){return t[id].v;}pushdown(id);ll ret=0;int mid=MID(t[id].l,t[id].r);if(l<=mid)ret+=query(id<<1,l,r);if(r>mid)ret+=query(id<<1|1,l,r);return ret%MOD;
}int main()
{read(n),read(m);f[1]=f[2]=1;for(int i=3;i<MAXN;i++)f[i]=(f[i-1]+f[i-2])%MOD;build(1,1,n);while(m--){read(q),read(l),read(r);if(q==1){update(1,l,r,1LL,1LL);}else{ll ans=(query(1,l,r)+MOD)%MOD;write(ans),putchar('\n');}}return 0;
}


这篇关于Codeforces Round #FF (Div. 1) C. DZY Loves Fibonacci Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1102434

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void