[CC-STREETTA]The Street

题目大意:

给定两个长度为\(n(n\le10^9)\)的数列\(A\)和\(B\),开始数列\(A\)中每一项值为\(-\infty\),数列\(B\)中每一项值为\(0\)。\(m(m\le3\times10^5)\)次操作,操作包含以下\(3\)种:

  1. 数列\(A\)区间加一条等差数列。
  2. 数列\(B\)区间对一个等差数列取\(\max\)。
  3. 询问\(A_i+B_i\)。

思路:

每个结点维护一个解析式\(kx+b\)。

对于数列\(A\),使用李超树维护最大值。

对于数列\(B\),直接合并两个解析式。

时间复杂度\(\mathcal O(m\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
typedef long long int64;
const int SIZE=9e6;
struct Node {
int64 a,b;
void operator += (const Node &rhs) {
a+=rhs.a;
b+=rhs.b;
}
};
int64 calc(const Node &s,const int &x) {
return s.a*x+s.b;
}
class SegmentTree1 {
#define mid ((b+e)>>1)
private:
Node node[SIZE];
int left[SIZE],right[SIZE];
int sz,new_node() {
node[++sz]=(Node){0,LLONG_MIN};
return sz;
}
public:
int root;
void modify(int &p,const int &b,const int &e,const int &l,const int &r,Node s) {
p=p?:new_node();
if(b==l&&e==r) {
if(calc(node[p],b)<calc(s,b)) std::swap(node[p],s);
if(calc(node[p],e)>=calc(s,e)) return;
const double c=1.*(s.b-node[p].b)/(node[p].a-s.a);
if(c<=mid) {
modify(left[p],b,mid,b,mid,node[p]);
node[p]=s;
}
if(c>mid) modify(right[p],mid+1,e,mid+1,e,s);
return;
}
if(l<=mid) modify(left[p],b,mid,l,std::min(mid,r),s);
if(r>mid) modify(right[p],mid+1,e,std::max(mid+1,l),r,s);
}
int64 query(int &p,const int &b,const int &e,const int &x) {
p=p?:new_node();
int64 ret=calc(node[p],x);
if(b==e) return ret;
if(x<=mid) ret=std::max(ret,query(left[p],b,mid,x));
if(x>mid) ret=std::max(ret,query(right[p],mid+1,e,x));
return ret;
}
#undef mid
};
SegmentTree1 t1;
class SegmentTree2 {
#define mid ((b+e)>>1)
private:
Node node[SIZE];
int left[SIZE],right[SIZE];
int sz,new_node() {
return ++sz;
}
public:
int root;
void modify(int &p,const int &b,const int &e,const int &l,const int &r,const Node &s) {
p=p?:new_node();
if(b==l&&e==r) {
node[p]+=s;
return;
}
if(l<=mid) modify(left[p],b,mid,l,std::min(mid,r),s);
if(r>mid) modify(right[p],mid+1,e,std::max(mid+1,l),r,s);
}
int64 query(int &p,const int &b,const int &e,const int &x) {
p=p?:new_node();
int64 ret=calc(node[p],x);
if(b==e) return ret;
if(x<=mid) ret+=query(left[p],b,mid,x);
if(x>mid) ret+=query(right[p],mid+1,e,x);
return ret;
}
#undef mid
};
SegmentTree2 t2;
int main() {
const int n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int opt=getint();
if(opt==1) {
const int u=getint(),v=getint(),a=getint(),b=getint();
t1.modify(t1.root,1,n,u,v,(Node){a,b-(int64)u*a});
}
if(opt==2) {
const int u=getint(),v=getint(),a=getint(),b=getint();
t2.modify(t2.root,1,n,u,v,(Node){a,b-(int64)u*a});
}
if(opt==3) {
const int i=getint();
const int64 a=t1.query(t1.root,1,n,i),b=t2.query(t2.root,1,n,i);
if(a==LLONG_MIN) {
puts("NA");
} else {
printf("%lld\n",a+b);
}
}
}
return 0;
}

[CC-STREETTA]The Street的更多相关文章

  1. [CodeChef - STREETTA] The Street 李超线段树

    大致题意: 给出两个序列A,B,A初始为负无穷,B初始为0,有三种操作 1.在A上区间[u,v]上加一个等差数列,取与原本A序列的最大值. 2.在B上区间[u,v]上加一个等差数列. 3.给出一个点X ...

  2. hive 下篇

    由于spark on hive 问题,导致无法插入数据,暂时使用spark进行hive操作 向分区表插入数据 hive> show partitions customers;OKpartitio ...

  3. Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器

    1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set. 体验spring: 1.创建java项目. 2.引入spring ...

  4. Codechef March Challenge 2014——The Street

    The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...

  5. Atitti.dw cc 2015 绿色版本安装总结

    Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...

  6. 【Hello CC.NET】CC.NET 实现自动化集成

    一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...

  7. 浅谈iptables防SYN Flood攻击和CC攻击

    ------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...

  8. 申请邓白氏编码的时候总是提示 Enter a valid Street Address 怎么办?

    今天要申请一个苹果开发者公司(Company)账号,然后需要邓白氏编码,然后填写企业的基本信息.其中对于Street Address认真的对照着中文翻译为如下格式: Kang Hesheng buil ...

  9. checking for fcc ....no checking for cc .. no

         源码编译,提示缺少gcc cc cl.exe 解决方案:       yum install -y gcc glibc

  10. 编译器 cc、gcc、g++、CC 的区别

    gcc 是GNU Compiler Collection,原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C.C++.Objective-C.Ada.Fort ...

随机推荐

  1. Tensorflow中使用TFRecords高效读取数据--结合Attention-over-Attention Neural Network for Reading Comprehension

    原文链接:https://arxiv.org/pdf/1607.04423.pdf 本片论文主要讲了Attention Model在完形填空类的阅读理解上的应用. 转载:https://blog.cs ...

  2. Django框架<一>

    Django框架 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sess ...

  3. elk系列2之multiline模块的使用【转】

    preface 上回说道了elk的安装以及kibana的简单搜索语法,还有logstash的input,output的语法,但是我们在使用中发现了一个问题,我们知道,elk是每一行为一个事件,像Jav ...

  4. springboot使用fastJson作为json解析框架

    springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...

  5. RestTemplate OR Spring Cloud Feign 上传文件

    SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...

  6. 防范XSS跨站

    所有jsp页面输出全部使用<c:out value="{}"/> 默认就是escapeXml="true" java中间件,<c:out /& ...

  7. Caffe学习系列(8):solver,train_val.prototxt,deploy.prototxt及其配置

    solver是caffe的核心. net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_inter ...

  8. [转载]DirectX SDK (June 2010)安装错误S1023,解决方法

    导致这个错误的原因是在安装DirectX SDK (June 2010)之前.我就安装了VS2010 . 所以也就安装了: Microsoft Visual C++ 2010 x86 Redistri ...

  9. Ubuntu16.4 修改静态ip地址

    root@temple-102:~# ifconfig eno1 Link encap:Ethernet HWaddr 0c:c4:7a:e6:49:74 UP BROADCAST MULTICAST ...

  10. webstorm自动压缩js、css、html【工具篇】

    *注意:自动压缩的文件只能在同级目录下,不能指定文件夹,强制了文件自动保存,设置的手动保存将失效. 插件下载地址:点击这里下载 密码:e6bk 使用方法: 1.css&js 分别添加这两个,c ...