Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4823 Accepted Submission(s): 1408
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.
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.
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mms(x) memset(x, 0, sizeof x)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define MAX 100005
ll tree[MAX << 2], f[90], fib[MAX << 2];
bool lazy[MAX << 2];
void Fibonacci()
{
f[0] = f[1] = 1;
for(int i = 2; i < 90; i++)
f[i] = f[i - 2] + f[i - 1];
}
ll Changefib(ll x)
{
int a = lower_bound(f, f + 90, x) - f;
if(x && f[a] - x >= x - f[a - 1])
return f[a - 1];
return f[a];
}
void PushUp(int rt)
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
fib[rt] = fib[rt << 1] + fib[rt << 1 | 1];
}
void PushDown(int rt)
{
if(lazy[rt])
{
lazy[rt << 1] = lazy[rt << 1 | 1] = 1;
lazy[rt] = 0;
tree[rt << 1] = fib[rt << 1];
tree[rt << 1 | 1] = fib[rt << 1 | 1];
}
}
void BuildTree(int rt, int l, int r)
{
lazy[rt] = 0;
if(l == r)
{
tree[rt] = 0;
fib[rt] = 1;
return;
}
int mid = (l + r) >> 1;
BuildTree(lson);
BuildTree(rson);
PushUp(rt);
}
void Update_add(int rt, int l, int r, int i, int val)
{
if(l == r)
{
tree[rt] += val;
fib[rt] = Changefib(tree[rt]);
return;
}
PushDown(rt);
int mid = (l + r) >> 1;
if(i <= mid)
Update_add(lson, i, val);
if(i > mid)
Update_add(rson, i, val);
PushUp(rt);
}
void Update_fib(int rt, int l, int r, int x, int y)
{
if(x <= l && r <= y)
{
lazy[rt] = 1;
tree[rt] = fib[rt];
return;
}
PushDown(rt);
int mid = (l + r) >> 1;
if(x <= mid)
Update_fib(lson, x, y);
if(mid < y)
Update_fib(rson, x, y);
PushUp(rt);
}
ll Query(int rt, int l, int r, int x, int y)
{
if(x <= l && r <= y)
return tree[rt];
PushDown(rt);
int mid = (l + r) >> 1;
ll ans = 0;
if(x <= mid)
ans += Query(lson, x, y);
if(mid < y)
ans += Query(rson, x, y);
return ans;
}
void init(int n)
{
Fibonacci();
mms(tree);
mms(lazy);
mms(fib);
BuildTree(1, 1, n);
}
int main()
{
ios::sync_with_stdio(false);
int n, m, s, x, y;
while(cin >> n >> m)
{
init(n);
while(m--)
{
cin >> s;
if(s == 1)
{
cin >> x >> y;
Update_add(1, 1, n, x, y);
}
if(s == 2)
{
cin >> x >> y;
cout << Query(1, 1, n, x, y) << endl;
}
if(s == 3)
{
cin >> x >> y;
Update_fib(1, 1, n, x, y);
}
}
}
return 0;
}