Bnuoj-29359 Deal with numbers 线段树
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29359
题意:一个数列,有三种操作:
1.区间[a,b]之间大于零的数整出c。
2.区间[a,b]之间所有的数减去c。
3.求区间[a,b]的和。
只要注意到每个数最多除lgn次,总共除n*lgn次,那么直接对除法进行单点更新就可了,关键要分析好复杂度。。
//STATUS:C++_AC_3020MS_33996KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL sum[N<<],miu[N<<];
int flag[N<<],num[N];
int T,n,m,a,b,c;
LL ans; void pushdown(int l,int r,int rt)
{
if(miu[rt]){
miu[rt<<]+=miu[rt];
miu[rt<<|]+=miu[rt];
sum[rt]+=miu[rt]*(r-l+);
miu[rt]=;
}
} void pushup(int l,int r,int rt)
{
int mid=(l+r)>>;
sum[rt]=sum[rt<<]+(mid-l+)*miu[rt<<]
+sum[rt<<|]+(r-mid)*miu[rt<<|];
flag[rt]=flag[rt<<]|flag[rt<<|];
} void build(int l,int r,int rt)
{
miu[rt]=;
if(l==r){
sum[rt]=num[l];
flag[rt]=num[l]>;
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
sum[rt]=sum[rt<<]+sum[rt<<|];
flag[rt]=flag[rt<<]|flag[rt<<|];
} void update1(int l,int r,int rt)
{
if(l==r){
sum[rt]+=miu[rt];
miu[rt]=;
sum[rt]=(sum[rt]>?sum[rt]/=c:sum[rt]);
flag[rt]=sum[rt]>;
return;
}
int mid=(l+r)>>;
pushdown(l,r,rt);
if(a<=mid && flag[rt<<])update1(lson);
if(b>mid && flag[rt<<|])update1(rson);
pushup(l,r,rt);
} void update2(int l,int r,int rt)
{
if(a<=l && r<=b){
miu[rt]-=c;
return;
}
int mid=(l+r)>>;
pushdown(l,r,rt);
if(a<=mid)update2(lson);
if(b>mid)update2(rson);
pushup(l,r,rt);
} void query(int l,int r,int rt)
{
if(a<=l && r<=b){
ans+=sum[rt]+(r-l+)*miu[rt];
return;
}
int mid=(l+r)>>;
pushdown(l,r,rt);
if(a<=mid)query(lson);
if(b>mid)query(rson);
pushup(l,r,rt);
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,ca=;
char s[];
scanf("%d",&T);
while(T--)
{
printf("Case %d:\n",ca++);
scanf("%d%d",&n,&m);
mem(sum,),mem(miu,);
for(i=;i<=n;i++){
scanf("%d",&num[i]);
}
build(,n,);
while(m--){
scanf("%s",s);
if(s[]=='D'){
scanf("%d%d%d",&a,&b,&c);
if(c==)continue;
update1(,n,);
}
else if(s[]=='M'){
scanf("%d%d%d",&a,&b,&c);
update2(,n,);
}
else {
scanf("%d%d",&a,&b);
ans=;
query(,n,);
printf("%lld\n",ans);
}
}
putchar('\n');
}
return ;
}
Bnuoj-29359 Deal with numbers 线段树的更多相关文章
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- codeforces 446C DZY Loves Fibonacci Numbers 线段树
假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- CF446C DZY Loves Fibonacci Numbers 线段树 + 数学
有两个性质需要知道: $1.$ 对于任意的 $f[i]=f[i-1]+f[i-2]$ 的数列,都有 $f[i]=fib[i-2]\times f[1]+fib[i-1]\times f[2]$ 其中 ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Random Numbers Gym - 101466K dfs序+线段树
Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
随机推荐
- uva 10562
二叉树的先序遍历 这个还是比较简单的 ~~ /************************************************************************* &g ...
- python参考手册--第9章
1.读取命令行选项 (1)sys.args python启动时,命令行选项设置在列表sys.args中. sys.args[0]:xxx.py sys.args[1:]: 其他参数 (2)optpar ...
- IText 生成简单表格(报表)doc文档 单元居中
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下所示: import com.lowagie ...
- java jdk自带程序分析(内存分析/线程分析)
周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令. 1.1.Jstack 1.1 jstack能得到运行jav ...
- js动态创建及移除div的方法
本文实例讲述了js动态创建及移除div的方法.分享给大家供大家参考.具体实现方法如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- LINUX进程优先级实现
首先linux进程优先级的范围是-20到19 将当前目录下的documents目录打包,但不希望tar占用太多CPU: nice -19 tar zcf pack.tar.gz documents 这 ...
- 【HDOJ】3828 A + B problem
显然需要贪心,重叠越长越好,这样最终的串长尽可能短.需要注意的是,不要考虑中间结果,显然是个状态dp.先做预处理去重,然后求任意一对串的公共长度. /* 3828 */ #include <io ...
- Smallest unused ID
http://www.codewars.com/kata/smallest-unused-id Description: Hey awesome programmer! You've got much ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- oracle 根据汉字返回拼音函数
参见戴明明的博客,oracle 根据汉字返回拼音函数,由于他的博客里没有提供完整的代码,研究了一个多小时,才弄出来: 上来贴代码吧.. --------------Type Definition CR ...