The Street Problem Code: STREETTA

https://www.codechef.com/problems/STREETTA

Submit

All submissions for this problem are available.

Read problems statements in Mandarin Chineseand Russian.

The String street is known as the busiest street in Codeland.
Tourists from all over the world want to visit the street once they are in Codeland.
The Chef owns N souvenir stores across the street (numbered from 1 to N).
At the beginning there is no souvenir in any store, the Chef has some plans to add some new items.
Each the Chef's plan is represented by 4 numbers: u v a b which mean an items with price b
is added to the store u, an items with price a + b is added to the store u + 1 and so on.
More formally, an item with price a * i + b is added to the store u + i for all (0 ≤ i ≤ v - u).

In additional to the cost of the item itself, the tourist must pay some conservation fees as well.
The Codeland regularly defines the new conservation fee. Each fee is represented by 4 numbers: u v a b which means
the tourist buying any item in the store u + i will be charged a fee of i * a + b for all (0 ≤ i ≤ v - u).
In the case that several conservation fees have effect on the same store, the customer needs to pay all of those fees.

At some point of time, a tourist at store i asks you what is the largest amount of money they have to spend for
a souvenir at that store (the amount of money includes the price of one of the souvenirs and all the conservation fees for that store).

Input

  • The first line of the input contains two integers N and M represent the number of stores and the number of events
  • Each of the next M lines represents an event of three types below in the chronological order.
    • The new plan of the Chef: "1 u v a b".
    • The new conservation fee: "2 u v a b".
    • The query from tourist: "3 i".

Output

For each query from tourist, print in one line the corresponding answer.
If there is no item at the ith store, print out "NA" (without quotes) as the answer.

Constraints

  • 1 ≤ N ≤ 109
  • 1 ≤ M ≤ 3*105
  • For events of type 1: 1 ≤ u ≤ v ≤ N. |a|, |b| ≤ 109
  • For events of type 2: 1 ≤ u ≤ v ≤ N. |a|, |b| ≤ 104
  • For events of type 3: 1 ≤ i ≤ N

Example

