题目链接: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. Android 环境搭建 版本问题

    jdk1.6 1.7  eclipse 3.7.2    SDK-r12 ADT 12 SDK和ADT必须配套 搭建环境需要四个软件: 1.JDK(这是最新版本jdk1.7官方下载地址:http:// ...

  2. 用CImage类来显示PNG、JPG等图片

    系统环境:Windows 7软件环境:Visual Studio 2008 SP1本次目的:实现VC单文档.对话框程序显示图片效果 CImage 是VC.NET中定义的一种MFC/ATL共享类,也是A ...

  3. Android ExpandableListView 带有Checkbox的简单应用

    expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...

  4. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  5. AD15高版软件卡不卡,问题解决大讨论

    AD高版软件很卡(包括13 14 15版),这是我遇到过的问题,大家都遇到过的问题, 这里我分享一个解决办法:也请给位有什么好的方法也一起分享. 问题1卡:打开AD15软件, 按住鼠标中键 放大 或 ...

  6. 最受欢迎的5个Android ORM框架

    在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...

  7. 对Cost (%CPU) 粗略的理解

    今天研究执行计划,看到执行计划里面有Cost (%CPU),我这边研究了一把,不知道对与否,拿出来晒晒 在Oracle 10g中,Oracle 把CPU的cost也统计在执行计划中去了, 这和以前的8 ...

  8. CHtmlView类的中文介绍

    http://zhidao.baidu.com/link?url=h8FaKA6FMNXzYJu_XO-_buBxuGdM0jozKUSVv6pgEPsvhTB2-xLltH-jVLDDJKMBAkn ...

  9. python学习笔记二--列表

    一.列表: 1. 任意类型对象的位置相关的有序集合. 2. 没有固定大小. 3. 对偏移量进行赋值及各种方法的调用,修改列表. 4. 列表是序列的一种. 5. 所有对字符串的序列操作对列表均适用. 二 ...

  10. System.Drawing.Design.UITypeEditor自定义控件属性GetEditStyle(ITypeDescriptorContext context),EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...