首页 > 其他分享 >题解 CF1942F【Farmer John's Favorite Function】

题解 CF1942F【Farmer John's Favorite Function】

时间:2024-04-05 11:22:41浏览次数:23  
标签:CF1942F Function return int 题解 ll operator Modint friend

萌萌 F 题,上大分。

首先,如下定义 \(g(i)\):

  • \(g(1)=\lfloor\sqrt{a_1}\rfloor\);
  • 对于所有 \(i > 1\),\(g(i)=\lfloor\sqrt{g(i-1)+a_i}\rfloor\)。

也就是将 \(f(i)\) 的每一步运算后都向下取整。注意到 \(\lfloor f(i)\rfloor=g(i)\) 恒成立,于是我们只需要转而求每次修改后 \(g(n)\) 的值,避免了浮点数运算。

将询问离线,对时间轴(操作序列)维护数据结构,按空间轴(序列 \(a_i\))下标从小到大扫描线。每扫描到一个新的下标 \(i\),需要做的操作是将每个时刻的 \(g\) 加上对应时刻修改后的 \(a_i\),然后将每个时刻的 \(g\) 开根号下取整。因此,数据结构需要支持 \(n+q\) 次区间加操作和 \(n\) 次全局开根号下取整操作,并在最后输出每个位置的值。

可以使用势能线段树维护:每个节点维护区间最小值、最大值、区间加法标记。区间加操作是平凡的,全局开根号下取整操作直接暴力递归,直到 \(mx-mn=\lfloor\sqrt{mx}\rfloor-\lfloor\sqrt{mn}\rfloor\),此时打区间加法标记即可。也可以使用分块等维护。

// Problem: F. Farmer John's Favorite Function
// Contest: Codeforces - CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1942/problem/F
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x, y, z) for(int x = (y); x <= (z); ++x)
#define per(x, y, z) for(int x = (y); x >= (z); --x)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do {freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);} while(false)
#define endl '\n'
using namespace std;
typedef long long ll;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
int randint(int L, int R) {
    uniform_int_distribution<int> dist(L, R);
    return dist(rnd);
}

template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}

template<int mod>
inline unsigned int down(unsigned int x) {
	return x >= mod ? x - mod : x;
}

