区间修改区间求和cdq分治
2024-10-15 05:48:26
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]);
}
最新文章
- IDEA开发多项目的示例
- Sublime Text3快捷键汇总
- php中pdo例子
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- 查看Linus中自带的jdk ,设置JAVA_HOME
- 如何进行shell脚本正确性测试
- Socket异步通信学习二
- POJ 2923 Relocation
- Direct2D 几何计算和几何变幻
- HTML5 的WebSocket
- 闲聊select和input常用的小插件
- JAVA基础面试(四)
- ASP.NET MVC4通过UrlRewriter配置伪静态,支持html后缀
- 解决apache httpd列出目录列表中文乱码问题
- C风格字符串和C++string对象的相互转化
- 【Tomcat】Unrecognized Windows Sockets error: 10106: Socket creation failed
- Xcode调试LLDB
- Scrutiny of Partner&#39;s individual project Code
- document的全量替换、强制创建、删除
- java日志及异常错误信息输出的问题