https://www.luogu.org/problemnew/show/P4243#sub

自己的思路错了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> using namespace std;
const int N = 1e5 + ; #define gc getchar() int F[N << ], tot[N << ], l_col[N << ], r_col[N << ];
int n, Ty, Answer;
int my[N], data[N]; inline int read() {
int x = , f = ; char c = gc;
while(c < '' || c > '') {if(c == '-') f = -; c = gc;}
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x * f;
} #define lson jd << 1
#define rson jd << 1 | 1 void Push_up(int jd) {
l_col[jd] = l_col[lson]; r_col[jd] = r_col[rson];
tot[jd] = tot[lson] + tot[rson];
if(r_col[lson] == l_col[rson]) tot[jd] --;
return ;
} void Build_tree(int l, int r, int jd) {
if(l == r) {l_col[jd] = r_col[jd] = data[l]; tot[jd] = ; return ;}
int mid = (l + r) >> ;
Build_tree(l, mid, lson);
Build_tree(mid + , r, rson);
Push_up(jd);
} void Push_down(int jd) {
F[lson] += F[jd]; F[rson] += F[jd];
l_col[lson] += F[jd]; r_col[lson] += F[jd];
l_col[rson] += F[jd]; r_col[rson] += F[jd];
F[jd] = ;
return ;
} void Poi_G(int l, int r, int jd, int x, int num) {
if(l == r) {l_col[jd] += num; r_col[jd] += num; return ;}
if(F[jd]) Push_down(jd);
int mid = (l + r) >> ;
if(x <= mid) Poi_G(l, mid, lson, x, num);
else Poi_G(mid + , r, rson, x, num);
Push_up(jd);
} void Sec_G(int l, int r, int jd, int x, int y, int num) {
if(x <= l && r <= y) {F[jd] += num; l_col[jd] += num; r_col[jd] += num; return ;}
if(F[jd]) Push_down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_G(l, mid, lson, x, y, num);
if(y > mid) Sec_G(mid + , r, rson, x, y, num);
Push_up(jd);
} void Sec_A(int l, int r, int jd, int x, int y) {
if(x <= l && r <= y) {Answer += tot[jd]; return ;}
if(F[jd]) Push_down(jd);
int mid = (l + r) >> ;
if(x <= mid) Sec_A(l, mid, lson, x, y);
if(y > mid) Sec_A(mid + , r, rson, x, y);
if(x <= mid && y > mid && r_col[lson] == l_col[rson]) Answer --;
} int main() {
n = read();
for(int i = ; i <= n; i ++) my[i] = read();
for(int i = ; i <= n; i ++) data[i] = my[i] - my[i - ];
Build_tree(, n, );
Ty = read();
while(Ty --) {
string s; cin >> s;
if(s[] == 'A') {
int l = read(), r = read(), a = read(), b = read();
int imp = a + (r - l) * b;
if(l != ) Poi_G(, n, , l, a);
Sec_G(, n, , l + , r, b);
Poi_G(, n, , r + , - imp);
} else {
Answer = ;
int l = read(), r = read();
if(l == r) {cout << "" << endl; continue ;}
Sec_A(, n, , l, r);
cout << Answer << endl;
}
}
return ;
}