template<int mod>
struct Modint {
	unsigned int x;
	Modint() = default;
	Modint(unsigned int x) : x(x) {}
	friend istream& operator>>(istream& in, Modint& a) {return in >> a.x;}
	friend ostream& operator<<(ostream& out, Modint a) {return out << a.x;}
	friend Modint operator+(Modint a, Modint b) {return down<mod>(a.x + b.x);}
	friend Modint operator-(Modint a, Modint b) {return down<mod>(a.x - b.x + mod);}
	friend Modint operator*(Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}
	friend Modint operator/(Modint a, Modint b) {return a * ~b;}
	friend Modint operator^(Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
	friend Modint operator~(Modint a) {return a ^ (mod - 2);}
	friend Modint operator-(Modint a) {return down<mod>(mod - a.x);}
	friend Modint& operator+=(Modint& a, Modint b) {return a = a + b;}
	friend Modint& operator-=(Modint& a, Modint b) {return a = a - b;}
	friend Modint& operator*=(Modint& a, Modint b) {return a = a * b;}
	friend Modint& operator/=(Modint& a, Modint b) {return a = a / b;}
	friend Modint& operator^=(Modint& a, int b) {return a = a ^ b;}
	friend Modint& operator++(Modint& a) {return a += 1;}
	friend Modint operator++(Modint& a, int) {Modint x = a; a += 1; return x;}
	friend Modint& operator--(Modint& a) {return a -= 1;}
	friend Modint operator--(Modint& a, int) {Modint x = a; a -= 1; return x;}
	friend bool operator==(Modint a, Modint b) {return a.x == b.x;}
	friend bool operator!=(Modint a, Modint b) {return !(a == b);}
};

const ll N = 2e5 + 5;

ll n, m, a[N], qk[N], qx[N];
vector<tuple<ll, ll>> qs[N];

inline ll mysqrt(ll x) {
    ll k = sqrtl(x);
    while(k * k > x) --k;
    while((k + 1) * (k + 1) <= x) ++k;
    return k;
}

struct SegTree {
    ll mx[N << 2], mn[N << 2], tag[N << 2];
    #define lc(u) (u << 1)
    #define rc(u) (u << 1 | 1)
    void pushup(ll u) {
        mx[u] = max(mx[lc(u)], mx[rc(u)]);
        mn[u] = min(mn[lc(u)], mn[rc(u)]);
    }
    void pushdown(ll u) {
        tag[lc(u)] += tag[u];
        tag[rc(u)] += tag[u];
        mx[lc(u)] += tag[u];
        mx[rc(u)] += tag[u];
        mn[lc(u)] += tag[u];
        mn[rc(u)] += tag[u];
        tag[u] = 0;
    }
    void rangeadd(ll u, ll l, ll r, ll ql, ll qr, ll k) {
        if(ql > qr) return;
        if(ql <= l && r <= qr) {
            tag[u] += k;
            mx[u] += k;
            mn[u] += k;
            return;
        }
        pushdown(u);
        ll mid = (l + r) >> 1;
        if(ql <= mid) rangeadd(lc(u), l, mid, ql, qr, k);
        if(qr > mid) rangeadd(rc(u), mid + 1, r, ql, qr, k);
        pushup(u);
    }
    void rangesqrt(ll u, ll l, ll r, ll ql, ll qr) {
        if(ql > qr) return;
        if(l == r) {
            mx[u] = mn[u] = mysqrt(mx[u]);
            return;
        }
        if(ql <= l && r <= qr && mx[u] - mn[u] == mysqrt(mx[u]) - mysqrt(mn[u])) {
            ll diff = mysqrt(mx[u]) - mx[u];
            tag[u] += diff;
            mx[u] += diff;
            mn[u] += diff;
            return;
        }
        pushdown(u);
        ll mid = (l + r) >> 1;
        if(ql <= mid) rangesqrt(lc(u), l, mid, ql, qr);
        if(qr > mid) rangesqrt(rc(u), mid + 1, r, ql, qr);
        pushup(u);
    }
    void print(ll u, ll l, ll r, char ENDPRINT = '\n') {
        if(l == r) {
            cout << mx[u] << ENDPRINT;
            return;
        }
        pushdown(u);
        ll mid = (l + r) >> 1;
        print(lc(u), l, mid, ENDPRINT);
        print(rc(u), mid + 1, r, ENDPRINT);
        pushup(u);
    }
    #undef lc
    #undef rc
}sgt;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    rep(i, 1, n) cin >> a[i];
    rep(i, 1, m) cin >> qk[i] >> qx[i];
    rep(i, 1, m) qs[qk[i]].emplace_back(qx[i], i);
    rep(i, 1, n) {
        ll lstval = a[i], lstkey = 1;
        for(auto [val, key] : qs[i]) {
            sgt.rangeadd(1, 1, m, lstkey, key - 1, lstval);
            lstval = val;
            lstkey = key;
        }
        sgt.rangeadd(1, 1, m, lstkey, m, lstval);
        // sgt.print(1, 1, m, ' '); cout << endl;
        sgt.rangesqrt(1, 1, m, 1, m);
        // sgt.print(1, 1, m, ' '); cout << endl;
    }
    sgt.print(1, 1, m);
    return 0;
}

标签:CF1942F,Function,return,int,题解,ll,operator,Modint,friend
From: https://www.cnblogs.com/ruierqwq/p/18115577/CF-1942F

