HDU 4893 Wow! Such Sequence! 解题报告(线段树)
2014-07-29 22:07
288 查看
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 850 Accepted Submission(s): 251
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
解题报告:裸裸的线段树,不过很久没接触线段树了,略生疏。代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define bit(n) (1LL<<(n))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
#define lson l, m, pos<<1
#define rson m+1, r, pos<<1|1
const int maxn = 111111;
LL sum[maxn<<2];
LL fsum[maxn<<2];
bool fib[maxn<<2];
LL fibonacci[100];
int tot = 80;
void updateFather(int pos)
{
sum[pos] = (fib[pos<<1]?fsum[pos<<1]:sum[pos<<1]) + (fib[pos<<1|1]?fsum[pos<<1|1]:sum[pos<<1|1]);
fsum[pos] = fsum[pos<<1] + fsum[pos<<1|1];
}
void build(int l, int r, int pos)
{
fib[pos] = false;
if(l == r)
{
sum[pos] = 0;
fsum[pos] = 1;
fib[pos] = false;
return;
}
int m = (l+r)/2;
build(lson);
build(rson);
updateFather(pos);
}
void updateSon(int pos)
{
if(fib[pos])
{
fib[pos<<1] = fib[pos<<1|1] = true;
fib[pos] = false;
}
}
void updatePoint(int p, int v, int l, int r, int pos)
{
if(l == r)
{
if(fib[pos]) sum[pos] = fsum[pos];
sum[pos] += v;
fib[pos] = false;
int pp = lower_bound(fibonacci+1, fibonacci+tot, sum[pos]) - fibonacci;
if(sum[pos]-fibonacci[pp-1] <= fibonacci[pp]-sum[pos])
pp--;
fsum[pos] = fibonacci[pp];
return;
}
updateSon(pos);
int m = (l+r)/2;
if(p <= m)
updatePoint(p, v, lson);
else
updatePoint(p, v, rson);
updateFather(pos);
}
void updateLine(int L, int R, int l, int r, int pos)
{
if(fib[pos]) return;
if(L<=l && r<=R)
{
fib[pos] = true;
return;
}
int m = (l+r)/2;
if(L <= m)
updateLine(L, R, lson);
if(m < R)
updateLine(L, R, rson);
updateFather(pos);
}
LL query(int L, int R, int l, int r, int pos)
{
if(L<=l && r<=R)
return fib[pos]?fsum[pos]:sum[pos];
updateSon(pos);
updateFather(pos);
int m = (l+r)/2;
return (L<=m ? query(L, R, lson) : 0) + (m<R ? query(L, R, rson) : 0);
}
void init()
{
fibonacci[0] = fibonacci[1] = 1;
fff(i, 2, tot)
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
void work()
{
init();
int n, m;
while(scanf("%d%d", &n, &m) == 2)
{
build(1, n, 1);
while(m--)
{
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if(op == 1)
{
updatePoint(l, r, 1, n, 1);
}
else if(op == 2)
{
cout << query(l, r, 1, n, 1) << endl;
}
else if(op == 3)
{
updateLine(l, r, 1, n, 1);
}
}
}
}
相关文章推荐
- 【HDU 4893 多校联合】 Wow! Such Sequence!【线段树】
- hdu 4893 Wow! Such Sequence! 线段树
- 线段树 + 区间更新: HDU 4893 Wow! Such Sequence!
- HDU 4893 Wow! Such Sequence! 线段树
- HDU 4893 Wow! Such Sequence! 水线段树
- 【HDU】4893 Wow! Such Sequence! 线段树
- HDU 4893 Wow! Such Sequence! (线段树)
- HDU 4893 Wow! Such Sequence! (线段树)
- HDU 4893(Wow! Such Sequence!-线段树单点修改+区间求和+改为最近Fib数)
- HDU 4893 Wow! Such Sequence!【线段树】
- ACM 线段树模板 hdu 4893 Wow! Such Sequence!
- hdu 4893 Wow! Such Sequence! 线段树单点更新+区间更新+区间查询
- hdu 4893 Wow! Such Sequence! 水线段树。
- HDU 4893 Wow! Such Sequence! (线段树)
- hdu 4893 Wow! Such Sequence!
- HDOJ 4893 Wow! Such Sequence! (线段树)
- hdu 4893 Wow! Such Sequence!
- HDU 4893 Wow! Such Sequence!
- 【线段树】 HDOJ 4893 Wow! Such Sequence!
- hdu 4893 Wow! Such Sequence!