Input:
10 10
3 5
1 3 8 3 1
3 5
1 5 10 -8 2
3 5
3 10
2 1 10 0 1
3 6
2 5 7 2 1
3 6 Output:
NA
7
7
-38
11
14 题意:
操作1:a数组的[l,r]对一个等差数列取大
操作2:给b数组的[l,r]加一个等差数列
操作3:询问 ai+bi 数据范围:10^9,所以线段树动态开点
等差数列最大值/和:一次函数
对于操作2,线段树维护a的和,b的和,当加入ax+b时,直接加上就好
对于操作1,分3种情况:
1、区间本没有等差数列,直接加上
2、区间原有的等差数列与现在的等差数列在区间内无交点,用大的直接覆盖
3、区间原有的等差数列与现在的等差数列在区间内有交点,,那么一定有一个等差数列只能对当前区间的一半区间有影响,
下传这个对一半区间有影响的等差数列,本区间保留另一个等差数列
#include<cstdio>
#include<algorithm>
#define INF (1LL<<62)
#define N 300001
using namespace std;
struct node
{
long long a,b,end;
bool have1,cross1;
bool have2,cross2;
long long maxna,maxnb;
}tr[N*];;
int n,m,lc[N*],rc[N*],tot,cnt;
int op,opl,opr,root;
long long ans1,ans2,A,B;
void flag(int k,int who,int l,int r);
long long read()
{
long long x=,f=; char c=getchar();
while(c<''||c>'') { if(c=='-') f=-; c=getchar(); }
while(c>=''&&c<='') { x=x*+c-'' ; c=getchar(); }
return x*f;
}
struct TREE
{
void add(int &k,int l,int r,long long a,long long b)
{
if(!k)
{
k=++tot;
tr[k].maxnb=-INF;
}
tr[k].cross1=true;
if(l>=opl&&r<=opr)
{
tr[k].b+=b; tr[k].a+=a;
tr[k].have1=true;
return;
}
int mid=l+r>>;
if(opl<=mid) add(lc[k],l,mid,a,b);
if(opr>mid) add(rc[k],mid+,r,a,A*(mid+-opl+-)+B);
}
double meet(long long a1,long long b1,long long a2,long long b2)
{
if(a1!=a2) return 1.0*(b2-b1)/(a1-a2);
return ;
}
void down(int &k,long long aa,long long bb,int l,int r)
{
flag(lc[k],aa,bb,l,l+r>>);
flag(rc[k],aa,aa*((l+r>>)+-l)+bb,(l+r>>)+,r);
}
void flag(int &k,long long aa,long long bb,int l,int r)
{
if(!k)
{
k=++tot;
tr[k].maxnb=-INF;
}
tr[k].cross2=true;
tr[k].have2=true;
if(l==r)
{
tr[k].maxnb=max(tr[k].maxnb,bb);
return;
}
if(!tr[k].maxna&&!tr[k].maxnb) { tr[k].maxna=aa; tr[k].maxnb=bb; return; }
long long prea=tr[k].maxna,preb=tr[k].maxnb;
long long preend=prea*(r-l+-)+preb;
long long nowend=aa*(r-l+-)+bb;
if(preb>=bb&&preend>=nowend) return;
if(preb<=bb&&preend<=nowend) { tr[k].maxna=aa; tr[k].maxnb=bb; return; }
double point=meet(tr[k].maxna,tr[k].maxnb,aa,bb);
point+=l;
if(point<1.0*(l+r>>))
{
if(preb>bb)
{
down(k,prea,preb,l,r);
tr[k].maxna=aa; tr[k].maxnb=bb;
}
else down(k,aa,bb,l,r);
}
else
{
if(preend>nowend)
{
down(k,prea,preb,l,r);
tr[k].maxna=aa; tr[k].maxnb=bb;
}
else down(k,aa,bb,l,r);
}
}
void maxn(int &k,long long aa,long long bb,int l,int r)
{
if(!k)
{
k=++tot;
tr[k].maxnb=-INF;
}
tr[k].cross2=true;
if(l>=opl&&r<=opr)
{
flag(k,aa,bb,l,r);
return;
}
if(tr[k].have2) down(k,tr[k].maxna,tr[k].maxnb,l,r);
int mid=l+r>>;
if(opl<=mid) maxn(lc[k],aa,bb,l,mid);
if(opr>mid) maxn(rc[k],aa,(mid+-opl+-)*A+B,mid+,r);
}
void query1(int k,int l,int r)
{
if(tr[k].have1) ans1+=tr[k].a*(opl-l+-)+tr[k].b;
if(!tr[lc[k]].cross1&&!tr[rc[k]].cross1) return;
int mid=l+r>>;
if(opl<=mid&&tr[lc[k]].cross1) query1(lc[k],l,mid);
else if(opl>mid&&tr[rc[k]].cross1) query1(rc[k],mid+,r);
}
void query2(int k,int l,int r)
{
if(tr[k].have2) ans2=max(ans2,tr[k].maxna*(opl-l+-)+tr[k].maxnb);
if(!tr[lc[k]].cross2&&!tr[rc[k]].cross2) return;
if(tr[k].have2) down(k,tr[k].maxna,tr[k].maxnb,l,r);
int mid=l+r>>;
if(opl<=mid&&tr[lc[k]].cross2) query2(lc[k],l,mid);
else if(opl>mid&&tr[rc[k]].cross2) query2(rc[k],mid+,r);
}
}Tree;
int main()
{
n=read(); m=read();
while(m--)
{
op=read();
if(op==)
{
opl=read(); opr=read(); A=read(); B=read();
Tree.maxn(root,A,B,,n);
}
else if(op==)
{
opl=read(); opr=read(); A=read(); B=read();
Tree.add(root,,n,A,B);
}
else
{
scanf("%d",&opl);
ans2=-INF; Tree.query2(root,,n);
if(ans2==-INF) { puts("NA"); continue; }
ans1=; Tree.query1(root,,n);
printf("%lld\n",ans1+ans2);
}
}
}

