AC日记——The Street codechef March challenge 2014
思路:
动态开节点线段树;
等差序列求和于取大,是两个独立的子问题;
所以,建两颗线段树分开维护;
求和:等差数列的首项和公差直接相加即可;
取大:
对于线段树每个节点储存一条斜率为等差数列公差的线段;
当添加线段到已有线段的节点,下传一条线段,当前节点留下一条线段;
当要添加的线段完全覆盖或者被覆盖当前节点储存的线段时,选择更新或者不更新;
单点查询时,从根节点到叶节点的路径上去最大值;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define ll long long
#define INF (1LL<<62) struct TreeNodeType {
ll a,b; bool if_; struct TreeNodeType *lc,*rc; TreeNodeType()
{
lc=NULL,rc=NULL,if_=false,a=,b=;
}
}; ll n,m; inline void in(ll &now)
{
ll if_z=;now=;
char Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} class SumTreeType {
public:
struct TreeNodeType *root; SumTreeType(){} inline void tree_down(TreeNodeType *&now,ll l,ll r)
{
if(now->lc==NULL) now->lc=new TreeNodeType;
if(now->rc==NULL) now->rc=new TreeNodeType;
now->lc->a+=now->a,now->lc->b+=now->b;
now->rc->a+=now->a+((l+r>>)-l+)*now->b,now->rc->b+=now->b;
now->a=,now->b=;
} void tree_add(TreeNodeType *&now,ll l,ll r,ll li,ll ri,ll a,ll b)
{
if(now==NULL) now=new TreeNodeType;
if(l==li&&r==ri)
{
now->a+=a,now->b+=b;
return ;
}
ll mid=l+r>>;
if(now->a!=&&now->b!=) tree_down(now,l,r);
if(li>mid) tree_add(now->rc,mid+,r,li,ri,a,b);
else if(ri<=mid) tree_add(now->lc,l,mid,li,ri,a,b);
else
{
tree_add(now->lc,l,mid,li,mid,a,b);
tree_add(now->rc,mid+,r,mid+,ri,a+(mid-li+)*b,b);
}
} ll tree_query(TreeNodeType *&now,ll l,ll r,ll to)
{
if(now==NULL) return ;
if(l==r) return now->a;
if(now->a!=||now->b!=) tree_down(now,l,r);
ll mid=l+r>>;
if(to<=mid) return tree_query(now->lc,l,mid,to);
else return tree_query(now->rc,mid+,r,to);
}
};
class SumTreeType ai; class MaxTreeType {
public:
ll X; bool op; struct TreeNodeType *root; MaxTreeType(){} inline double com(ll a1,ll b1,ll a2,ll b2)
{
if(a1==a2) return ;
return (double)(a1-a2)/(double)(b2-b1);
} void tree_down(TreeNodeType *&now,ll l,ll r,ll a,ll b)
{
if(now==NULL)
{
now=new TreeNodeType;
now->if_=true;
now->a=a,now->b=b;
return ;
}
if(!now->if_)
{
now->a=a,now->b=b,now->if_=true;
return ;
}
double xx=com(now->a-l*now->b,now->b,a-l*b,b),mid=l+r>>;
if(xx<=l||xx>=r)
{
if((mid-l)*b+a>(mid-l)*now->b+now->a) now->a=a,now->b=b;
return ;
}
if(xx<=mid)
{
if(now->b<b)
{
tree_down(now->lc,l,mid,now->a,now->b);
now->a=a,now->b=b,now->if_=true;
}
else tree_down(now->lc,l,mid,a,b);
}
else
{
if(now->b<b) tree_down(now->rc,mid+,r,a+(mid-l+)*b,b);
else
{
tree_down(now->rc,mid+,r,now->a+(mid-l+)*now->b,now->b);
now->a=a,now->b=b,now->if_=true;
}
}
} void tree_add(TreeNodeType *&now,ll l,ll r,ll li,ll ri,ll a,ll b)
{
if(now==NULL) now=new TreeNodeType;
if(l==li&&r==ri)
{
if(!now->if_)
{
now->if_=true;
now->a=a,now->b=b;
}
else tree_down(now,l,r,a,b);
return ;
}
ll mid=l+r>>;
if(ri<=mid) tree_add(now->lc,l,mid,li,ri,a,b);
else if(li>mid) tree_add(now->rc,mid+,r,li,ri,a,b);
else
{
tree_add(now->lc,l,mid,li,mid,a,b);
tree_add(now->rc,mid+,r,mid+,ri,a+(mid-li+)*b,b);
}
} void tree_query(TreeNodeType *&now,ll l,ll r,ll to)
{
if(now==NULL) return ;
if(now->if_) X=max(X,now->a+(to-l)*now->b);
if(l==r) return ;
ll mid=l+r>>;
if(to<=mid) tree_query(now->lc,l,mid,to);
else tree_query(now->rc,mid+,r,to);
}
};
class MaxTreeType bi; int main()
{
in(n),in(m);ll op,u,v,a,b;
for(ll i=;i<=m;i++)
{
in(op);
if(op==)
{
in(u),in(v),in(b),in(a);
bi.tree_add(bi.root,,n,u,v,a,b);
}
else if(op==)
{
in(u),in(v),in(b),in(a);
ai.tree_add(ai.root,,n,u,v,a,b);
}
else if(op==)
{
in(u);
bi.X=-INF;
bi.tree_query(bi.root,,n,u);
if(bi.X==-INF) printf("NA\n");
else printf("%lld\n",bi.X+ai.tree_query(ai.root,,n,u));
}
}
return ;
}
AC日记——The Street codechef March challenge 2014的更多相关文章
- Codechef March Challenge 2014——The Street
The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submis ...
- codechef January Challenge 2014 Sereja and Graph
题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...
- 【分块+树状数组】codechef November Challenge 2014 .Chef and Churu
https://www.codechef.com/problems/FNCS [题意] [思路] 把n个函数分成√n块,预处理出每块中各个点(n个)被块中函数(√n个)覆盖的次数 查询时求前缀和,对于 ...
- CodeChef November Challenge 2014
重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...
- 刷漆(Codechef October Challenge 2014:Remy paints the fence)
[问题描述] Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏, ...
- [Codechef October Challenge 2014]刷漆
问题描述 Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏,木板 ...
- 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 ...
- 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; ...
- CODECHEF Oct. Challenge 2014 Children Trips
@(XSY)[分塊, 倍增] Description There's a new trend among Bytelandian schools. The "Byteland Tourist ...
随机推荐
- Android学习记录(7)—Intent中显示意图和隐式意图的用法
Intent(意图)主要是解决Android应用的各项组件之间的通讯. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的 ...
- Android FrameWork 概述
Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...
- 1090 Highest Price in Supply Chain (25 分)(树的遍历)
求所有叶节点中的最高价以及这个价格的叶节点个数 #include<bits/stdc++.h> using namespace std; ; vector<int>mp[N]; ...
- Python——数据类型之list、tuple
本篇主要内容 • list初识 • list元素的访问 • list内部所有的方法 • tuple介绍和与list用法的比较 我觉得Python里面用的最多的就是List了,感觉好强大.他能存 ...
- Learn the shell
learn the shell what is the shell? when we speak of the command line,we are really to the shell.Actu ...
- c++ devived object model
单一虚函数继承 class A{public: virtual int foo( ) { return val ; } virtual int funA( ) {}private: int val ; ...
- free、vmstat监视内存使用情况
9. free 查询可用内存 free工具用来查看系统可用内存: /opt/app/tdev1$free total used free shared buffers cached Mem: 8175 ...
- Codeforces Round #389 (Div. 2) 752E(二分答案)
题目大意 可以理解成有n个木板,可以选取木板将其劈成2半(如果长度是奇数,就切成x和x+1),切完之后还可以再切 然后你要把这n个木板切成更多的木板,然后从中选择k个,使得这k个木板的最小长度尽量大 ...
- 洛谷 P2634 [国家集训队]聪聪可可 解题报告
P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一 ...
- B-number(hdu 3652)
题意:找出1~n范围内含有13并且能被13整除的数字的个数 /* 数位DP dp[i][j][0]表示i位数模13为j当前没有包含13并且最高位不为1的方案数: dp[i][j][0]表示i位数模13 ...