相关文章

  • P9870 [NOIP2023] 双序列拓展 题解
    题意:称某个序列\(B=\{b_1,b_2,\cdots,b_n\}\)是另一个序列\(A=\{a_1,a_2,\cdots,a_m\}\)的拓展当且仅当存在正整数序列\(L=\{l_1,l_2,\cdots,l_m\}\),将\(a_i\)替换为\(l_i\)个\(a_i\)后得到序列\(B\)。例如,\(\{1,3,3,3,2,2,2\}\)是\(\{1,3,3,2\}\)的拓展,......
  • CF1530G 题解
    考虑对操作进行转换。假设\(a_i\)为第\(i\)个\(1\)前面的\(0\)的个数。则操作可以进行如下转换:转换1:选择一个长度为\(k+1\)的子区间\(a_{l~l+k}\)。我们先把\(a_{l+1~l+k-1}\)翻转,然后更改\(a_l\)和\(a_{l+k}\)使得\(a_l+a_{l+k}\)不......
  • 【蓝桥杯选拔赛真题56】C++求位数 第十四届蓝桥杯青少年创意编程大赛 算法思维 C++编
    目录C++求位数一、题目要求1、编程实现2、输入输出二、算法分析三、程序编写四、程序说明五、运行结果六、考点分析七、推荐资料C++求位数第十四届蓝桥杯青少年创意编程大赛C++选拔赛真题一、题目要求1、编程实现给定一个正整数N(1<N<10^8),输出N为几位数2、......
  • CF1924D Balanced Subsequences 题解
    先判掉\(k>\min(n,m)\)的情况。首先有个明显的计算最长合法括号子序列的贪心方法:初始一个值为\(0\)的计数器,从左到右枚举每个字符,遇到(时将计数器加一,遇到)时如果计数器不为\(0\)就减一然后将答案加一。考虑绘制它的折线图。初始时纵坐标为\(0\),从左到右枚举每个字符......
  • CF1942F Farmer John's Favorite Function
    题意简述定义\(f_i=\sqrt{f_{i-1}+a_i},f_0=0\)。有\(q\)次操作,每次操作单点修改一个\(a_i\)的值,每次修改后求\(\lfloorf_n\rfloor\)的值。\(n,q\le2\times10^5,0\lea_i\le10^{18}\)。分析发现这个\(f_i\)不是整数非常不利于我们做题,考虑将它变成整数。令新的......
  • flask 装饰器 AssertionError: View function mapping is overwriting an existing en
    1问题描述写了一个登陆认证装饰器,部分试图,只有用户登陆才能访问deflogin_wrapper(func):definner(*args,**kwargs):"""判断是否登陆若是进入视图函数否则重定向到登陆页面"""if......
  • 小猫爬山 C++题解
    小猫爬山内存限制:256MiB时间限制:1000ms标准输入输出题目类型:传统评测方式:文本比较题目描述Freda和rainbow饲养了N只小猫,这天,小猫们要去爬山。经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<)。Freda和rainbow只好花钱让它......
  • git clone失败问题解决
    gitclone失败问题解决背景当gitclone出现以下问题时:error:RPCfailed;curl92HTTP/2stream5wasnotclosedcleanly:CANCEL(err8)error:5492bytesofbodyarestillexpectedfetch-pack:unexpecteddisconnectwhilereadingsidebandpacketfatal:earlyE......
  • 小木棍 C++题解
    小木棍内存限制:1024MiB时间限制:1000ms标准输入输出题目类型:传统评测方式:文本比较题目描述乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50。现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度。给出每......
  • CodeForces 1942F Farmer John's Favorite Function
    洛谷传送门CF传送门考虑一些复杂度带根号的做法。考虑分块,对于一个块,我们需要处理出一个数经过这个块会变成哪个数。以下假设块长\(\ge10\)(最后一个块块长可能\(<10\),暴力处理即可)。观察这个递推式\(f_i=\left\lfloor\sqrt{f_{i-1}+a_i}\right\rfloor\),发现对于一......