首页 > 代码库 > 福建省冬令营 Day2 T2

福建省冬令营 Day2 T2

技术分享

技术分享

技术分享


 

题解:

技术分享

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cstdio>
  6 #include<cmath>
  7 using namespace std;
  8 typedef long long lol;
  9 const lol mo = 1e9 + 7;
 10 const int me = 200233;
 11 struct shape
 12 {
 13     int e, o, se, so, si;
 14 };
 15 shape tr[me << 3];
 16 inline shape operator + (shape a, const shape &b)
 17 {
 18     if(a.si & 1)
 19     {
 20         a.o = a.o + b.e, a.e = a.e + b.o;
 21         a.so = a.so + b.se, a.se = a.se + b.so;
 22     }
 23     else
 24     {
 25         a.o = a.o + b.o, a.e = a.e + b.e;
 26         a.so = a.so + b.so, a.se = a.se + b.se;
 27     }
 28     a.si += b.si;
 29     if(a.o >= mo) a.o -= mo;
 30     if(a.e >= mo) a.e -= mo;
 31     if(a.so >= mo) a.so -= mo;
 32     if(a.se >= mo) a.se -= mo;
 33     if(a.si >= mo) a.si -= mo;
 34     return a;
 35 }
 36 inline lol Pow(lol x, lol y)
 37 {
 38     lol sum = 1;
 39     while(y)
 40     {
 41         if(y & 1) sum = (sum * x) % mo;
 42         x = (x * x) % mo;
 43         y >>= 1;
 44     }
 45     return sum;
 46 }
 47 void Ins(const int &k, const int &l, const int &r, const int &x, const int &y)
 48 {
 49     if(l == r && r == x)
 50     {
 51         tr[k].si = 1;
 52         tr[k].so = y;
 53         tr[k].o = y * r % mo;
 54         return;
 55     }
 56     int mi = l + r >> 1;
 57     int lc = k << 1, rc = k << 1 | 1;
 58     if(x <= mi) Ins(lc, l, mi, x, y);
 59     else Ins(rc, mi + 1, r, x, y);
 60     tr[k] = tr[lc] + tr[rc];
 61 }
 62 shape Ask(const int &k, const int &l, const int &r, const int &x, const int &y)
 63 {
 64     if(x > y) return (shape) {0, 0, 0, 0, 0};
 65     if(x <= l && r <= y) return tr[k];
 66     int mi = l + r >> 1;
 67     if(mi >= y) return Ask(k << 1, l, mi, x, y);
 68     if(mi < x) return Ask(k << 1 | 1, mi + 1, r, x, y);
 69     return Ask(k << 1, l, mi, x, y) + Ask(k << 1 | 1, mi + 1, r, x, y);
 70 }
 71 lol two;
 72 inline lol Exp(lol a, lol b)
 73 {
 74     --a, --b;
 75     return (a + b) % mo * ((b - a + 1) % mo) % mo * two % mo;
 76 }
 77 int m, n;
 78 int flag;
 79 lol x, y;
 80 lol cl, cr, nl, nr;
 81 inline void Solve(const shape &l, const shape &r, const shape &all)
 82 {
 83     int len = (cr - cl - 1) % mo;
 84     lol sum = (l.so + r.so + (lol) all.so * len % mo) % mo;
 85     lol ans = 0;
 86     ans = (ans + l.o + (cl - 1) % mo * l.so % mo * n % mo) % mo;
 87     ans = (ans + r.o + (cr - 1) % mo * r.so % mo * n % mo) % mo;
 88     ans = (ans + (lol) all.o * len % mo + all.so * Exp(cl + 1, cr - 1) % mo * n % mo ) % mo;
 89     printf("%lld\n", ((sum * (y % mo + 1) % mo - ans) % mo + mo) % mo);
 90 }
 91 char s[me];
 92 int main()
 93 {
 94     two = Pow(2, mo - 2);
 95     scanf("%d%s", &m, s);
 96     n = m << 1;
 97     for(int i = 1; i <= m; ++i) Ins(1, 1, n, i, s[i - 1] - 0);
 98     for(int i = 1; i <= m; ++i) Ins(1, 1, n, i + m, s[i - 1] - 0);
 99     int q;
100     scanf("%d", &q);
101     while(q--)
102     {
103         scanf("%d%lld%lld", &flag, &x, &y);
104         if(flag > 1)
105         {
106             cl = (x - 1) / n + 1, cr = (y - 1) / n + 1;
107             nl = (x - 1) % n + 1, nr = (y - 1) % n + 1;
108             if(cl == cr)
109             {
110                 shape c = Ask(1, 1, n, x, y);
111                 lol cc = (c.o + (cl - 1) % mo * c.so % mo * n % mo) % mo;
112                 printf("%lld\n", ((c.so * (y % mo + 1) % mo - cc) % mo + mo) % mo);
113             }
114             else
115             {
116                 shape l = Ask(1, 1, n, nl, n);
117                 shape r = Ask(1, 1, n, 1 + ((x & 1) ^ 1), nr);
118                 shape all = Ask(1, 1, n, 1 + ((x & 1) ^ 1), n);
119                 Solve(l, r, all);
120             }
121         }
122         else
123         {
124             Ins(1, 1, n, x, y);
125             Ins(1, 1, n, x + m, y);
126         }
127     }
128 }

福建省冬令营 Day2 T2