Alyona and towers CodeForces - 739C (线段树)
Posted uid001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Alyona and towers CodeForces - 739C (线段树)相关的知识,希望对你有一定的参考价值。
大意: 给定序列, 要求实现区间加, 询问整个序列最长的先增后减的区间.
线段树维护左右两端递增,递减,先增后减的长度即可, 要注意严格递增, 合并时要注意相等的情况, 要注意相加会爆int.
#include <iostream> #include <random> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ‘\n‘ #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<‘ ‘;hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<‘0‘||p>‘9‘)p=getchar();while(p>=‘0‘&&p<=‘9‘)x=x*10+p-‘0‘,p=getchar();return x;} //head const int N = 3e5+10; int n, m; struct _ { ll l,r,tag; int len,La,Lb,Lab,Ra,Rb,Rab,ab; _ () {} _ (int x) { l=r=x,len=La=Lb=Lab=Ra=Rb=Rab=ab=1,tag=0; } void upd(ll x) { l+=x,r+=x,tag+=x; } _ operator + (const _ &rhs) const { _ ret; ret.l=l,ret.r=rhs.r; ret.len=len+rhs.len; ret.La = La+(La==len&&r<rhs.l?rhs.La:0); ret.Lb = Lb+(Lb==len&&r>rhs.l?rhs.Lb:0); ret.Lab = Lab; if (La==len) { if (r<rhs.l) ret.Lab=La+rhs.Lab; else if (r>rhs.l) ret.Lab=La+rhs.Lb; } else if (Lab==len&&r>rhs.l) ret.Lab=Lab+rhs.Lb; ret.Ra = rhs.Ra+(rhs.Ra==rhs.len&&r<rhs.l?Ra:0); ret.Rb = rhs.Rb+(rhs.Rb==rhs.len&&r>rhs.l?Rb:0); ret.Rab = rhs.Rab; if (rhs.Rb==rhs.len) { if (r<rhs.l) ret.Rab=rhs.Rb+Ra; else if (r>rhs.l) ret.Rab=rhs.Rb+Rab; } else if (rhs.Rab==rhs.len&&r<rhs.l) ret.Rab=rhs.Rab+Ra; ret.ab = max(ab,rhs.ab); if (r!=rhs.l) ret.ab=max(ret.ab,Ra+rhs.Lb); if (r<rhs.l) ret.ab=max(ret.ab,Ra+rhs.Lab); if (r>rhs.l) ret.ab=max(ret.ab,Rab+rhs.Lb); ret.tag = 0; return ret; } } tr[N<<2]; void build(int o, int l, int r) { if (l==r) tr[o]=_(rd()); else build(ls),build(rs),tr[o]=tr[lc]+tr[rc]; } void pd(int o) { if (tr[o].tag) { tr[lc].upd(tr[o].tag); tr[rc].upd(tr[o].tag); tr[o].tag=0; } } void update(int o, int l, int r, int ql, int qr, int v) { if (ql<=l&&r<=qr) return tr[o].upd(v); pd(o); if (mid>=ql) update(ls,ql,qr,v); if (mid<qr) update(rs,ql,qr,v); tr[o]=tr[lc]+tr[rc]; } int main() { build(1,1,n=rd()); m=rd(); REP(i,1,m) { int l=rd(),r=rd(),d=rd(); update(1,1,n,l,r,d); printf("%d\n", tr[1].ab); } }
以上是关于Alyona and towers CodeForces - 739C (线段树)的主要内容,如果未能解决你的问题,请参考以下文章