https://www.cnblogs.com/zbtrs/p/8424737.html

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const ll maxn = ;
ll n,a[maxn],b[maxn],q; struct node {
ll ls,rs,lsize,rsize,sum,tag,sizee;
void init() {
ls = rs = lsize = rsize = sum = tag = sizee = ;
}
} e[maxn]; void pushdown(ll o) {
if (e[o].tag) {
e[o * ].tag += e[o].tag;
e[o * + ].tag += e[o].tag;
e[o * ].ls += e[o].tag;
e[o * + ].ls += e[o].tag;
e[o * ].rs += e[o].tag;
e[o * + ].rs += e[o].tag;
e[o].tag = ;
}
} node pushup(node a,node b) {
bool flag = (a.rs == b.ls);
node c;
c.init();
c.ls = a.ls;
c.rs = b.rs;
c.sizee = a.sizee + b.sizee;
c.sum = a.sum + b.sum;
if (a.sum == && b.sum == ) {
if (!flag) {
c.lsize = a.sizee + b.sizee;
c.rsize = a.sizee + b.sizee;
} else {
c.lsize = a.lsize - ;
c.rsize = b.rsize - ;
++c.sum;
}
return c;
}
if (a.sum == ) {
c.rsize = b.rsize;
if (!flag)
c.lsize = a.sizee + b.lsize;
else {
c.lsize = a.lsize - ;
if (b.lsize > )
c.sum += (b.lsize - ) / + ;
}
return c;
}
if (b.sum == ) {
c.lsize = a.lsize;
if (!flag)
c.rsize = b.sizee + a.rsize;
else {
c.rsize = b.rsize - ;
if (a.rsize > )
c.sum += (a.rsize - ) / + ;
}
return c;
}
c.lsize = a.lsize;
c.rsize = b.rsize;
if (a.rsize == && b.lsize == ) {
if (flag)
c.sum--;
return c;
}
if (a.rsize == ) {
if (flag)
c.sum += (b.lsize - ) / ;
else
c.sum += b.lsize / ;
return c;
}
if (b.lsize == ) {
if (flag)
c.sum += (a.rsize - ) / ;
else
c.sum += a.rsize / ;
return c;
}
ll minn = (a.rsize + b.lsize) / ;
if (flag)
minn = min(minn, + (a.rsize - ) / + (b.lsize - ) / );
c.sum += minn;
return c;
} void build(ll o,ll l,ll r) {
if (l == r) {
e[o].init();
e[o].ls = e[o].rs = b[l];
e[o].lsize = e[o].rsize = ;
e[o].sizee = ;
return;
}
ll mid = (l + r) >> ;
build(o * ,l,mid);
build(o * + ,mid + ,r);
e[o] = pushup(e[o * ],e[o * + ]);
} void update1(ll o,ll l,ll r,ll pos,ll v) {
if (l == r) {
e[o].ls += v;
e[o].rs += v;
return;
}
pushdown(o);
ll mid = (l + r) >> ;
if (pos <= mid)
update1(o * ,l,mid,pos,v);
else
update1(o * + ,mid + ,r,pos,v);
e[o] = pushup(e[o * ],e[o * + ]);
} void update2(ll o,ll l,ll r,ll x,ll y,ll v) {
if (x <= l && r <= y) {
e[o].ls += v;
e[o].rs += v;
e[o].tag += v;
return;
}
pushdown(o);
ll mid = (l + r) >> ;
if (x <= mid)
update2(o * ,l,mid,x,y,v);
if (y > mid)
update2(o * + ,mid + ,r,x,y,v);
e[o] = pushup(e[o * ],e[o * + ]);
} node query(ll o,ll l,ll r,ll x,ll y) {
if (x <= l && r <= y)
return e[o];
pushdown(o);
ll mid = (l + r) >> ;
if (y <= mid)
return query(o * ,l,mid,x,y);
else if (x > mid)
return query(o * + ,mid + ,r,x,y);
else
return pushup(query(o * ,l,mid,x,mid),query(o * + ,mid + ,r,mid + ,y));
} int main() {
scanf("%lld",&n);
for (ll i = ; i <= n; i++)
scanf("%lld",&a[i]);
for (ll i = ; i < n; i++)
b[i] = a[i + ] - a[i];
n--;
build(,,n);
scanf("%lld",&q);
while (q--) {
char ch[];
ll s,t,a,b;
scanf("%s",ch);
if (ch[] == 'A') {
scanf("%lld%lld%lld%lld",&s,&t,&a,&b);
if (s != )
update1(,,n,s - ,a);
if (t != n + )
update1(,,n,t,- * ((t - s) * b + a));
if (s <= t - )
update2(,,n,s,t - ,b);
} else {
scanf("%lld%lld",&s,&t);
if (s == t) {
printf("1\n");
continue;
}
node temp = query(,,n,s,t - );
ll res = (t - s + ) / ;
if (temp.sum == )
printf("%lld\n",res);
else {
res = min(res,temp.sum + (temp.lsize + ) / + (temp.rsize + ) / );
printf("%lld\n",res);
}
}
} return ;
}

