100粉粉福

· · 个人记录

P5073 [Ynoi2015] 世上最幸福的女孩

#pragma pack(1)
#include<bits/stdc++.h>
using namespace std;
const long long MINF = -LONG_LONG_MAX;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<21, stdin), p1==p2) ? EOF: *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline long long read(){
    int n = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        n = (n << 1) + (n << 3) + (c ^ 48);
        c = getchar();
    }
    return n * f;
}
struct point{
    long long x, y;
    point(long long x = 0, long long y = 0) : x(x), y(y){}
    inline point operator + (const point &A)const{
        return point(x + A.x, y + A.y);
    }
    inline point operator - (const point &A)const{
        return point(x - A.x, y - A.y);
    }
    inline long long operator * (const point &A)const{
        return x * A.y - y * A.x;
    }
    inline bool operator < (const point &A)const{
        return (*this) * A >= 0;
    }
};
long long addv, a[300010];
struct hull{
    point *vc;
    int pnt, sz;
    hull(){
        pnt = 0;
        sz = 0;
    }
    inline point operator [] (const int &idx){
        return vc[idx];
    }
    inline void insert(const point &A){
        vc[A.x].y = max(vc[A.x].y, A.y);
    }
    inline void push_back(const point &A){
        vc[++sz] = A;
    }
    inline void empty(int len){
        for(register int i = 1; i <= len; i++){
            vc[i] = point(i, MINF);
        }
        sz = len;
    }
    inline void convex(){
        if(sz <= 2) return;
        int top = 2;
        for(register int i = 3; i <= sz; i++){
            if(vc[i].y == MINF) continue;
            while(top > 1 && vc[top]-vc[top-1] < vc[i]-vc[top-1]) top--;
            vc[++top] = vc[i];
        }
        sz = top;
        pnt = 1;
    }
    inline long long maxv(){
        while(pnt < sz && addv*(vc[pnt + 1].x-vc[pnt].x)+vc[pnt + 1].y-vc[pnt].y > 0){
            pnt++;
        }
        return addv * vc[pnt].x + vc[pnt].y;
    }
};
struct result{
    long long lmax, rmax, midmax, sum;
    result(long long lmax = 0, long long rmax = 0, long long midmax = 0, long long sum = 0) : lmax(lmax), rmax(rmax), midmax(midmax), sum(sum){}
    result operator + (const result &A)const{
        return result(max(lmax, sum+A.lmax), max(A.rmax, A.sum+rmax), max(max(midmax, A.midmax), rmax+A.lmax), sum + A.sum);
    }
} ans[600010];
struct qu{
    int l, r, idx;
    long long addv;
    bool operator < (const qu &A)const{
        return addv < A.addv;
    }
};
qu que[600010];
vector<int> qry;
struct segtree{
    struct sgtnode{
        hull lmx, rmx, ans;
        long long sum; 
    };
    point bl[600010], br[600010], bm[600010], tl[300010], tr[300010], tm[300010];
    point *tmpl = bl, *tmpr = br, *tmpm = bm;
    void presufmerge(hull &c, hull &a, hull &b, point addb){
        int sizea = a.sz, sizeb = b.sz;
        for(register int i = 1; i <= sizea; i++){
            c.push_back(a[i]);
        }
        for(register int i = 1; i <= sizeb; i++){
            c.push_back(addb + b[i]);
        }
        c.convex();
    }
    inline void minkowski(hull &c, hull &a, hull &b){
        int i = 1, j = 1, sizea = a.sz, sizeb = b.sz;
        c.insert(a[i] + b[j]);
        while(i < sizea && j < sizeb){
            if(a[i+1] - a[i] < b[j+1] - b[j]){
                j++;
                c.insert(a[i] + b[j]);
            }else{
                i++;
                c.insert(a[i] + b[j]);
            }
        }
        while(i < sizea){
            i++;
            c.insert(a[i] + b[j]);
        }
        while(j < sizeb){
            j++;
            c.insert(a[i] + b[j]);
        }
    }
    inline sgtnode build(int u, int l, int r){
        int mid = (l + r) >> 1;
        sgtnode lc, rc, now;
        vector<int> t1, t2;
        now.lmx.vc = tl, now.rmx.vc = tr, now.ans.vc = tm;
        for(register int x : qry){
            if(que[x].r < l || que[x].l > r) continue;
            if(que[x].l <= l && r <= que[x].r) t2.push_back(x); 
            else t1.push_back(x);
        }
        if(l == r){
            now.lmx.vc[1] = now.rmx.vc[1] = now.ans.vc[1] = {0, 0};
            now.lmx.vc[2] = now.rmx.vc[2] = now.ans.vc[2] = {1, a[l]};
            now.lmx.sz = now.rmx.sz = now.ans.sz = 2;
            now.sum = a[l];
            lc.lmx.vc = tmpl, lc.rmx.vc = tmpr, lc.ans.vc = tmpm;
            tmpl += 2, tmpr += 2, tmpm += 2;
            goto end;
        }
        qry = t1;
        lc = build(u << 1, l, mid);
        qry = t1;
        rc = build(u << 1 | 1, mid+1, r);
        presufmerge(now.lmx, lc.lmx, rc.lmx, point(mid - l + 1, lc.sum));
        presufmerge(now.rmx, rc.rmx, lc.rmx, point(r - mid, rc.sum));
        now.ans.vc++;
        now.ans.empty(r-l+1);
        for(register int i = 1; i <= lc.ans.sz; i++){
            now.ans.insert(lc.ans[i]);
        }
        for(register int i = 1; i <=  rc.ans.sz; i++){
            now.ans.insert(rc.ans[i]);
        }
        minkowski(now.ans, lc.rmx, rc.lmx);
        now.ans.vc--;
        now.ans[1] = {0, 0};
        now.ans.sz++;
        now.ans.convex();
        now.sum = lc.sum + rc.sum;
        end:
            now.lmx.pnt = now.rmx.pnt = now.ans.pnt = 1;
            for(register int _ = 0; _ < t2.size(); _++){
                int i = t2[_];
                addv = que[i].addv;
                result res = result(now.lmx.maxv(), now.rmx.maxv(), now.ans.maxv(), now.sum + (r - l + 1) * addv);
                ans[que[i].idx] = ans[que[i].idx] + res;
            }
            point *xl = lc.lmx.vc, *xr = lc.rmx.vc, *xm = lc.ans.vc;
            for(register int i = 1; i <= now.lmx.sz; i++) xl[i] = now.lmx.vc[i];
            for(register int i = 1; i <= now.rmx.sz; i++) xr[i] = now.rmx.vc[i];
            for(register int i = 1; i <= now.ans.sz; i++) xm[i] = now.ans.vc[i];
            now.lmx.vc = xl, now.rmx.vc = xr, now.ans.vc = xm;
            return now;
    }
};
int n, m, q;
segtree sgt;
int main(){
    n = read(), m = read();
    for(register int i = 1; i <= n; i++){
        a[i] = read();
    }
    for(register int i = 1; i <= m; i++){
        int opt = read();
        if(opt&1) addv += read();
        else{
            q++;
            que[q].idx = q;
            que[q].l = read();
            que[q].r = read();
            que[q].addv = addv;
        }
    }
    sort(que+1, que+q+1);
    for(register int i = 1; i <= q; i++){
        qry.push_back(i);
    }
    sgt.build(1, 1, n);
    for(register int i = 1; i <= q; i++){
        printf("%lld\n", ans[i].midmax);
    }
    return 0;
}

