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 ...
随机推荐
- WPF性能优化经验总结
WPF性能优化一.Rendering Tier 1. 根据硬件配置的不同,WPF采用不同的Rendering Tier做渲染.下列情况请特别注意,因为在这些情况下,即使是处于Rendering Tie ...
- XSS 攻击在它的面前都弱爆了!
虽然双十一刚刚过去不久,但是对很多工程师来说,连续熬夜加班的「噩梦」似乎还没有过去.尤其是像双十一这种活动,对于电商网站的工程师们来说,他们需要彻夜的加班加点来保障网站的稳定性和安全性.当然,面对上千 ...
- VARCHAR2转换为CLOB碰到ORA-22858错误
近日工作中发现有一张表的字段类型建错了,本应是BLOB类型却被别人建成了VARCHAR2(200),修改时oracle却提示“ORA-22858 invalid alteration of datat ...
- PHP设计模式浅析
工厂模式 提到的最多, 用途也最广. 简单说就是: 定义一个用户创建对象的接口. 把创建对象的过程封装起来. 工厂类是指包含一个专门用来创建其他对象的方法的类,工厂类在多态性编程实践中是至关重要的,它 ...
- Android 自定义对话框使用静态Handler传递参数
JsdMainDialog.java package com.jsd.demo; import android.app.Activity; import android.content.Context ...
- LINUX进程优先级实现
首先linux进程优先级的范围是-20到19 将当前目录下的documents目录打包,但不希望tar占用太多CPU: nice -19 tar zcf pack.tar.gz documents 这 ...
- ServletContext对象
**1 ServletContext对象 1)在web应用中,由服务器创建的唯一的一个对象是ServletContext 2)ServletContext对象在每一个Servlet中取得都是相 ...
- C#中的多文档的使用
1.首先,新建一个窗体,设置窗体的IsMdiContainer = true; 窗体的大小为700*600 长700 高600 2.在窗体的Load事件中添加如下代码 private void F ...
- C语言一维数组中的数据随机排列
#include <stdio.h>#include <stdlib.h> void randomlize(int *a, int n){ int i = 0,j ...
- poj3592Instantaneous Transference(tarjan+spfa)
http://poj.org/problem?id=3592提交了30多次了 受不了了 两份的代码基本上一样了 一个AC一个WA 木办法 贴份别人的吧 改得跟我得一样 人家能A 我是WA.. 强连通 ...