Cf250D. The Child and Sequence

Keywords: C++

meaning of the title

Title Link

Single point modification, interval mod, interval sum

Sol

If x > mod, then x% mod < x / 2

Prove:

Ordinary is easy to see.

Following the example above, it is obvious that:

The answers to the exercises are brief.

It is not difficult for readers to prove themselves.

And vice versa.

The deduction is natural.

The process Q.E.D., is omitted.

We can see from the above.

Then the maintenance of a maximum is done.

Complexity does not know whether it is a log or two logs, probably two (line tree one + change logs at most). )

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, a[MAXN];
#define ls k << 1
#define rs k << 1 | 1
LL sum[MAXN];
int mx[MAXN];
void update(int k) {
    sum[k] = sum[ls] + sum[rs];
    mx[k] = max(mx[ls], mx[rs]);
}
void Build(int k, int ll, int rr) {
    if(ll == rr) {sum[k] = mx[k] = read(); return ;}
    int mid =  ll + rr >> 1;
    Build(ls, ll, mid); Build(rs, mid + 1, rr);
    update(k);
}
LL Query(int k, int l, int r, int ql, int qr) {
    if(ql <= l && r <= qr) return sum[k];
    int mid = l + r >> 1;
    if(ql > mid) return Query(rs, mid + 1, r, ql, qr);
    else if(qr <= mid) return Query(ls, l, mid, ql, qr);
    else return Query(ls, l, mid, ql, qr) + Query(rs, mid + 1, r, ql, qr);
}
void Modify(int k, int l, int r, int p, int v) {
    if(l == r) {sum[k] = mx[k] = v; return ;}
    int mid = l + r >> 1;
    if(p <= mid) Modify(ls, l, mid, p, v);
    else Modify(rs, mid + 1, r, p, v);
    update(k); 
}
void Mod(int k, int l, int r, int ql, int qr, int x) {
    if(mx[k] < x) return ;
    if(l == r) {sum[k] = mx[k] % x; mx[k] %= x; return ;}
    int mid = l + r >> 1;
    if(ql <= mid) Mod(ls, l, mid, ql, qr, x);
    if(qr  > mid) Mod(rs, mid + 1, r, ql, qr, x);
    update(k);
}
signed main() {
    N = read(); M = read();
    Build(1, 1, N);
    while(M--) {
        int opt = read();
        if(opt == 1) {
            int l = read(), r = read();
            cout << Query(1, 1, N, l, r) << '\n'; 
        } else if(opt == 2) {
            int l = read(), r = read(), x = read();
            Mod(1, 1, N, l, r, x); 
        } else {
            int k = read(), x = read();
            Modify(1, 1, N, k, x);
        }
    }
    return 0;
}
/*

*/

Posted by shaitand on Fri, 01 Feb 2019 22:24:15 -0800