题目链接: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. Emoji表情符号兼容方案(适用ios,android,wp等平台)

    http://blog.csdn.net/qdkfriend/article/details/7576524 Emoji表情符号兼容方案 一 什么是Emoji emoji就是表情符号:词义来自日语(え ...

  2. Redis-PHP-Hash 表相关API

    Hashes 相关 ================================ hDel - 删除一个哈希 key hExists - 检查哈希 key是否存在 hGet - 获得某哈希 key ...

  3. CSU1326+背包+并查集

    先预处理出有多少个任务即可 #include<stdio.h> #include<stdlib.h> #include<string.h> #include< ...

  4. redisb并发访问慢出现的问题

    最近项目一上线,就问题颇多,本地测试,ok,上线后,大用户量的时候,顶不住.用了一个礼拜的时间发现的问题,总结下来. 项目是netty4.0,reids2.8,nginx等框架.目前是4台proxy服 ...

  5. NSString的常用方法

    创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...

  6. 理解TCP/IP协议

    TCP/IP协议是Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议. 单从TCP/IP协议这个名称看,好多人误 ...

  7. Android 签名(7)签名常见问题,debug签名和release签名的区别等

    一般在安装时提示出错:INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES 1) 两个应用,名字相同,签名不同 2) 升级时前一版本签名,后一版本没签名 3) ...

  8. Redpine的Lite-Fi解决方案获Wi-Fi CERTIFIED认证

    应用微电路公司(AMCC)和Redpine Signals日前共同宣布,已合作开发出新一代基于Power Architecture的嵌入式Wi-Fi连接性解决方案,目前双方已经在AMCC的PowerP ...

  9. bzoj1003: [ZJOI2006]物流运输

    dp+最短路.暴力枚举就可以了.O(n3logn).样例中m=n然后测样例过了.然后 54行习惯性的dis[n]然后就WA了!!!. #include<cstdio> #include&l ...

  10. 让Codeigniter控制器支持多级目录

    <?php if (!defined('BASEPATH')) { exit ('No direct script access allowed');} class MY_Router exte ...