[Luogu] 等差数列的更多相关文章

  1. luogu P1214 [USACO1.4]等差数列 Arithmetic Progressions

    题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...

  2. luogu P4243 [JSOI2009]等差数列 题解

    前言: 这题真ex... 强烈谴责在题解里面放毒瘤题链接的屑出题人! 吐 ️ 解析: 这题分成两步走. 首先,既然题目中的修改操作是区间加等差数列,那么就容易想到在差分数组上进行操作. 然后就是相当恶 ...

  3. 【题解】Luogu P5313 僕たちはひとつの光([Ynoi2012]D2T2)

    原题传送门 lovelive好评 比赛时只拿到了60pts,还是自己太菜了 这题的思想实际有点像Luogu P3674 小清新人渣的本愿与Luogu P5071 [Ynoi2015]此时此刻的光辉 这 ...

  4. luogu P4156 [WC2016]论战捆竹竿

    传送门 官方题解(证明都在这) 神仙题鸭qwq 转化模型,发现这题本质就是一个集合,每次可以加上集合里的数,问可以拼出多少不同的数 首先暴力需要膜意义下的最短路,例题戳这 然后这个暴力可以优化成N^2 ...

  5. [luogu P1438] 无聊的数列

    [luogu P1438] 无聊的数列 题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个 ...

  6. [luogu P4230]连环病原体

    [luogu P4230] 连环病原体 题意 给定一个长度为 \(n\) 的边序列, 当这个序列的一个子区间内的边都加入图中时产生了环则称其为"加强区间", 求序列中的每条边在多少 ...

  7. 洛谷 P1214 等差数列

    https://www.luogu.org/problemnew/show/P1214 首先暴力枚举可以凑出来的数,对于每个数进行标记. 对于每一个等差数列,当我们知道前两个数后即可以得出整个序列,那 ...

  8. 「Luogu 3792」由乃与大母神原型和偶像崇拜

    更好的阅读体验 Portal Portal1: Luogu Description 给你一个序列\(a\) 每次两个操作: 修改\(x\)位置的值为\(y\): 查询区间\([l, r]\)是否可以重 ...

  9. [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)

    题面 传送门:洛咕 Solution 调到自闭,我好菜啊 为了方便讨论,以下式子\(m>=n\) 为了方便书写,以下式子中的除号均为向下取整 我们来颓柿子吧qwq 显然,题目让我们求: \(\l ...

随机推荐

  1. ElasticSearch中term和match探索

    一.创建测试数据 1.创建一个index curl -X PUT http://127.0.0.1:9200/student?pretty -H "Content-Type: applica ...

  2. 使用rsync工具构建php项目管理平台

    对于phper来说部署项目和更新项目是很方便的,只要直接将写好的项目覆盖到项目的根目录就可以啦.但是平时项目开发的时候肯定不是只部署一个环境,一般是三套环境(开发环境.测试环境.生产环境),我们每次在 ...

  3. RPA自定义脚本打开文件夹

    import os import subprocess from rpa.web.common.utils import convert_2_unicode def startfile(filenam ...

  4. 17.tmux相关

    Linux终端复用神器-Tmux使用梳理 Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是,通过一个终端登录远程主机并运行tm ...

  5. DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化

    DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化 2017年11月29日 06:40:37 机器之心V 阅读数 2183   版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  6. .net Core 图片验证码 基于SkiaSharp实现

    public class ImageCaptcha { /// <summary> /// 干扰线的颜色集合 /// </summary> private List<SK ...

  7. spring利用xml配置定时任务

    在开发中会经常遇到做定时任务的需求,例如日志定时清理与处理,数据信息定时同步等需求. 1.在spring中利用xml配置定时任务,如下 <!-- ftpiptv信息同步接口定时任务配置--> ...

  8. html5中本地存储概念是什么?

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话中的数据,这些数据只有在同一个会话中的页 ...

  9. 销售订单(SO)-API-更新销售订单

    更新销售订单和创建销售订单差不多,调用的API相同,只是传入的时候标识不一样:operation := oe_globals.g_opr_update 示例代码如下: PROCEDURE update ...

  10. linux MD5使用

    # define MD5_LONG unsigned int # define MD5_CBLOCK 64 # define MD5_LBLOCK (MD5_CBLOCK/4) # define MD ...