跳到正文

剪贴板

剪贴板 ne07ln76

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define rep(i,x,y) for(int i = (x) ; i <= (y) ; ++ i)
#define Rep(i,x,y) for(int i = (x) ; i >= (y) ; -- i)

namespace Fread {
const int SIZE = 1 << 22;
char buf[SIZE], *S, *T;
inline char getchar() {
	if (S == T) {
		T = (S = buf) + fread(buf, 1, SIZE, stdin);
		if (S == T) return '\n';
	}
	return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 22;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
	fwrite(buf, 1, S - buf, stdout);
	S = buf;
}
inline void putchar(char c) {
	*S++ = c;
	if (S == T) flush();
}
struct NTR {
	~ NTR() { flush(); }
} ztr;
} // namespace Fwrite
#ifndef DEBUG
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
#endif
namespace Fastio {
struct ifstream {
	template<typename T>
	inline ifstream& operator >> (T& x) {
		char c = getchar();
		T f = 1;
		while (c < '0' || c > '9') {
			if (c == '-') f = -1;
			c = getchar();
		}
		x = 0;
		while (c >= '0' && c <= '9') {
			x = x * 10 + (c - '0');
			c = getchar();
		}
		x *= f;
		return *this;
	}
	inline ifstream& operator >> (char& c) {
		c = getchar();
		while (c == ' ' || c == '\n') c = getchar();
		return *this;
	}
	inline ifstream& operator >> (char* str) {
		int len = 0;
		char c = getchar();
		while (c == ' ' || c == '\n') c = getchar();
		while (c != ' ' && c != '\n' && c != '\r') { // \r\n in windows
			str[len++] = c;
			c = getchar();
		}
		str[len] = '\0';
		return *this;
	}
	ifstream(){}
} cin;
const char endl = '\n';
struct ofstream {
	template<typename T>
	inline ofstream& operator << (T x) {
		if (x == 0) { putchar('0'); return *this; }
		if (x < 0) { putchar('-'); x = -x; }
		static int sta[45];
		int top = 0;
		while (x) { sta[++top] = x % 10; x /= 10; }
		while (top) { putchar(sta[top] + '0'); --top; }
		return *this;
	}
	inline ofstream& operator << (char c) {
		putchar(c);
		return *this;
	}
	inline ofstream& operator << (char* str) {
		int cur = 0;
		while (str[cur]) putchar(str[cur++]);
		return *this;
	}
	inline ofstream& operator << (const char* str) {
		int cur = 0;
		while (str[cur]) putchar(str[cur++]);
		return *this;
	}
	ofstream(){}
} cout;
} // namespace Fastio
using Fastio :: cin ;
using Fastio :: cout ;
using Fastio :: endl ;
using std :: cerr ;


using ll = long long ;
const int N = 3e5 + 20 ;
const int M = 6e5 + 20 ;
struct line {
    ll k , b ;
    line () : k(0ll) , b(0ll) {

    }
    line (ll K,ll B) : k(K) , b(B) {

    }
    ll operator()(ll x) {
        return k * x + b ;
    }
} ;

inline line operator+(const line &l1,const line &l2) {
    return line (l1.k + l2.k , l1.b + l2.b) ;
}

inline bool check (const line &x,const line &y,const line &z) {
    return (__int128_t)(x.b - z.b) * (y.k - x.k) <= (__int128_t)(x.b - y.b) * (z.k - x.k) ;
}

inline int sm (const line &x,const line &y,const line &z,const line &w) {
    auto lhs = (__int128_t) (x.b - y.b) * (w.k - z.k) , rhs = (__int128_t) (z.b - w.b) * (y.k - x.k);
    if (lhs < rhs) return -1;
    if (lhs == rhs) return 0;
    return 1;
}

struct covex {

    std :: vector<line> f;
    inline covex (){

    }

    inline ~covex () {
        std :: vector<line> ().swap(f) ;
    }

    inline void swap (covex &c) {
        f.swap(c.f) ;
    }

    inline void fix () {
        f.shrink_to_fit()  ;
    }

    inline void shift (const line &delta) {
        for (auto &l : f) {
            l = l + delta ;
        }
    }

    inline void sum(const covex &A,const covex &B) {
        auto &a = A.f , &b = B.f ;
        int pa = 0 , pb = 0 , sa = (int)a.size( ) - 1 , sb = (int)b.size( ) - 1;
        f.clear( ) ;
        #define addline f.emplace_back(a[pa].k + b[pb].k , a[pa].b + b[pb].b)
        addline ;
        while (pa < sa || pb < sb) {
            if (pa == sa) {
                ++ pb ;
                addline ;
                continue;
            }
            if (pb == sb) {
                ++ pa ;
                addline ;
                continue;
            }
            int comp = sm (a[pa] , a[pa + 1] , b[pb] , b[pb + 1]) ;
            if (comp < 0) ++ pa ;
            if (comp == 0) ++ pa , ++ pb ;
            if (comp > 0) ++ pb ;
            addline ;
        }
        #undef addline
        fix( );
    }

