bzoj-5049-线段树
5039: [Jsoi2014]序列维护
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 323 Solved: 193
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7
Sample Output
35
8
【样例说明】
初始时数列为(1,2,3,4,5,6,7)。
经过第 1 次操作后,数列为(1,10,15,20,25,6,7)。
对第 2 次操作,和为 10+15+20=45,模 43 的结果是 2。
经过第 3 次操作后,数列为(1,10,24,29,34,15,16}
对第 4 次操作,和为 1+10+24=35,模 43 的结果是 35。
对第 5 次操作,和为 29+34+15+16=94,模 43 的结果是 8。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define inf 0x3f3f3f3f const int MAX=;
int N;
LL P;
struct Seg{
#define lc (id<<1)
#define rc (id<<1|1)
#define mid ((L+R)>>1)
LL sum[MAX<<];
LL mul[MAX<<],add[MAX<<];
void pushup(int id){
sum[id]=(sum[lc]+sum[rc])%P;
}
void pushdown(int id,int L,int R){
if(mul[id]==&&add[id]==) return;
add[lc]=(add[id]+mul[id]*add[lc])%P;
mul[lc]=mul[id]*mul[lc]%P;
sum[lc]=(sum[lc]*mul[id]%P+((LL)mid-L+)*add[id]%P)%P; add[rc]=(add[id]+mul[id]*add[rc])%P;
mul[rc]=(mul[rc]*mul[id])%P;
sum[rc]=(sum[rc]*mul[id]%P+((LL)R-mid)*add[id]%P)%P; add[id]=;
mul[id]=;
}
void build(int id,int L,int R){
mul[id]=;
add[id]=;
if(L==R){
scanf("%lld",&sum[id]);
sum[id]%=P;
return ;
}
build(lc,L,mid);
build(rc,mid+,R);
pushup(id);
}
void update1(int id,int L,int R,int l,int r,int v){
if(L>=l&&R<=r){
sum[id]=sum[id]*v%P;
mul[id]=mul[id]*v%P;
add[id]=add[id]*v%P;
return ;
}
pushdown(id,L,R);
if(l<=mid) update1(lc,L,mid,l,r,v);
if(r>mid) update1(rc,mid+,R,l,r,v);
pushup(id);
}
void update2(int id,int L,int R,int l,int r,int v){
if(L>=l&&R<=r){
add[id]+=v;
add[id]%=P;
sum[id]+=((LL)R-L+)*v;
sum[id]%=P;
return;
}
pushdown(id,L,R);
if(l<=mid) update2(lc,L,mid,l,r,v);
if(r>mid) update2(rc,mid+,R,l,r,v);
pushup(id);
}
LL query(int id,int L,int R,int l,int r){
if(L>=l&&R<=r){
return sum[id];
}
pushdown(id,L,R);
if(r<=mid) return query(lc,L,mid,l,r);
else if(l>mid) return query(rc,mid+,R,l,r);
else return (query(lc,L,mid,l,r)+query(rc,mid+,R,l,r))%P;
}
}ac;
int main(){
int i,j,op,t,g,m,c;
scanf("%d%lld",&N,&P);
ac.build(,,N);
scanf("%d",&m);
while(m--){
scanf("%d",&op);
if(op==){
scanf("%d%d%d",&t,&g,&c);
ac.update1(,,N,t,g,c);
}
else if(op==){
scanf("%d%d%d",&t,&g,&c);
ac.update2(,,N,t,g,c);
}
else{
scanf("%d%d",&t,&g);
printf("%lld\n",ac.query(,,N,t,g));
}
}
return ;
}
bzoj-5049-线段树的更多相关文章
- BZOJ 1798 (线段树||分块)的标记合并
我原来准备做方差的.. 结果发现不会维护两个标记.. 就是操作变成一个 a*x+b ,每次维护a , b 即可 加的时候a=1 ,b=v 乘的时候a=v ,b=0 #include <cstdi ...
- bzoj 3999 线段树区间提取 有序链剖
看错题目了,想成每个城市都可以买一个东西,然后在后面的某个城市卖掉,问最大收益.这个可以类似维护上升序列的方法在O(nlog^3n)的时间复杂度内搞定 这道题用到的一些方法: 1. 可以将有关的线段提 ...
- bzoj 3211 线段树
开方操作最多进行5次就可以把出现的任何数变成1. 所以用线段树暴力修改,以后修改时只需看一下是否当前区间都是0或1,如果是那么就直接返回. /***************************** ...
- bzoj 1018 线段树维护连通性
本题将一道LCT的题特殊化(支持加边和删边,询问图的连通性),将图变成了2×m的网格图,然后就神奇地可以用线段树来维护. 对于每个区间[l,r],维护其四个角落之间的连通性(仅仅通过[l,r]这段的边 ...
- bzoj 3212 线段树
裸的线段树 /************************************************************** Problem: User: BLADEVIL Langua ...
- bzoj 2120 线段树套平衡树
先吐下槽,改了快一个小时,最后发现是SBT的delete写错了,顿时就有想死的心..... 首先对于这道题,我们应该先做一下他的小问题,bzoj1878,虽然和这道题几乎一点关系没有, 但是能给我们一 ...
- bzoj 1901 线段树套平衡树+二分答案查询
我们就建一颗线段树,线段树的每一个节点都是一颗平衡树,对于每个询问来说,我们就二分答案, 查询每个二分到的mid在这个区间里的rank,然后就行了 /************************* ...
- BZOJ 1012 线段树||单调队列
非常裸的线段树 || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...
- BZOJ 3681 线段树合并+网络流
思路: 暴力建图有n*m条边 考虑怎么优化 (那就只能加个线段树了呗) 然后我就不会写了..... 抄了一波题解 //By SiriusRen #include <bits/stdc++.h&g ...
- BZOJ 4756 线段树合并(线段树)
思路: 1.最裸的线段树合并 2. 我们可以观察到子树求一个东西 那我们直接DFS序好了 入队的时候统计一下有多少比他大的 出的时候统计一下 减一下 搞定~ 线段树合并代码: //By SiriusR ...
随机推荐
- SQL Server使用 LEFT JOIN ON LIKE进行数据关联查询
这是来新公司写的第一篇文章,使用LEFT JOIN ON LIKE处理一下这种问题: SQL视图代码如下: CREATE View [dbo].[VI_SearchCN] AS --搜索产品的文件 ( ...
- ArrayList遍历的三种方式 array arrayList转换
ArrayList遍历的三种方式 - 呵呵静 - 博客园 https://www.cnblogs.com/mjyung/p/6725182.html
- rpyc
import json import socket from thread import * from ansible_api import * from rpyc import Service fr ...
- centos 搭建php环境
安装Apache1.安装yum -y install httpd2.开启apache服务systemctl start httpd.service3.设置apache服务开机启动systemctl e ...
- 二.re库介绍
一.re库的主要功能函数 1.re.search()用法 2.re.match()的用法 >>> match=re.match(r'[1-9]\d{5}','BIT 100081') ...
- Drawing Graphs using Dot and Graphviz
Drawing Graphs using Dot and Graphviz Table of Contents 1. License 2. Introduction 2.1. What is DOT? ...
- PAT 1061 Dating[简单]
1061 Dating(20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4 ...
- 自动化测试中级篇——LazyAndroid UI自动化测试框架使用指南
原文地址https://blog.csdn.net/iamhuanggua/article/details/53104345 简介 一直以来,安卓UI自动化测试都存在以下两个障碍,一是测试工具Mo ...
- Django:学习笔记(9)——视图
Django:学习笔记(9)——视图 基础视图 基于函数的视图,我们需要在使用条件语句来判断请求类型,并分支处理.但是在基于类的视图中,我们可以在类中定义不同请求类型的方法来处理相对应的请求. 基于函 ...
- AVAudioFoundation(3):音视频编辑
本文转自:AVAudioFoundation(3):音视频编辑 | www.samirchen.com 本文主要内容来自 AVFoundation Programming Guide. 音视频编辑 上 ...