现在的位置: 首页 > 综合 > 正文

HDU 5068 Harry And Math Teacher

2019年02月22日 ⁄ 综合 ⁄ 共 1859字 ⁄ 字号 评论关闭

题目链接~~>

做题感悟:这题真的很高深,有种高大上的感觉。

解题思路:

                两层之间的联通可以看成是一个矩阵  代表上下两层都可以联通,,代表下层第1个门与上层第一个门不联通,以此类推联通就可以用矩阵表示了。这样修改和查询都可以用线段树来完成。

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std  ;
#define INT __int64
#define L(x)  (x * 2)
#define R(x)  (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const INT mod =  1000000007 ;
const int MY = 1400 + 5 ;
const int MX = 50000 + 5 ;
int n ,m ;
struct node
{
    int le ,rt ;
    INT p[2][2] ;
}T[MX*8] ;
node operator *(const node& a ,const node& b)
{
    node c ;
    memset(c.p ,0 ,sizeof(c.p)) ;
    for(int i = 0 ;i < 2 ; ++i)
      for(int k = 0 ;k < 2 ; ++k)
         if(a.p[i][k])
            for(int j = 0 ;j < 2 ; ++j)
               c.p[i][j] = (c.p[i][j] + a.p[i][k]*b.p[k][j])%mod ;
    return c ;
}
void build(int x ,int le ,int rt)
{
    T[x].le = le ; T[x].rt = rt ;
    if(le == rt)
    {
        for(int i = 0 ;i < 2 ; ++i)
          for(int j = 0 ;j < 2 ; ++j)
              T[x].p[i][j] = 1 ;
        return ;
    }
    int Mid = (le + rt)>>1 ;
    build(L(x) ,le ,Mid) ;
    build(R(x) ,Mid+1 ,rt) ;
    T[x] = T[L(x)] * T[R(x)] ;
    T[x].le = le ; T[x].rt = rt ;
}
void update(int x ,int pos ,int i ,int j)
{
    if(T[x].le == T[x].rt)
    {
        T[x].p[i][j] ^= 1 ;
        return ;
    }
    int Mid = (T[x].le + T[x].rt)>>1 ;
    if(pos <= Mid)   update(L(x) ,pos ,i ,j) ;
    else    update(R(x) ,pos ,i ,j) ;
    int le = T[x].le ,rt = T[x].rt ;
    T[x] = T[L(x)] * T[R(x)] ;
    T[x].le = le ; T[x].rt = rt ;
}
node section(int x ,int le ,int rt) // 查询
{
    if(T[x].le == le && T[x].rt == rt)
        return T[x] ;
    int Mid = (T[x].le + T[x].rt)>>1 ;
    if(rt <= Mid)      return   section(L(x) ,le ,rt) ;
    else if(le > Mid)  return   section(R(x) ,le ,rt) ;
    else
          return  section(L(x) ,le ,Mid)*section(R(x) ,Mid+1 ,rt) ;
}
int main()
{
    //freopen("input.txt" ,"r" ,stdin) ;
    int p ,u ,v ,z ;
    while(~scanf("%d%d" ,&n ,&m))
    {
        build(1 ,1 ,n) ;
        for(int i = 0 ;i < m ; ++i)
        {
            scanf("%d" ,&p) ;
            if(p)
            {
                scanf("%d%d%d" ,&u ,&v ,&z) ;
                update(1 ,u ,v-1 ,z-1) ;
            }
            else
            {
               scanf("%d%d" ,&u ,&v) ;
               node ans = section(1 ,u ,v-1) ;
               printf("%I64d\n" ,(ans.p[0][0] + ans.p[0][1] + ans.p[1][0] + ans.p[1][1])%mod) ;
            }
        }
    }
    return 0 ;
}

抱歉!评论已关闭.