Problem J: Alex’s Foolish Function

Time Limit: 8 Sec  Memory Limit: 128 MB
Submit: 18  Solved: 2

Description

Alex has an array F which size is n, initially each element’s value is zero. Now he wants to operate the array, he has 4 different functions:
1) Function A(l, r):
Add the elements between l to r (inclusive) where the ith add i - l + 1, for instance, if l is 4,
r is 6 and the elements between 4 to 6 is 3 4 10, after this operation, these elements will become 4 6 13.
2) Function B(l, r):
Add the elements between l to r (inclusive) where the ith add r - i + 1, for instance, if l is 4,
r is 6 and the elements between 4 to 6 is 3 4 10, after this operation, these elements will become 6 6 11.
3) Function C(l, r, x):
Set all the elements(between l to r) to x, for instance, if l is 4, r is 6 and the elements
between 4 to 6 is 3 4 10, and x = 2, after this operation, these elements will become 2 2 2.
4) Function S(l, r):
Output Fl + Fl+1 + Fl+2 + ...+ Fr. 

Input

Input start with an integer T(1 <= T <= 5), denoting the number of test case.
For each test case, first line contains n, m (1 <= n <= 200000, 1 <= m <= 100000), denoting the array’size and the number of operations.
Next m lines, each line contains an operation, formatting as
A l r
B l r
C l r x
S l r 

Output

For each S Function operations, output the sum. 

Sample Input

1
5 4
A 1 3
B 2 5
C 1 1 2
S 1 5

Sample Output

17

正规做法是线段树维护区间的左加、右加或者覆盖信息,然而想用分块写一写。

对于A操作,每一个块维护起始的加数,以及A操作的次数,

对于B操作,每一个块维护末尾的加数,以及B操作的次数,

对于C操作,每一个块维护准备覆盖的值val。

