题目链接: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 线段树的更多相关文章

  1. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  2. 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. ...

  3. Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]

    洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...

  4. 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]$ 其中 ...

  5. Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)

    第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...

  6. codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)

    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...

  7. 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 ...

  8. Random Numbers Gym - 101466K dfs序+线段树

    Tamref love random numbers, but he hates recurrent relations, Tamref thinks that mainstream random g ...

  9. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

随机推荐

  1. Spring在代码中获取bean的几种方式

    方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...

  2. highcharts 折线图

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  3. Fundamental Datastructure

    11988 - Broken Keyboard (a.k.a. Beiju Text) 可以用deque来模拟. #include <iostream> #include <stri ...

  4. android开发无障碍app

    最近做一些为盲人提供服务的APP,还是挺有感触的,感谢手机和互联网的普及,他们的生活比以前丰富了很多. 通过读屏软件,盲人可以操作手机,上网浏览信息.读屏软件的工作原理很简单,就是读出屏幕上按钮.文本 ...

  5. Microsoft.Data.ConnectionUI.DataConnectionDialog

    MicrosoftVisualStudio里面的资源之数据库连接配置 这个功能的实现主要是用了Microsoft.Data.ConnectionUI.dll和Microsoft.Data.Connec ...

  6. 在运行时切换 WinForm 程序的界面语言 System.ComponentModel.ComponentResourceManager .ApplyResources

    Download the code for this article: WinForm-Multilanguages-2.rar (11 KB). 方法二: 下面介绍一种只需对现有代码做较小改动的方法 ...

  7. 【HDOJ】4317 Unfair Nim

    基本的状态压缩,想明白怎么dp还是挺简单的.显然对n个数字进行状态压缩,dp[i][j]表示第i位状态j表示的位向高位产生了进位. /* 4317 */ #include <iostream&g ...

  8. Struts个人总结

    编写Struts2第一个程序 Struts2是目前最流行的MVC框架,吸收了传统Struts和WebWork两者的精华,基于Struts2来进行开发可以大大减少开发时间,提高开发效率,并降低后期维护时 ...

  9. 1067. Disk Tree(字符串)

    1067 破题啊  写完发现理解错题意了 子目录下会有跟之前重名的 把输入的字符串存下来 排下序 然后依次找跟上面有没有重的 #include <iostream> #include< ...

  10. 扫描.net dll引用dll

    最近升级系统里的NHibernate,从3.3到4,项目工程太多, 一个模块分bll,dal,model,web,test,10几个模块,就要60多dll,升级一次太头疼. 编译过后,有时候会有的dl ...