SPOJ DCEPC11I
题目大意:
就是给定一段区间令其中的数增加一个递增序列(也就是说第一个+1,第二个+2.。。。。)
询问操作是区间的和
这里的查询很简单,但是对于添加递增序列入区间就比较搞脑子了
我们需要一个add[]作为区间的首个数字增加的值,del[]表示等差数列的公差,因为你每次添加进入一个等差数列,是可以叠加的但公差变为了del[ls]+=del[o]
这里主要是pushdown函数的写法
我们要用到等差公式的求和:S =a*n+n(n-1)*d/2
void push_down(int o,int x,int y)
{
int mid=(x+y)/2,ls=o<<1,rs=o<<1|1;
if(add[o]||del[o]){
int t1=mid-x+1,t2=y-mid;
LL a2=add[o]+t1*del[o];
sum[ls]+=add[o]*t1+t1*(t1-1)*del[o]/2;
sum[rs]+=a2*t2+t2*(t2-1)*del[o]/2;
add[ls]+=add[o],add[rs]+=a2;
del[ls]+=del[o],del[rs]+=del[o];
add[o]=del[o]=0;
}
}
总代码如下所示:
#include <cstdio>
#include <cstring>
using namespace std;
#define L ls,x,mid
#define R rs,mid+1,y
#define LL long long
#define N 100010
LL sum[N<<],add[N<<];
int del[N<<];
void push_up(int o)
{
sum[o]=sum[o<<]+sum[o<<|];
}
void push_down(int o,int x,int y)
{
int mid=(x+y)/,ls=o<<,rs=o<<|;
if(add[o]||del[o]){
int t1=mid-x+,t2=y-mid;
LL a2=add[o]+t1*del[o];
sum[ls]+=add[o]*t1+t1*(t1-)*del[o]/;
sum[rs]+=a2*t2+t2*(t2-)*del[o]/;
add[ls]+=add[o],add[rs]+=a2;
del[ls]+=del[o],del[rs]+=del[o];
add[o]=del[o]=;
}
}
void build(int o,int x,int y)
{
int mid=(x+y)/,ls=o<<,rs=o<<|;
sum[o]=del[o]=add[o]=;
if(x==y) return;
build(L);
build(R);
}
void update(int o,int x,int y,int s,int t)
{
int mid=(x+y)/,ls=o<<,rs=o<<|;
if(x>=s&&y<=t){
add[o]+=x-s+;
sum[o]+=(x-s+)*(y-x+)+(y-x+)*(y-x)/;
del[o]++;
return;
}
push_down(o,x,y);
if(mid>=s) update(L,s,t);
if(mid<t) update(R,s,t);
push_up(o);
}
void query(int o,int x,int y,int s,int t,LL &ans)
{
int mid=(x+y)/,ls=o<<,rs=o<<|;
if(x>=s&&y<=t){
ans+=sum[o];
return;
}
push_down(o,x,y);
if(mid>=s) query(L,s,t,ans);
if(mid<t) query(R,s,t,ans);
}
int main()
{
int n,q,op,a,b;
scanf("%d%d",&n,&q);
build(,,n);
for(int i=;i<q;i++){
scanf("%d%d%d",&op,&a,&b);
if(op){
LL ans=;
query(,,n,a,b,ans);
printf("%lld\n",ans);
}
else update(,,n,a,b);
}
return ;
}
SPOJ DCEPC11I的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
随机推荐
- Problem D: 勤奋的涟漪2 dp + 求导
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...
- Java 中 i++和++i的区别
public class Test{ public static void main(String [] args){ int i = 1; int s = ++i; int x= i++; Syst ...
- 2019/05/11 Java内存结构
1. 类加载子系统:负责从文件系统或者网络加载Class信息,加载的信息存放在一块称之方法区的内存空间. 2. 方法区:就是存放类的信息.常量信息.常量池信息.包括字符串字面量和数字常量等. 3. ...
- T4308 数据结构判断
https://www.luogu.org/record/show?rid=2143639 题目描述 在世界的东边,有三瓶雪碧. ——laekov 黎大爷为了虐 zhx,给 zhx 出了这样一道题.黎 ...
- VPS环境配置预备篇
VPS买到手了,在配置环境前要做哪些操作呢?老谢说一下自己的习惯,希望对和老谢一样的菜鸟有帮助更新系统内核和rpm包#安装yum-fastestmirror插件yum -y install yum-f ...
- Java JDK装配置
1- 介绍 本文章介绍JAVA开发环境安装是基于: Java8(JDK8) 2- 下载JDK http://www.oracle.com/technetwork/java/javase/dow ...
- (转)配置Spring管理的bean的作用域
http://blog.csdn.net/yerenyuan_pku/article/details/52833477 Spring管理的bean的作用域有: singleton 在每个Spring ...
- docker使用阿里云镜像加速器(属于自己的专属加速器)
https://cr.console.aliyun.com/cn-shanghai/mirrors
- Python3简明教程(十)—— 异常
在本节我们学习 Python 的异常以及如何在你的代码中处理它们. 异常 在程序执行过程中发生的任何错误都是异常.每个异常显示一些相关的错误信息,比如你在 Python3 中使用 Python2 独有 ...
- dockerfile 的最佳实践
Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...