zoukankan      html  css  js  c++  java
  • [清华集训]序列操作

    嘟嘟嘟


    此题以前觉得不可做的原因是当时组合数方面几乎啥也不会,现在对这题算是理解了(然而还是看了题解)。


    因为(c leqslant 20),因此可以把每一个(c)的答案都存下来,即每一个节点开一个长度为20的数组,(f[i])表示当前区间选(i)个数相乘的所有方案和。


    1.区间取反。
    那么偶数项不变,奇数项取反。
    2.区间加。
    即一个序列(a_1, a_2, a_3 ldots a_n)变成了(a_1 + c, a_2 + c, a_3 + c ldots a_n + c)
    考虑加上这个(c)(f(x))的变化,能搞出这么个式子:$$f'(x) = sum _ {i = 0} ^ {x} f(i) * c ^ {x - i} * C_{len - i} ^ {x - i}$$((f'(x))表示新的(f(x)),不是(f(x))的导数……)
    理解起来就是我们把(f(x))的每一项拆开,然后发现每一项都是由(f(i) * c ^ {x - i})构成的,所以枚举(i)。因为是任意取的(x)项,所以(f(i))(c ^ {x - i})都是任意取的,而(f(i))本身就表示任意取(i)项,所以只用考虑剩下(x - i)(c)怎么取,自然有(C_{len - i} ^ {x - i})种取法。
    这样pushdown就完事了。


    最后剩一个pushup。只要枚举左右区间的项数,然后相乘即可:$$f(i) = sum _ {j = 0} ^ {i} f_{ls} (j) * f_{rs} (i - j)$$
    到这里这题就没了。细节就是啥都可能是负数,得加模数变回正的……

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<assert.h>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 5e4 + 5;
    const int N = 20;
    const ll mod = 19940417;
    In ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    In void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
      freopen(".in", "r", stdin);
      freopen(".out", "w", stdout);
    #endif
    }
    
    char s[2];
    int n, m;
    
    In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}
    ll C[maxn][N + 2];
    In void init()
    {
      C[0][0] = 1;
      for(int i = 1; i < maxn; ++i)
        {
          C[i][0] = 1;
          for(int j = 1; j <= min(i, N); ++j) C[i][j] = inc(C[i - 1][j - 1], C[i - 1][j]);
        }
    }
    
    struct Tree
    {
      int l, r, lzy, rev;
      ll f[N + 2];
      In Tree operator + (const Tree& oth)const
      {
        Tree ret; Mem(ret.f, 0);
        ret.l = l, ret.r = oth.r;
        ret.lzy = ret.rev = 0;
        for(int i = 0; i <= min(ret.r - ret.l + 1, N); ++i)
          for(int j = 0; j <= i; ++j)
    	ret.f[i] = inc(ret.f[i], f[j] * oth.f[i - j] % mod);
        return ret;
      }
    }t[maxn << 2];
    In void build(int L, int R, int now)
    {
      t[now].l = L, t[now].r = R;
      if(L == R)
        {
          t[now].f[1] = inc(mod, read()), t[now].f[0] = 1;
          return;
        }
      int mid = (L + R) >> 1;
      build(L, mid, now << 1);
      build(mid + 1, R, now << 1 | 1);
      t[now] = t[now << 1] + t[now << 1 | 1];
    }
    In void change_rev(int now)
    {
      t[now].rev ^= 1, t[now].lzy = mod - t[now].lzy;
      for(int i = 1; i <= N; i += 2) t[now].f[i] = mod - t[now].f[i];
    }
    In void change_add(int now, int d)
    {
      t[now].lzy = inc(t[now].lzy, d); if(t[now].lzy < 0) t[now].lzy += mod;
      int len = t[now].r - t[now].l + 1;
      for(int i = min(len, N); i >= 0; --i)
        {
          for(int j = i - 1, c = d; j >= 0; --j, c = 1LL * c * d % mod)
    	{
    	  t[now].f[i] = inc(t[now].f[i], t[now].f[j] * c % mod * C[len - j][i - j] % mod);
    	  if(t[now].f[i] < 0) t[now].f[i] += mod;
    	}
        }
    }
    In void pushdown(int now)
    {
      if(t[now].rev)
        {
          change_rev(now << 1), change_rev(now << 1 | 1);
          t[now].rev = 0;
        }
      if(t[now].lzy)
        {
          change_add(now << 1, t[now].lzy), change_add(now << 1 | 1, t[now].lzy);
          t[now].lzy = 0;
        }
    }
    In void update(int L, int R, int now, int d)
    {
      if(t[now].l == L && t[now].r == R)
        {
          d == INF ? change_rev(now) : change_add(now, d);
          return;
        }
      pushdown(now);
      int mid = (t[now].l + t[now].r) >> 1;
      if(R <= mid) update(L, R, now << 1, d);
      else if(L > mid) update(L, R, now << 1 | 1, d);
      else update(L, mid, now << 1, d), update(mid + 1, R, now << 1 | 1, d);
      t[now] = t[now << 1] + t[now << 1 | 1];
    }
    In Tree query(int L, int R, int now)
    {
      if(t[now].l == L && t[now].r == R) return t[now];
      pushdown(now);
      int mid = (t[now].l + t[now].r) >> 1;
      if(R <= mid) return query(L, R, now << 1);
      else if(L > mid) return query(L, R, now << 1 | 1);
      else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);
    }
    
    int main()
    {
      MYFILE();
      init();
      n = read(), m = read();
      build(1, n, 1);
      for(int i = 1; i <= m; ++i)
        {
          scanf("%s", s); int L = read(), R = read();
          if(s[0] == 'I') update(L, R, 1, read());
          else if(s[0] == 'R') update(L, R, 1, INF);
          else write(query(L, R, 1).f[read()]), enter; 
        }
      return 0;
    }
    
  • 相关阅读:
    LAMP应用 wdlinux 配置文件
    Linux mysql忘记root密码
    vim 编辑器 常用的设置
    Virtual PC(VPC)虚拟机安装CentOS 6.0网络配置
    缓存系统memcache的安装,配置和使用
    CentOS 去掉Last login提示/系统欢迎信息
    configure: error: GD build test failed. Please check the config.log for details.
    Centos 6.0 yum 更新源
    MySQL is running but PID file is not found
    Visual Studio快速开发以及Visual Studio 2010新功能介绍
  • 原文地址:https://www.cnblogs.com/mrclr/p/10977599.html
Copyright © 2011-2022 走看看