Codeforces 627B Factory Repairs 线段树
来源:互联网 发布:淘宝9888 编辑:程序博客网 时间:2024/05/21 08:04
题目:627B - Factory Repairs
The first line contains five integers n,k, a,b, and q (1 ≤ k ≤ n ≤ 200 000,1 ≤ b < a ≤ 10 000, 1 ≤ q ≤ 200 000) — the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.
The next q lines contain the descriptions of the queries. Each query is of one of the following two forms:
- 1 di ai (1 ≤ di ≤ n,1 ≤ ai ≤ 10 000), representing an update ofai orders on daydi, or
- 2 pi (1 ≤ pi ≤ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi?
It's guaranteed that the input will contain at least one query of the second type.
For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over alln days.
题意:
有个工厂在n天内生产产品,本来机器是坏的,每天只能生产b件,修一次需要k天,修完后,每天可以生产a件,其中有q次询问,1开头,后面是d和ai,表示第d天有个ai的订单
2开头,后面有个d,表示这一天开始修理机器。每次2的时候,问工厂n天可以出售多少件产品?
分析:
线段树基础题型,1是单点增减,2是区间求和,sum二维数组分别维护修理前和修理后的生产效率,但是要注意都不可以超过订单额度,而且同一天可能有多个订单
区间求和的是修理前的天数和修理后的天数,相加即可
#include <bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int n,k,a,b;const int N=2e5+1;int sum[N<<2][2];void pushup(int rt){ sum[rt][0]=sum[rt<<1][0]+sum[rt<<1|1][0]; sum[rt][1]=sum[rt<<1][1]+sum[rt<<1|1][1];}void update(int d,int ai,int l,int r,int rt){ if(d==l&&l==r){ sum[rt][0]=ai+sum[rt][0]>b?b:ai+sum[rt][0]; sum[rt][1]=ai+sum[rt][1]>a?a:ai+sum[rt][1]; return; } int m=(l+r)>>1; if(d<=m)update(d,ai,lson); else update(d,ai,rson); pushup(rt);}int query(int L,int R,int id,int l,int r,int rt){ if(L<=l&&r<=R){ return sum[rt][id]; } int ans=0; int m=(l+r)>>1; if(L<=m)ans+=query(L,R,id,lson); if(m<R)ans+=query(L,R,id,rson); return ans;}int main(){ int q,op,d,ai; //freopen("f.txt","r",stdin); memset(sum,0,sizeof(sum)); scanf("%d%d%d%d%d",&n,&k,&a,&b,&q); for(int i=0;i<q;i++){ scanf("%d",&op); if(op==1){ scanf("%d%d",&d,&ai); update(d,ai,1,n,1); } else{ scanf("%d",&d); int ans=0; if(d>1)ans=query(1,d-1,0,1,n,1); // cout<<ans<<endl; if(d+k<=n) ans+=query(d+k,n,1,1,n,1); printf("%d\n",ans); } } return 0;}
- Codeforces 627B Factory Repairs 【线段树】
- Codeforces 627B Factory Repairs 线段树
- CodeForces 627B Factory Repairs
- CodeForces 627 B.Factory Repairs(BIT)
- Codeforces 635D Factory Repairs (树状数组)
- Codeforces 635D Factory Repairs【树状数组】
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D635 - Factory Repairs 线段树
- CodeForces 635D Factory Repairs(树状数组)
- Codeforces 635D Factory Repairs【思维+树状数组】
- CodeForces 240E E. Road Repairs 树
- Codeforces 482B 线段树
- codeforces 777E. Hanoi Factory (线段树)
- 【codeforces 777E】Hanoi Factory【动态规划+线段树】
- codeforces 482B Interesting Array 线段树
- Codeforces 482B Interesting Array(线段树)
- Codeforces 482B. Interesting Array 线段树
- Interesting Array - CodeForces 482 B 线段树
- CodeForces 482B-Interesting Array(线段树)
- ViewDragHelper源代码分析
- 慢连接
- C++中的struct和class的区别
- bzoj 1041: [HAOI2008]圆上的整点 【数学 思维】
- java数据库查询显示结果显示在JTable
- Codeforces 627B Factory Repairs 线段树
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- 深度优先遍历(DFS)
- 后台性能测试总结—测试准备篇
- 剑指offer-和为S的两个数字
- iOS开发小技巧
- 为什么MVC不是一种设计模式?
- Android四大组件深度解析
- 转载NSPredicate 筛选查询匹配