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分治的更多相关文章

  1. hdu 1698 (延迟标记+区间修改+区间求和)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  2. 【树状数组区间修改区间求和】codevs 1082 线段树练习 3

    http://codevs.cn/problem/1082/ [AC] #include<bits/stdc++.h> using namespace std; typedef long ...

  3. 【codevs1690】开关灯 线段树 区间修改+区间求和(标记)

    [codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...

  4. 【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))

    [codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...

  5. bzoj2631 tree LCT 区间修改,求和

    tree Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 4962  Solved: 1697[Submit][Status][Discuss] Des ...

  6. 【vijos1659】河蟹王国 线段树<区间修改+区间最大值>

    描述 河蟹王国有一位河蟹国王,他的名字叫羊驼.河蟹王国富饶安定,人们和谐相处.有一天,羊驼国王心血来潮,想在一部分人中挑出最和谐的人.于是,羊驼国王将他的子民排成了一列(==!!b汗~好长呀).每个人 ...

  7. 【codevs】1082 线段树练习 3 <区间修改+区间和>

    题目连接   http://codevs.cn/problem/1082/ Description 给你N个数,有两种操作: 1:给区间[a,b]的所有数增加X 2:询问区间[a,b]的数的和. In ...

  8. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  9. BZOJ 1935 Tree 园丁的烦恼 CDQ分治/主席树

    CDQ分治版本 我们把询问拆成四个前缀和,也就是二维前缀和的表达式, 我们把所有操作放入一个序列中 操作1代表在x,y出现一个树 操作2代表加上在x,y内部树的个数 操作3代表减去在x,y内部树的个数 ...

随机推荐

  1. h5互动课件动画如何实现?如何快速开发h5互动课件动画

    最近几年随着h5的兴起,复杂的h5动画,甚至是交互动画类型的产品不断涌现,尤其在课件产品方面,很多公司都有相关需求,最近很多h5开发工程师想了解相关方面的技术. 针对h5,如果是简单的动画效果,可以考 ...

  2. linux修改root密码

    或者是:sudo passwd root   提示输入新的密码.再确认输入一次密码回车,就可以完成root密码的修改.     更改成功,以后就用这个新的密码登陆到Linux系统中去

  3. JVM-垃圾收集算法

    新生代:复制算法,IBM研究过,约98%对象是“朝生夕死”,所以不需要按照1:1来划分:HotSpot中默认是8:1:1,即每次新生代可用空间为90%,只有10%会被“浪费”, 当Survivor空间 ...

  4. Oracle 巡检命令

    [grid@XXXXX ~]$ asmcmd -pASMCMD [+] > lsdg

  5. python科学计算包numpy用法(一)

    numpy是python中一个用来做科学计算的包,用起来十分方便,下面是我总结的numpy的用法: 1.如何创建矩阵 创建矩阵有很多种方法,主要包括以下几种: 通过array函数创建 >> ...

  6. Ajax异步请求阻塞情况的解决办法(asp.net MVC Session锁的问题)

    讨论今天这个问题之前,我们先来看下浏览器公布的资源并发数限制个数,如下图 不难看出,目前主流浏览器支持都是最多6个并发 需要注意的是,浏览器的并发请求数目限制是针对同一域名的 意即,同一时间针对同一域 ...

  7. Python 网站后台扫描脚本

    Python  网站后台扫描脚本 #!/usr/bin/python #coding=utf-8 import sys import urllib import time url = "ht ...

  8. eclipse--常见问题

    学习java的都知道这个编辑器,但这个编辑器的有很多功能很多人不知道怎么用,我系统的整理一下我学习过程中遇到的过的问题 1.eclipse如何导入external jar包?        参考:ht ...

  9. mybatis关于ORM的使用以及设计(一)[ORM的初始化]

    ORM WIKI中的解释.画重点 Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is ...

  10. Ehcart整合百度地图

    最近上班有些时间,学习了一下Ehcart的知识,自己制作了一份Ehcart整合百度地图的示例代码. GItHub地址:https://github.com/TianYanFd/tianjin-powe ...