三大错误:

1、当前函数下传时,右区间首项为(mid+1-1)*a+b

mid+1时右区间第一个,再-1因为等差数列首项为 a*0+b

2、初始化:结构体内maxnb初始值为0,应该是无穷小,因为a,b可能为负数

无穷小:-2e15 会WA,直接 1LL<<62

3、down函数里下传k的等差数列后,k不能清0,因为k可能还有一个等差数列

Codechef March Challenge 2014——The Street的更多相关文章

  1. AC日记——The Street codechef March challenge 2014

    The Street 思路: 动态开节点线段树: 等差序列求和于取大,是两个独立的子问题: 所以,建两颗线段树分开维护: 求和:等差数列的首项和公差直接相加即可: 取大: 对于线段树每个节点储存一条斜 ...

  2. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  3. 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu

    https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...

  4. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  5. 刷漆(Codechef October Challenge 2014:Remy paints the fence)

    [问题描述] Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏, ...

  6. [Codechef October Challenge 2014]刷漆

    问题描述 Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏,木板 ...

  7. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  8. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...

  9. CODECHEF Oct. Challenge 2014 Children Trips

    @(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...

随机推荐

  1. JavaScript函数与对象

    函数 函数的定义 JavaScript中的函数和Python中的非常类似,只是定义方式有点区别. // 普通函数定义 function f1() { console.log("Hello w ...

  2. 【BZOJ2333】棘手的操作(左偏树,STL)

    [BZOJ2333]棘手的操作(左偏树,STL) 题面 BZOJ上看把... 题解 正如这题的题号 我只能\(2333\) 神TM棘手的题目... 前面的单点/联通块操作 很显然是一个左偏树+标记 ( ...

  3. Js - JQ事件委托( 适用于给动态生成的脚本元素添加事件)

    最近一段时间打了一个大仗,现在总算消停点,才有时间来做个总结吧算是: 移动端遇到一个项目,是一个列表的侧滑栏,在我这里用jq写的交互事件.自测各方面都挺好的,美滋滋的给了研发.研发也美滋滋的开始开发. ...

  4. wireshark抓包看ECN

    由于实验需要,要统计ECN信息.为了验证拓扑中是否真的有ECN信息,用了wireshark进行抓包查看. 网上找到的相关有用资料有:http://blog.csdn.net/u011414200/ar ...

  5. SFTP环境搭建及客户代码调用公共方法封装

    一.背景 在开发应用软件的过程中,广泛使用FTP在各子系统间传送文本数据.但FTP存在安全问题,开放到外网存在安全漏洞,容易被攻击.替换方案是使用SFTP,SFTP提供更高的安全性,当然传输的效率也会 ...

  6. python PEP8相关介绍

    在学习了python相关技术之后,开始重视其开发规范,以满足代码的可读性以及可维护性.主要的是学习了PEP8-style for python code的相关内容. 代码布局 缩进:每一级4个缩进.连 ...

  7. JavaScript中的私有成员[翻译]

    原作者:Douglas Crockford,原文地址:http://www.crockford.com/javascript/private.html JavaScript 是世界上被误解最深的编程语 ...

  8. Kafka最佳实践

    一.硬件考量 1.1.内存 不建议为kafka分配超过5g的heap,因为会消耗28-30g的文件系统缓存,而是考虑为kafka的读写预留充足的buffer.Buffer大小的快速计算方法是平均磁盘写 ...

  9. [转]【安卓笔记】AsyncTask源码剖析

    [转][安卓笔记]AsyncTask源码剖析 http://blog.csdn.net/chdjj/article/details/39122547 前言: 初学AsyncTask时,就想研究下它的实 ...

  10. .NET Core开源API网关 – Ocelot中文文档

    Ocelot是一个用.NET Core实现并且开源的API网关,它功能强大,包括了:路由.请求聚合.服务发现.认证.鉴权.限流熔断.并内置了负载均衡器与Service Fabric.Butterfly ...