P3987 我永远喜欢珂朵莉~

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int n = 0;  
    int f = 1;
    char c = getchar_unlocked();
    while(c < '0' || c > '9'){
        if(c == '-') f = -1;
        c = getchar_unlocked();
    }
    while(c >= '0' && c <= '9'){
        n = (n << 3) + (n << 1) + (c ^ 48);
        c = getchar_unlocked();
    }
    return n * f;
}
inline void Write(long long x){
    if(x < 0){
        putchar_unlocked('-');
        x = -x;
    }
    if(x > 9) Write(x / 10);
    putchar_unlocked((x % 10) ^ 48);
    return;
}
inline void write(long long x, char c){
    Write(x);
    putchar_unlocked(c);
}
int n, m, a[1000010], q, sz[1000010];
long long tr[1000010];
vector<int> T[1000010], p[1000010], c[1000010];
inline int lowbit(int x){
    return x & -x;
}
inline void modify(int x, int y){
    while(x <= n){
        tr[x] += y;
        x += lowbit(x);
    }
}
inline long long sum(int x){
    long long res = 0;
    while(x){
        res += tr[x];
        x -= lowbit(x);
    }
    return res;
}
inline long long query(int l, int r){
    return sum(r) - sum(l-1);
}
inline int find(int f, int x){
    if(x >= p[f].size()) return x;
    return p[f][x] == x ? x : p[f][x] = find(f, p[f][x]);
}
inline void work(){
    for(register int i = 1; i <= q; i++){
        for(register int j = i; j <= q; j += i){
            for(int k : c[j]){
                T[i].push_back(k);
                p[i].push_back(p[i].size());
            }
        }
        if(T[i].empty()) continue;
        for(register int j = 1; j < T[i].size(); j++){
            if(T[i][j-1] > T[i][j]){
                stable_sort(T[i].begin(), T[i].end());
                break;
            }
        }
    }
}
inline void change(int l, int r, int k){
    if(k == 1 || T[k].empty()) return ;
    int pos1 = lower_bound(T[k].begin(), T[k].end(), l) - T[k].begin();
    for(register int i = find(k, pos1); i < T[k].size() && T[k][i] <= r; i = find(k, i+1)){
        if(a[T[k][i]] % k == 0){
            modify(T[k][i], -(a[T[k][i]] - a[T[k][i]]/k));
            a[T[k][i]] /= k;
        }
        if(a[T[k][i]] % k) p[k][i] = i+1;
    }
}
int main(){
    n = read(), m = read();
    for(register int i = 1; i <= n; i++){
        a[i] = read();
        c[a[i]].push_back(i);
        modify(i, a[i]);
        q = max(q, a[i]);
    }
    work();
    while(m--){
        int opt, l, r;
        opt = read(), l = read(), r = read();
        if(opt == 1){
            int k;
            k = read();
            change(l, r, k);
        }else{
            write(query(l, r), '\n');
        }
    }
    return 0;
}