当我备战省选的时候,我才喜欢上 同桌 NOIP就精通的 线段树。GG。
* 洛谷 P3097 [USACO13DEC]最优挤奶Optimal Milking*
题目描述
Farmer John has recently purchased a new barn containing N milking machines (1 <= N <= 40,000), conveniently numbered 1..N and arranged in a row.
Milking machine i is capable of extracting M(i) units of milk per day (1 <= M(i) <= 100,000). Unfortunately, the machines were installed so close together that if a machine i is in use on a particular day, its two neighboring machines cannot be used that day (endpoint machines have only one neighbor, of course). Farmer John is free to select different subsets of machines to operate on different days.
Farmer John is interested in computing the maximum amount of milk he can extract over a series of D days (1 <= D <= 50,000). At the beginning of each day, he has enough time to perform maintenance on one selected milking machine i, thereby changing its daily milk output M(i) from that day forward. Given a list of these daily modifications, please tell Farmer John how much milk he can produce over D days (note that this number might not fit into a 32-bit integer).
FJ最近买了1个新仓库, 内含N 个挤奶机,1 到N 编号并排成一行。
挤奶机i 每天能产出M(i) 单位的奶。不幸的是, 机器装得太近以至于如果一台机器i 在某天被使用, 那与它相邻的两台机器那一天不能被使用
(当然, 两端点处的机器分别只有一个与之相邻的机器)。
FJ 可自由选择不同的机器在不同的日子工作。
FJ感兴趣于计算在D 天内他能产出奶的最大值。在每天开始时, 他有足够的时间维护一个选中的挤奶机i, 从而改变它从那天起的每日产奶量M(i)。
给出这些每日的修改,请告诉FJ他D 天中能产多少奶。
输入输出格式
输入格式:
* Line 1: The values of N and D.
Lines 2..1+N: Line i+1 contains the initial value of M(i).
Lines 2+N..1+N+D: Line 1+N+d contains two integers i and m,
indicating that Farmer John updates the value of M(i) to m at the beginning of day d.
输出格式:
* Line 1: The maximum total amount of milk FJ can produce over D days.
输入输出样例
输入样例#1:
5 3
1
2
3
4
5
5 2
2 7
1 10
输出样例#1: 32
说明
There are 5 machines, with initial milk outputs 1,2,3,4,5. On day 1, machine 5 is updated to output 2 unit of milk, and so on.
On day one, the optimal amount of milk is 2+4 = 6 (also achievable as 1+3+2). On day two, the optimal amount is 7+4 = 11. On day three, the optimal amount is 10+3+2=15.
题意简述:给定n个点排成一排,每个点有一个点权,多次改变某个点的点权并将最大点独立集计入答案,输出最终的答案
带限制的区间信息合并╮(╯▽╰)╭
s[ pos ][ L ][ R ] L表示 :该节点的 左端点有(1)没有(0)挤牛奶, R同理
#include
#include
#include
using namespace std;
inline long long read(){
char ch='*';long long sum=0,f=1;
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')
sum*=10,sum+=ch-'0',ch=getchar();
return sum*f;
}
#if 0
Writers: G.S.M. && Goes
Try
#endif
const int N=40000;
long long s[N*8][2][2];
void up(int pos){
s[pos][0][0]=max(
s[pos*2][0][1]+s[pos*2+1][0][0],
s[pos*2][0][0]+s[pos*2+1][1][0]);
s[pos][0][1]=max(
s[pos*2][0][0]+s[pos*2+1][1][1],
s[pos*2][0][1]+s[pos*2+1][0][1]);
s[pos][1][0]=max(
s[pos*2][1][0]+s[pos*2+1][1][0],
s[pos*2][1][1]+s[pos*2+1][0][0]);
s[pos][1][1]=max(
s[pos*2][1][1]+s[pos*2+1][0][1],
s[pos*2][1][0]+s[pos*2+1][1][1]);
}
void change(int pos,int l,int r,int x,long long va){
if(l==r){s[pos][1][1]=va; return ;}
int mid=(l+r)>>1;if(x<=mid) change(pos*2,l,mid,x,va);
else change(pos*2+1,mid+1,r,x,va);up(pos); return ;
}
int n,d;long long ans;
int main()
{
n=read(),d=read();
for(int i=1;i<=n;i++) change(1,1,n,i,read());
while(d--){
int P=read(),V=read();
change(1,1,n,P,V);
ans+=max( max(s[1][1][1],s[1][1][0]),
max(s[1][0][0],s[1][0][1]));
}printf("%lld\n",ans);
}
PS. long long ,数组大小 之类的基础性错误,我现在没有不错的时候。药丸。