显然对于A操作和B操作维护的信息,是满足区间加法的,即[1,4]A操作一次,[2,4]A操作一次,比如当前块是[2,4],记为Bx,那么我们可以记录起始的加数:(2-1+1)+(2-2+1)=3,A操作次数为2,即arr[2]+=3,arr[3]+=(3+2),arr[4]+=(3+2+2),然后我们可以发现这是一个等差数列,可以用公式快速得到有延迟标记A操作的区间和,即$(Bx.lazy_A+(Bx.lazy_A+Bx.len-1))*Bx.len/2+Bx.sum$,然后B操作也同理,最后加上本身的$Bx.sum$即可(这里要注意AB操作不能重复加Bx.sum);B操作同理;C操作的话由于是覆盖那么在更新的时候把更新到的块的$lazy_c$更新,并把$lazy_A$与$lazy_B$取消掉即可,由于可能$lazy_c$为0,初始值应设为一个不太可能用到的数,比如$-INF$。

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 200010;
const int M = 450;
struct Block
{int l, r;LL lazy1, lazy2, lazy3, sum;LL c1, c2;inline int len(){return r - l + 1;}
};
Block B[M];
LL arr[N];
int belong[N], unit, bcnt;
int n, m;void init()
{int i;unit = sqrt(n);bcnt = n / unit;if (n % unit)++bcnt;for (i = 1; i <= bcnt; ++i){B[i].l = (i - 1) * unit + 1;B[i].r = i * unit;B[i].sum = 0LL;B[i].lazy1 = 0LL;B[i].lazy2 = 0LL;B[i].lazy3 = -INF;B[i].c1 = B[i].c2 = 0LL;}B[bcnt].r = n;for (i = 1; i <= n; ++i){arr[i] = 0LL;belong[i] = (i - 1) / unit + 1;}
}
void pushdown(int bx)
{if (B[bx].lazy3 != -INF){for (int i = B[bx].l; i <= B[bx].r; ++i)arr[i] = B[bx].lazy3;B[bx].lazy3 = -INF;}if (B[bx].lazy1){for (int i = B[bx].l; i <= B[bx].r; ++i){arr[i] += + B[bx].lazy1;B[bx].lazy1 += B[bx].c1;}B[bx].lazy1 = 0LL;B[bx].c1 = 0LL;}if (B[bx].lazy2){for (int i = B[bx].r; i >= B[bx].l; --i){arr[i] += + B[bx].lazy2;B[bx].lazy2 += B[bx].c2;}B[bx].lazy2 = 0LL;B[bx].c2 = 0LL;}B[bx].sum = 0LL;for (int i = B[bx].l; i <= B[bx].r; ++i)B[bx].sum += arr[i];
}
void update(int l, int r, int ops, LL val = 0LL)
{int bl = belong[l], br = belong[r];int i;if (bl == br){pushdown(bl);if (ops == 1){for (i = l; i <= r; ++i){B[bl].sum -= arr[i];arr[i] = arr[i] + (LL)(i - l + 1LL);B[bl].sum += arr[i];}}else if (ops == 2){for (i = r; i >= l; --i){B[bl].sum -= arr[i];arr[i] = arr[i] + (LL)(r - i + 1LL);B[bl].sum += arr[i];}}else if (ops == 3){for (i = l; i <= r; ++i){B[bl].sum -= arr[i];arr[i] = val;B[bl].sum += arr[i];}}}else{//leftpushdown(bl);if (ops == 1){for (i = l; i <= B[bl].r; ++i){B[bl].sum -= arr[i];arr[i] = arr[i] + (LL)(i - l + 1LL);B[bl].sum += arr[i];}}else if (ops == 2){for (i = B[bl].r; i >= l; --i){B[bl].sum -= arr[i];arr[i] = arr[i] + (LL)(r - i + 1LL);B[bl].sum += arr[i];}}else if (ops == 3){for (i = l; i <= B[bl].r; ++i){B[bl].sum -= arr[i];arr[i] = val;B[bl].sum += arr[i];}}//rightpushdown(br);if (ops == 1){for (i = B[br].l; i <= r; ++i){B[br].sum -= arr[i];arr[i] = arr[i] + (LL)(i - l + 1LL);B[br].sum += arr[i];}}else if (ops == 2){for (i = r; i >= B[br].l; --i){B[br].sum -= arr[i];arr[i] = arr[i] + (LL)(r - i + 1LL);B[br].sum += arr[i];}}else if (ops == 3){for (i = B[br].l; i <= r; ++i){B[br].sum -= arr[i];arr[i] = val;B[br].sum += arr[i];}}for (i = bl + 1; i < br; ++i){if (ops == 3){B[i].lazy3 = val;B[i].c1 = 0LL;B[i].c2 = 0LL;B[i].lazy1 = 0LL;B[i].lazy2 = 0LL;}else if (ops == 2){B[i].lazy2 += (LL)(r - B[i].r + 1LL);++B[i].c2;}else if (ops == 1){B[i].lazy1 += (LL)(B[i].l - l + 1LL);++B[i].c1;}}}
}
LL query(int l, int r)
{int bl = belong[l], br = belong[r], i;LL sum = 0LL;if (bl == br){pushdown(bl);for (i = l; i <= r; ++i)sum += arr[i];return sum;}else{pushdown(bl);pushdown(br);for (i = l; i <= B[bl].r; ++i)sum += arr[i];for (i = B[br].l; i <= r; ++i)sum += arr[i];for (i = bl + 1; i < br; ++i){if (B[i].lazy3 != -INF)sum += (LL)B[i].len() * B[i].lazy3;if (B[i].lazy1)sum += (B[i].lazy1 + B[i].lazy1 + (LL)(B[i].len() - 1LL) * B[i].c1) * (LL)B[i].len() / 2LL;if (B[i].lazy2)sum += (B[i].lazy2 + B[i].lazy2 + (LL)(B[i].len() - 1LL) * B[i].c2) * (LL)B[i].len() / 2LL;if (B[i].lazy3 == -INF)sum += B[i].sum;}return sum;}
}
int main(void)
{// fin("test0.in");// fout("data_wa.txt");int tcase, l, r, v;char ops[4];scanf("%d", &tcase);while (tcase--){scanf("%d%d", &n, &m);init();while (m--){scanf("%s", ops);if (ops[0] == 'A'){scanf("%d%d", &l, &r);update(l, r, 1);}else if (ops[0] == 'B'){scanf("%d%d", &l, &r);update(l, r, 2);}else if (ops[0] == 'C'){scanf("%d%d%d", &l, &r, &v);update(l, r, 3, (LL)v);}else{scanf("%d%d", &l, &r);printf("%lld\n", query(l, r));}}}return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/7223649.html

NBUT校赛 J Alex’s Foolish Function(分块+延迟标记)相关推荐

  1. 21-5-22校赛J 下围棋

    21-5-22校赛J 下围棋 给出一个节点个数为n的树,其根节点为1,两个人轮流操作,每一次操作可以选择一个非根节点,删除这个节点及其子树,当一个人无法进行操作的时候,他就输掉了.问后手是必胜还是必败 ...

  2. QAU 18校赛 J题 天平(01背包 判断能否装满)

    问题 J: 天平 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 9 [提交][状态][讨论版][命题人:admin] 题目描述 天平的右端放着一件重量为w的物品.现在有n ...

  3. 西交2021校赛 J题校赛质量

    彩彩在视频网站上拥有N个粉丝,并且发布了M个视频,编号为0,1,-,M-1 .粉丝会为编号连续的视频点赞,其中第 i 个粉丝选择为 (表示大于等于 并且严格小于 的整数范围)的视频点赞.该网站对视频质 ...

  4. DMCTF校赛思路总结

    DMCTF校赛思路总结 针对于校赛的一些题目的思路进行汇总. WEB 题: Weak_type: ​ 我首先是百度了一下,题中涉及到的 isset(),intval()这两个函数,然后知道了第一关的含 ...

  5. [置顶]2010年东北大学ACM程序设计竞赛冬季校赛题解

    8题只做出4题比较easy的题,而且做得挺麻烦,看来还要多练练. AC的题如下 NEUOJ  1112 I Love Apple Description So many people love app ...

  6. 2018 java蓝桥杯校赛题目

    1.[问题描述] 100 可以表示为带分数的形式:100 = 3 + 69258 / 714 还可以表示为:100 = 82 + 3546 / 197 注意特征:带分数中,数字1~9分别出现且只出现一 ...

  7. 多校赛 Barareh on Fire

    打完多校赛网络赛后 对这题有点感想,Barareh on Fire  点击打开链接 题意:你在一个荒野,初始位置为 s 你要走到出口 t 但是 荒野上有火 f ,火每隔 k 秒向周围蔓延(八个方向), ...

  8. 山东科技大学第二届ACM校赛解题报告

    这次校赛的目的,是为了省赛测试各种程序是否有问题. 热身赛的逗比题有点打击我,感觉正式赛应该不会出这种问题.开始的时候直接上了A题,然后大概是第六,前面好多友情队,正式队排名第二. 然后读了读B题,稍 ...

  9. 中国计量大学现代科技学院第四届“中竞杯”程序设计校赛(同步赛)F.爬塔

    中国计量大学现代科技学院第四届"中竞杯"程序设计校赛(同步赛)F.爬塔 题目链接 题目描述 高川最喜欢的游戏当属 Slay the Spire,这是一款爬塔游戏,你需要从一座塔的底 ...

最新文章

  1. 详解java方法与递归
  2. hdu2102(bfs)
  3. JAVA 文件监控 WatchService
  4. QML资源加载和网络透明度
  5. 文本生成器(bzoj 1030)
  6. axios请求拦截器、响应拦截器、vue-router路由导航守卫的使用(案例)
  7. 竞赛|数据竞赛Top解决方案开源整理-科大讯飞AI营销算法、阿里妈妈搜索广告、腾讯广告算法、搜狗的用户画像
  8. c语言中的标准数据类型,C语言中的基本数据类型
  9. 今儿新学会一个写日志技能:双缓冲机制
  10. 插入、删除和随机查询时间复杂度都为O(1) leetcode 381
  11. Java生成CSV文件的方法
  12. 转载金阳光测试:关于产品的三类文档
  13. Arduino - CHQ1838B红外接收模块控制LED实验(附.Tinkercad图形化编程)
  14. 学习计划大纲(大一)
  15. 【日常吐槽 · 第七期】进击的博客
  16. linu离线安装java1.8
  17. 一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)
  18. python科研作图系列-01热力图相关性分析
  19. linux篇之安装MySQL及远程连接
  20. 六、Hystrix详解三:Hystrix的健康监测

热门文章

  1. 华为新系统鸿蒙升级平板名单,华为鸿蒙系统升级名单正式确认!全面替换安卓:流畅度比肩iOS...
  2. java account函数的_用Java进行同时函数调用 - java
  3. 【深度学习】神经网络结构搜索(NAS)与多模态
  4. python【蓝桥杯vip练习题库】ADV-313字符串顺序比较
  5. python【力扣LeetCode算法题库】4- 寻找两个有序数组的中位数
  6. 数据分析的 8 种思维
  7. 习题1.8 二分查找 (20 分)
  8. 需求分析的接口需求_再谈需求分析
  9. android 动画引擎,一个使用openGL渲染的炫丽Android动画库
  10. python交作业的格式_python作业4