跳到正文

题解

题解:P11735 [集训队互测 2015] 胡策的数列

ZSB00000约 10 分钟
原文
文章信息与版本

文章信息

收录
2026年8月11日
更新
2026年8月11日
篇幅
约 10 分钟
题目
P11735

版本

  1. v1当前

谁不喜欢可可爱爱的珂朵莉呢? 被校内的模拟赛搬去当赛题了,趁这个机会写个题解。

解题思路

首先不难发现题目的递推式其实可以化为 ai=t×(25)ia_i=t \times (\frac{2}{5})^i,于是题目就转化为维护一个等比数列。 我们发现 nn 很大但是 mm 很小,所以我们不需要维护整个序列,只需要维护一些段,记录每次操作即可。 具体来说,就是我们开一个珂朵莉树来维护所有的段,每一个段记录一个区间 [l,r][l,r],同时记下这个区间的初始值 a0a_0 和区间对应的幂指数,查询时遍历所有的区间,利用等比数列求和公式 ∑i=lrqi=qr+1−qlq−1\sum_{i=l}^rq^i=\frac{q^{r+1}-q^l}{q-1} 计算段内的和就可以了。

代码实现

其实在赛时我只打算用这个代码打暴力,所以加了一些常数优化,用了快读和光速幂,结果直接过了,还成了最优解?

C++
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1e9 + 9, base = 400000004, maxn = 44721, p = 666666671;
int n, m, mi[2][45720];
void init()
{
    mi[0][0] = 1;
    for (int i = 1; i <= maxn; i++)
    {
        mi[0][i] = 1ull * mi[0][i - 1] * base % mod;
    }
    mi[1][0] = 1;
    for (int i = 1; i <= maxn; i++)
    {
        mi[1][i] = 1ull * mi[1][i - 1] * mi[0][maxn] % mod;
    }
}
inline int poww(int a, int b)
{
    return 1ull * mi[0][1ull * b % maxn] * mi[1][1ull * b / maxn] % mod;
}
struct ctholly
{
    struct tree
    {
        int l, r;
        int t;
        int lmi, rmi;
        tree(int a, int b, int c, int d, int e)
        {
            l = a, r = b, t = c, lmi = d, rmi = e;
        }
        bool operator<(const tree &x) const
        {
            return l < x.l;
        }
    };
    set<tree> k;
    auto split(int x)
    {
        auto it = k.lower_bound(tree(x, 0, 0, 0, 0));
        if (it == k.begin() || (it != k.end() && it->l == x))
            return it;
        it--;
        int l = it->l, r = it->r, t = it->t, lmi = it->lmi, rmi = it->rmi;
        k.erase(it);
        k.emplace(l, x - 1, t, lmi, lmi + x - l - 1);
        return k.emplace(x, r, t, lmi + x - l, rmi).first;
    }
    void assign(int l, int r, int x, int fmi)
    {
        auto rr = split(r + 1), ll = split(l);
        k.erase(ll, rr);
        k.emplace(l, r, x, fmi, fmi + r - l);
    }
    int query(int l, int r)
    {
        int ans = 0;
        auto rr = split(r + 1), ll = split(l);
        for (auto it = ll; it != rr; it++)
        {
            if (it->rmi == 0)
                continue;
            int res1 = 1ull * poww(base, it->rmi + 1) * p % mod - 1;
            int res2 = 1ull * poww(base, it->lmi) * p % mod - 1;
            res1 = ((res1 - res2) % mod + mod) % mod;
            ans = (ans + 1ull * it->t * res1 % mod) % mod;
        }
        return ans;
    }
} kdl;
constexpr int bufsize = 1100000;
class fastio
{
private:
    static char outbuf[bufsize], inbuf[bufsize];
    char *p1, *p2;
    char *out;

public:
    fastio() : p1(inbuf), p2(inbuf), out(outbuf) {}
    ~fastio() { fflush(); }
    char gc()
    {
        if (p1 == p2)
        {
            p2 = (p1 = inbuf) + fread(inbuf, 1, bufsize, stdin);
            if (p1 == p2)
                return EOF;
        }
        return *p1++;
    }
    template <typename T>
    void read(T &x)
    {
        x = 0;
        char c = gc();
        int f = 1;
        while (!isdigit(c))
        {
            f = (c == '-' ? -1 : 1), c = gc();
        }
        while (isdigit(c))
        {
            x = x * 10 + c - 48, c = gc();
        }
        x *= f;
    }
    void fflush()
    {
        fwrite(outbuf, 1, out - outbuf, stdout);
        out = outbuf;
    }
    void pc(char x)
    {
        if (out - outbuf == bufsize)
            fflush();
        *out++ = x;
    }
    template <typename T>
    void write(T x)
    {
        if (x < 0)
            x = -x, pc('-');
        char st[1000];
        char *p = st;
        do
            *p++ = x % 10 + '0';
        while (x /= 10);
        while (p != st)
            pc(*--p);
    }
};
char fastio::inbuf[bufsize];
char fastio::outbuf[bufsize];
fastio rr;
signed main()
{
    freopen("series.in", "r", stdin);
    freopen("series.out", "w", stdout);
    rr.read(n);
    rr.read(m);
    init();
    int lastans = 0;
    kdl.assign(1, n, 0, 0);
    while (m--)
    {
        int op;
        rr.read(op);
        if (op == 1)
        {
            int l, r, t, p;
            rr.read(l);
            rr.read(r);
            rr.read(t);
            rr.read(p);
            l ^= lastans, r ^= lastans;
            kdl.assign(l, r, t, p);
        }
        else
        {
            int l, r;
            rr.read(l);
            rr.read(r);
            l ^= lastans, r ^= lastans;
            lastans = kdl.query(l, r);
            rr.write(lastans);
            rr.pc('\n');
        }
    }
    return 0;
}
/*
3 3
1 1 3 2 3
1 2 2 4 5
2 1 3
*/
/*
1000000000 3
1 1 12450 6666666 23333333
1 6666 99999 2333 44444
2 1 1000000000
*/
/*
1000000000 6
1 844435993 976896338 475965351 354485879
1 368647859 688844189 33882089 241699188
1 459034317 614186765 989837311 601866909
2 78141932 931519289
2 993998821 83652583
1 879671665 485953237 741936909 219814352
*/
/*
1000000000 1
1 879671665 485953237 741936909 219814352
*/

有不理解的地方可以私信我。

原文