AtCoder square869120 Contest #3 F sushi
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
题目链接:sushi
正解:线段树
解题报告:
考虑$a[i]>=a[i+1]$恒成立,那么序列为一段段相等的数,类似于灌水的问题。
那么用线段树维护每个点的权值,不难发现,每次$add$操作,应该是先填平差距,有剩余的话就整体上升,余下的部分补给一段前缀。
那么只需要用线段树来维护就好了,在线段树上维护$set$和$add$标记,$pushdown$的时候注意先后顺序。
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <ctime>
#define lc root<<1
#define rc root<<1|1
using namespace std;
typedef long long LL;
const int MAXN = 200011;
int n,Q;
struct node{ LL set,add,sum; }a[MAXN*3];
inline void update(int root){ a[root].sum=a[lc].sum+a[rc].sum; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void pushdown(int root,int l,int r){
if(a[root].set==0 && a[root].add==0) return ; if(l==r) return ;
int mid=(l+r)>>1;
if(a[root].set>0) {
a[lc].set=a[rc].set=a[root].set;
a[lc].sum=1LL*a[root].set*(mid-l+1); a[rc].sum=1LL*a[root].set*(r-mid);
a[lc].add=a[rc].add=a[root].set=0;
} if(a[root].add>0) {
a[lc].add+=a[root].add; a[rc].add+=a[root].add;
a[lc].sum+=1LL*a[root].add*(mid-l+1); a[rc].sum+=1LL*a[root].add*(r-mid);
a[root].add=0;
}
} inline void modify(int root,int l,int r,int ql,int qr,LL val){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
a[root].set=val; a[root].add=0;
a[root].sum=1LL*(r-l+1)*val;
return ;
}
int mid=(l+r)>>1;
if(ql<=mid) modify(lc,l,mid,ql,qr,val);
if(qr>mid) modify(rc,mid+1,r,ql,qr,val);
update(root);
} inline void add(int root,int l,int r,int ql,int qr,LL val){
pushdown(root,l,r);
if(ql<=l && r<=qr) {
a[root].sum+=1LL*val*(r-l+1);
a[root].add+=val;
return ;
}
int mid=(l+r)>>1;
if(ql<=mid) add(lc,l,mid,ql,qr,val);
if(qr>mid) add(rc,mid+1,r,ql,qr,val);
update(root);
} inline LL query(int root,int l,int r,int ql,int qr){
if(ql<=l && r<=qr) return a[root].sum;
pushdown(root,l,r); int mid=(l+r)>>1;
if(qr<=mid) return query(lc,l,mid,ql,qr);
else if(ql>mid) return query(rc,mid+1,r,ql,qr);
return query(lc,l,mid,ql,qr)+query(rc,mid+1,r,ql,qr);
} inline void dfs(int root,int l,int r){
if(l==r) { printf("%lld\n",a[root].sum); return ; }
pushdown(root,l,r); int mid=(l+r)>>1;
dfs(lc,l,mid); dfs(rc,mid+1,r);
} inline void work(){
n=getint(); Q=getint(); int x; LL y; int l,r,mid,pos;
while(Q--) {
x=getint(); scanf("%lld",&y);
if(x==1) { add(1,1,n,1,1,y); continue; }
l=1; r=x; pos=0;
while(l<=r) {
mid=(l+r)>>1;
if(1LL*query(1,1,n,mid,mid)*(x-mid+1)-query(1,1,n,mid,x) <= y) r=mid-1;
else l=mid+1,pos=mid;
}
y-=1LL*query(1,1,n,pos+1,pos+1)*(x-pos)-query(1,1,n,pos+1,x);
modify(1,1,n,pos+1,x, query(1,1,n,pos+1,pos+1) );
if(y/(x-pos)) add(1,1,n,pos+1,x,y/(x-pos));
if(y%(x-pos)) add(1,1,n,pos+1,pos+y%(x-pos),1);
}
dfs(1,1,n);
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
AtCoder square869120 Contest #3 F sushi的更多相关文章
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Grand Contest 002 F:Leftmost Ball
题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...
- AtCoder Grand Contest 017 F - Zigzag
题目传送门:https://agc017.contest.atcoder.jp/tasks/agc017_f 题目大意: 找出\(m\)个长度为\(n\)的二进制数,定义两个二进制数的大小关系如下:若 ...
- AtCoder Regular Contest 074 F - Lotus Leaves
题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...
- AtCoder Grand Contest 003 F - Fraction of Fractal
题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_f 题目大意: 给定一个\(H×W\)的黑白网格,保证黑格四连通且至少有一个黑格 定义分形如下 ...
- AtCoder Grand Contest 011 F - Train Service Planning
题目传送门:https://agc011.contest.atcoder.jp/tasks/agc011_f 题目大意: 现有一条铁路,铁路分为\(1\sim n\)个区间和\(0\sim n\)个站 ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- AtCoder Regular Contest 081 F - Flip and Rectangles
题目传送门:https://arc081.contest.atcoder.jp/tasks/arc081_d 题目大意: 给定一个\(n×m\)的棋盘,棋盘上有一些黑点和白点,每次你可以选择一行或一列 ...
随机推荐
- orm之路由层
一.简单配置 1.参数 第一个参数是正则表达式(如果要精准匹配:‘^publish/$’),或者加斜杠('^publish/') 第二个参数是视图函数(不要加括号) urlpatterns = [ u ...
- SDUT1157:小鼠迷宫问题(bfs+dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1157 题目描述 小鼠a与小鼠b身处一个m×n的 ...
- hdu3339In Action(最短路+01背包)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...
- 安恒X计划12月月赛
ezweb 主要是序列化问题.没有PHP环境,在线运行的.实例化对象之后修改一下file.然后echo输出序列化的结果.不过下面有一个正则检查.数字前加一个+,影响了正则的匹配,但是对于序列化的还原没 ...
- js随机点名系统
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 5059 Help him(简单模拟题)
http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那 ...
- OAuth 白话简明教程 3.客户端模式(Client Credentials)
转自:http://www.cftea.com/c/2016/11/6704.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
- Webform和MVC,为什么MVC更好一些?(转)
转自http://www.admin10000.com/document/5277.html 前言 如果你看了最近微软的议程,你会发现他们现在的焦点除了MVC,还是MVC.问题在于为什么微软如此热衷于 ...
- SVA描述(一)
SystemVerilog Assertion(SVA):是一种描述性的语言,可以很容易的描述时序相关的情况,所以主要用在协议检查和协议覆盖.SVA在systemverilog仿真器中的 调度区间在R ...
- VS2010/MFC编程入门之四十五(MFC常用类:CFile文件操作类)
上一节中鸡啄米讲了定时器Timer的用法,本节介绍下文件操作类CFile类的使用. CFile类概述 如果你学过C语言,应该知道文件操作使用的是文件指针,通过文件指针实现对它指向的文件的各种操作.这些 ...