2019-2020 ICPC, Asia Jakarta Regional Contest k

先存一下,稍后写注释

#include
using namespace std;

#define pb emplace_back
#define mp make_pair
#define eps 1e-8
typedef long long ll;

const int maxn = 1e5+ 7;
const ll mod = 1e9 + 7;

struct P{
     
    ll m[2][2];
    P(){
     memset(m, 0, sizeof(m));}
}A, B;
char s[maxn];
P mul(P a, P b)
{
     
    P ans = P();
    for(int i = 0; i < 2; i ++)
    {
     
        for(int j = 0; j < 2; j ++)
        {
     
            for(int k = 0; k < 2; k ++)
            {
     
                ans.m[i][j] += (a.m[i][k] * b.m[k][j]) % mod;
                ans.m[i][j] %= mod;
            }
        }
    }
    return ans;
}
P tree[maxn << 2][2];
int lazy[maxn << 2];
ll lc(ll p)
{
     
    return p << 1;
}
ll rc(ll p)
{
     
    return p << 1 | 1;
}
void pushup(ll p)
{
     
    tree[p][0] = mul(tree[lc(p)][0], tree[rc(p)][0]);
    tree[p][1] = mul(tree[lc(p)][1], tree[rc(p)][1]);
}

void build(ll p, ll l, ll r)
{
     
    lazy[p] = 0;
    if(l == r)
    {
     
        if(s[r] == 'A')
            tree[p][0] = A, tree[p][1] = B;
        else
            tree[p][0] = B, tree[p][1] = A;
        return ;
    }
    ll mid = l + r >> 1;
    build(lc(p), l, mid);
    build(rc(p), mid + 1, r);
    pushup(p);
}

void pushdown(ll p)
{
     
    if(lazy[p])
    {
     
        lazy[lc(p)] ^= 1;
        lazy[rc(p)] ^= 1;
        swap(tree[lc(p)][0], tree[lc(p)][1]);
        swap(tree[rc(p)][0], tree[rc(p)][1]);
        lazy[p] = 0;
    }
}
void update(ll p, ll nl, ll nr, ll l, ll r)
{
     
    if(nl <= l && r <= nr)
    {
     
        swap(tree[p][0], tree[p][1]);
        lazy[p] ^= 1;
        return ;
    }
    pushdown(p);
    ll mid = l + r >> 1;
    if(nl <= mid)
        update(lc(p), nl, nr, l, mid);
    if(nr > mid)
        update(rc(p), nl, nr, mid + 1, r);
    pushup(p);
}
void query(ll p, ll nl, ll nr, ll l, ll r, P &ans)
{
     
    if(nl <= l && r <= nr)
    {
     
        ans = mul(ans, tree[p][0]);
        return ;
    }
    ll mid = l + r >> 1;
    pushdown(p);
    if(nl <= mid)
        query(lc(p), nl, nr, l, mid, ans);
    if(nr > mid)
        query(rc(p), nl, nr, mid + 1, r, ans);
    pushup(p);
}
int n, m, a, b, l, r, op;
int main()
{
     
    scanf("%d%d", &n, &m);
    scanf("%s", s + 1);
    A.m[0][0] = A.m[1][0] = A.m[1][1] = B.m[0][0] = B.m[0][1] = B.m[1][1] = 1;
    build(1, 1, n);
    for(int i = 0; i < m; i ++)
    {
     
        scanf("%d", &op);
        if(op == 1)
        {
     
            scanf("%d%d", &l ,&r);
            update(1, l, r, 1, n);
        }
        else
        {
     
            scanf("%d%d%d%d", &l, &r, &a, &b);

            P base = P();
            base.m[0][0] = base.m[1][1] = 1;
            query(1, l, r, 1, n, base);

            P ans = P();
            ans.m[0][0] = a;
            ans.m[0][1] = b;

            ans = mul(ans, base);

            //printf("%lld %lld\n", res.m[0][0], res.m[1][1]);
            printf("%lld %lld\n", ans.m[0][0], ans.m[0][1]);
        }
    }

}

你可能感兴趣的:(随笔)