cf250D. The Child and Sequence(线段树 均摊复杂度)

题意

题目链接

单点修改,区间mod,区间和

Sol

如果x > mod ,那么 x % mod < x / 2

证明:

即得易见平凡,

仿照上例显然,

留作习题答案略,

读者自证不难。

反之亦然同理,

推论自然成立,

略去过程Q.E.D.,

由上可知证毕。

然后维护个最大值就做完了。。

复杂度不知道是一个log还是两个log,大概是两个吧(线段树一个+最多改log次。)

#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;
}
/*

*/

原文地址:https://www.cnblogs.com/zwfymqz/p/10347536.html

时间: 2024-10-09 22:58:58

cf250D. The Child and Sequence(线段树 均摊复杂度)的相关文章

CF(438D) The Child and Sequence(线段树)

题意:对数列有三种操作: Print operation l,?r. Picks should write down the value of . Modulo operation l,?r,?x. Picks should perform assignment a[i]?=?a[i] mod x for each i (l?≤?i?≤?r). Set operation k,?x. Picks should set the value of a[k] to x (in other words

【loj6029】「雅礼集训 2017 Day1」市场 线段树+均摊分析

题目描述 给出一个长度为 $n$ 的序列,支持 $m$ 次操作,操作有四种:区间加.区间下取整除.区间求最小值.区间求和. $n\le 100000$ ,每次加的数在 $[-10^4,10^4]$ 之间,每次除的数在 $[2,10^9]$ 之间. 题解 线段树+均摊分析 和 [uoj#228]基础数据结构练习题 类似的均摊分析题. 对于原来的两个数 $a$ 和 $b$ ( $a>b$ ) ,原来的差是 $a-b$ ,都除以 $d$ 后的差是 $\frac{a-b}d$ ,相当于差也除了 $d$

Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks. Fortunately, Picks remembers how to

CodeForces 438D The Child and Sequence(线段树)

题目:http://codeforces.com/problemset/problem/438/D 一个数取模n%m,有两种情况. 1.m>n, n%m=n; 2.m<=n, n%m<=n/2; 所以当m>n时,取模操作可以忽略. 每个a[i]最多需要log(a[i])次取模操作变为0,因此我们可以对所有取模进行暴力更新.最多要更新n*log(a[i])次,由于单次更新复杂度为log(n),所以总复杂度为n*logn*log(a[i]). #include <bits/std

Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x,3操作是将下标为k的数变为x. 注意成段更新的时候,遇到一个区间的最大值还小于x的话就停止更新. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

CF438D The Child and Sequence 线段树

给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream> #define R register int #define ls (tr<<1) #define rs (tr<<1|1) using namespace std; inline long long g() { register long long ret=0,fix=1;

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

HDOJ 4893 Wow! Such Sequence! 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4893 题意:10万的区间,初始都为0,10万次操作,三种操作为单点修改,区间将每个数改成最近的斐波那契数,以及区间求和. 分析:用一个flag记录该段是否被改成斐波那契数,同时多维护一个sum1表示如果该段改成斐波那契数,区间和为多少.开始sum1为区间长度,之后在单点做了修改以后对其更新,需要的时候用其覆盖sum. 1 #include<cstdio> 2 #include<cstring>