    inline void max(const covex &a) {
        std :: vector <line> t(f.size() + a.f.size() ) ;
        std :: merge (all (f) , all (a.f) , t.begin() , [](const line &a,const line &b) {return (a.k != b.k) ? a.k < b.k : a.b < b.b;});
        f.clear () ;
        for (const auto &l:t) {
            while (
                (f.size( ) >= 1 && f.back().k == l.k) ||
                (f.size( ) >= 2 && check(f[(int)f.size() - 2] , f.back( ) , l)) )
                    f.pop_back( ) ;
            f.emplace_back(l.k,l.b) ;
        }
        fix() ;
    }

} ;

int ql[M] , qr[M] , n , q ;
ll qx[M] , ans[M] , sf[M] , a[N] ;

std :: tuple < covex , covex , covex , line > solve (int l,int r,std :: vector <int> &ques) {
    int mid = (l + r) >> 1;
    std :: vector <int> ls , rs , ths ;

    for (auto q : ques) {
        if (ql[q] <= l && r <= qr[q]) {
            ths.emplace_back(q) ;
            continue;
        }
        if (ql[q] <= mid) ls.emplace_back(q) ;
        if (qr[q] > mid) rs.emplace_back(q) ;
    }
    ls.shrink_to_fit() , rs.shrink_to_fit( ) ;
    std :: vector<int> ().swap (ques) ;

    covex pre , suf , sub ; line sum ;
    if (l == r) {
        pre.f.emplace_back(0 , 0) ;
        pre.f.emplace_back(1 , a[l]) ;
        suf = pre ;
        sub = pre ;
        sum = line (1 , a[l]) ;
    }
    else {
        auto [prel , sufl , subl , suml] = solve (l , mid , ls) ;
        auto [prer , sufr , subr , sumr] = solve (mid + 1 , r , rs) ;
        sum = suml + sumr ;
        /*
            pre = max (prel , prer + suml)
            suf = max (sufr , prel + sufr)
            sub = max (subl , subr , prer + sufl)
        */
        sub.sum (sufl , prer) ;
        sub.max (subl) ;
        sub.max (subr) ;
        prer.shift (suml) ;
        sufl.shift (sumr) ;
        pre.swap (prel) ;
        suf.swap (sufr) ;
        pre.max (prer) ;
        suf.max (sufl) ;
    }

    std :: vector<line> &c1 = pre.f , &c2 = suf.f , &c3 = sub.f ;

    int pt1 = 0 , pt2 = 0 , pt3 = 0 , s1 = (int)pre.f.size() - 1 , s2 = (int)suf.f.size( ) - 1 , s3 = (int)sub.f.size( ) - 1 ;
    for (const auto &q:ths) {

        ll del = qx[q] ;
        ll qpre , qsuf , qsub , qsum = sum(del) ;

        while (pt1 < s1 && c1[pt1](del) <= c1[pt1 + 1](del))
            ++ pt1 ;
        qpre = c1[pt1] (del) ;
        while (pt2 < s2 && c2[pt2](del) <= c2[pt2 + 1](del))
            ++ pt2 ;
        qsuf = c2[pt2] (del) ;
        while (pt3 < s3 && c3[pt3](del) <= c3[pt3 + 1](del))
            ++ pt3 ;
        qsub = c3[pt3] (del) ;

        ans[q] = std :: max (ans[q] , sf[q] + qpre ) ;
        ans[q] = std :: max (ans[q] , qsub ) ;
        sf[q] = std :: max (qsuf , qsum + sf[q]) ;
    }
    return {pre , suf , sub , sum} ;
}


int main( ) {
    cin >> n >> q ;
    rep (i,1,n)
        cin >> a[i] ;
    int qc = 0 ; ll acc = 0  ;
    rep (i,1,q) {
        int op , x ;
        cin >> op ;
        if (op == 1) {
            cin >> x ;
            acc += x ;
        }
        else {
            ++ qc;
            cin >> ql[qc] >> qr[qc] ;
            qx[qc] = acc ;
        }

    }
    std :: vector <int> ques(qc);
    std :: iota (all(ques) , 1);
    std :: sort (all(ques) , [](int a,int b) {return qx[a] < qx[b];});
    solve(1 , n , ques) ;
    rep (i,1,qc) {
        cout << ans[i] << '\n' ;
    }

}