区间修改区间求和cdq分治
https://www.luogu.org/problemnew/show/P3372
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&(-x))
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define lc (d<<1)
#define rc (d<<1|1)
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e5+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
using namespace std;
struct node{
int type,x,st;
LL v;
bool operator<(const node &t) const{
return x==t.x?type<t.type:x<t.x;
}
}q[maxn<<],temp[maxn<<];
LL ans[maxn];
LL pre[maxn];
void cdq(int l,int r){
if(l==r) return ;
//cout<<l<<' '<<r<<endl;
int mid=(l+r)>>;
cdq(l,mid);cdq(mid+,r);
int i=l,j=mid+,p=l;
LL now=,val=;
int last=;
while(i<=mid||j<=r){
if(j>r||(i<=mid&&q[i]<q[j]) ){
now+=(q[i].x-last)*val; last=q[i].x;
if(q[i].type==) val+=q[i].v;
temp[p++]=q[i++];
}
else{
now+=(q[j].x-last)*val; last=q[j].x;
if(q[j].type==) ans[q[j].st]+= now*q[j].v;
temp[p++]=q[j++];
}
}
for(int i=l;i<=r;i++) q[i]=temp[i];
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
LL xx;
for(int i=;i<=n;i++){
scanf("%lld",&xx);
pre[i]=pre[i-]+xx;
}
int opt,x,y;
int st=;
int tot=;
LL v;
for(int i=;i<=m;i++){
scanf("%d",&opt);
if(opt==){
scanf("%d%d%lld",&x,&y,&v);
q[++tot].x=x-;q[tot].v=v;q[tot].type=;
q[++tot].x=y;q[tot].v=-v;q[tot].type=;
}
else{
scanf("%d%d",&x,&y);
++st;
ans[st]=pre[y]-pre[x-];
q[++tot].x=x-;q[tot].v=-;q[tot].type=;q[tot].st=st;
q[++tot].x=y;q[tot].v=;q[tot].type=;q[tot].st=st;
}
}
cdq(,tot);
for(int i=;i<=st;i++) printf("%lld\n",ans[i]);
}
区间修改区间求和cdq分治的更多相关文章
- hdu 1698 (延迟标记+区间修改+区间求和)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- 【树状数组区间修改区间求和】codevs 1082 线段树练习 3
http://codevs.cn/problem/1082/ [AC] #include<bits/stdc++.h> using namespace std; typedef long ...
- 【codevs1690】开关灯 线段树 区间修改+区间求和(标记)
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- 【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- bzoj2631 tree LCT 区间修改,求和
tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 4962 Solved: 1697[Submit][Status][Discuss] Des ...
- 【vijos1659】河蟹王国 线段树<区间修改+区间最大值>
描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...
- 【codevs】1082 线段树练习 3 <区间修改+区间和>
题目连接 http://codevs.cn/problem/1082/ Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. In ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- BZOJ 1935 Tree 园丁的烦恼 CDQ分治/主席树
CDQ分治版本 我们把询问拆成四个前缀和,也就是二维前缀和的表达式, 我们把所有操作放入一个序列中 操作1代表在x,y出现一个树 操作2代表加上在x,y内部树的个数 操作3代表减去在x,y内部树的个数 ...
随机推荐
- 【转】Mysql索引设计原则
来源:https://segmentfault.com/a/1190000000473085 假设一高频查询如下SELECT * FROM user WHERE area='amoy' AND sex ...
- Ansible运维自动化
Ansible运维自动化 一.Ansible-playbook的初步使用 playbook的使用,playbook可以把ansible的模块进行组合 ln -s /usr/local/python/b ...
- Ubuntu 15.10 下Redis Cluster使用
1 Redis Standalone安装 可以参考这篇博文:http://www.cnblogs.com/_popc/p/3684835.html 2 Redis Cluster安装 2.1 环境介绍 ...
- 存储过程中调用webservice
存储过程中调用webservice其实是在数据库中利用系统函数调用OLE. 1.查找webservice api 可得到MSSOAP.SoapClient. 2.查找API 接口可得到mssoapin ...
- P1041 传染病控制(dfs)
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- okhttp 解析respone:
android,retrofit,okhttp,日志拦截器,使用拦截器Interceptor统一打印请求与响应的json: https://blog.csdn.net/qq_37043246/arti ...
- spring事务[转]
https://www.cnblogs.com/cnmenglang/p/6410848.html 先了解事务的7种传播属性: PROPAGATION_REQUIRED -- 支持当前事务,如果当前没 ...
- DCDC电源 TPS54525
电路图: 输入电压12V,输出电压5V.不接负载可以获得5V输出,接入负载后只有4mV,且PG为低. 预计原因:可能是上电瞬间电流过大,芯片过流保护. 解决方案:SS软启动引脚的电容C331加大至1u ...
- IE8图片上传预览
$("#smallImg").attr('style', "filter:progid:DXImageTransform.Microsoft.AlphaImageLoad ...
- Python全栈开发记录_第九篇(面向对象(类)的学习)
有点时间没更新博客了,今天就开始学习类了,今天主要是面向对象(类),我们知道面向对象的三大特性,那就是封装,继承和多态.内容参考该博客https://www.cnblogs.com/wupeiqi/p ...