<commit_before>/*
 * roimodel.cpp
 *
 *  Created on: 02.02.2013
 *      Author: Ralph
 */
#include "roimodel.h"

#include "selectionbox.h"
#include "vptr.h"

#include <QtCore/QDebug>

ROIModel::ROIModel( QAbstractItemModel* globalProps ) :
    m_globalProps( globalProps ),
    m_count( 0 )
{

}

ROIModel::~ROIModel()
{
}

int ROIModel::rowCount( const QModelIndex &parent ) const
{
    //qDebug() << "rowCount():" << parent;
    if ( parent.isValid() )
    {
        if ( parent.internalId() == - 1 )
        {
            return m_boxes[parent.row()].size() - 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return m_boxes.size();
    }
}

int ROIModel::columnCount( const QModelIndex &parent ) const
{
    return 1;
}

QModelIndex ROIModel::index( int row, int column, const QModelIndex & parent ) const
{
    //qDebug() << "index():" << row << column << parent;
    if ( parent.isValid() )
    {
        return createIndex( row, column, parent.row() );
    }
    else
    {
        return createIndex( row, column, -1 );
    }
}

QModelIndex ROIModel::parent( const QModelIndex & index ) const
{
    //qDebug() << "parent():" << index;
    if ( index.internalId() == -1 )
    {
        return QModelIndex();
    }
    else
    {
        return createIndex( index.internalId(), index.column(), -1 );
    }
}

QVariant ROIModel::data( const QModelIndex &index, int role ) const
{
    QVariant roi;
    switch ( role )
    {
        case Qt::CheckStateRole:
        {
            if ( index.internalId() == -1 )
            {
                roi = m_boxes[index.row()][0];
            }
            else
            {
                roi = m_boxes[index.internalId()][index.row()+1];
            }
            return VPtr<ROI>::asPtr( roi )->properties()->get( Fn::ROI::ACTIVE );
            break;
        }
        case Qt::DisplayRole:
        {
            if ( index.internalId() == -1 )
            {
                roi = m_boxes[index.row()][0];
            }
            else
            {
                roi = m_boxes[index.internalId()][index.row()+1];
            }
            switch ( (Fn::ROI)index.column() )
            {
                case Fn::ROI::POINTER:
                {
                    return roi;
                    break;
                }
                default:
                    return VPtr<ROI>::asPtr( roi )->properties()->get( Fn::ROI::NAME );
                    break;
            }
            break;
        }
        default:
            break;
    }
    return QVariant();
}

QVariant ROIModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    switch ( role )
    {
        case Qt::DisplayRole:
        {
            if ( orientation == Qt::Horizontal )
            {
                return QString( Fn::ROI2String::s( (Fn::ROI)section ) );
            }
            break;
        }
    }
    return QVariant();
}

bool ROIModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
    switch ( role )
    {
        case Qt::CheckStateRole:
        {
            if ( index.internalId() == -1 )
            {
                return VPtr<ROI>::asPtr( m_boxes[index.row()][0] )->properties()->set( Fn::ROI::ACTIVE, !VPtr<ROI>::asPtr( m_boxes[index.row()][0] )->properties()->get( Fn::ROI::ACTIVE ).toBool() );
            }
            else
            {
                return VPtr<ROI>::asPtr( m_boxes[index.internalId()][index.row()+1] )->properties()->set( Fn::ROI::ACTIVE, !VPtr<ROI>::asPtr( m_boxes[index.internalId()][index.row()+1] )->properties()->get( Fn::ROI::ACTIVE ).toBool() );
            }
            break;
        }
        case Qt::DisplayRole:
        {
            break;
        }
    }

    emit( dataChanged( index, index ) );

    return true;
}

Qt::ItemFlags ROIModel::flags( const QModelIndex& index ) const
{
    if ( !index.isValid() )
    {
        return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled ;
    }

    Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    if ( index.column() == 0 )
    {
        defaultFlags |= Qt::ItemIsUserCheckable;
    }

    return defaultFlags;
}

bool ROIModel::insertRows( int row, int count, const QModelIndex &parent )
{
    QString name = QString("new box") + QString::number( m_count++ );
    SelectionBox* newBox = new SelectionBox( name, m_globalProps );
    connect( newBox->properties(), SIGNAL( signalPropChanged() ), this, SLOT( propChanged() ) );
    //qDebug() << parent << parent.isValid();
    if ( !parent.isValid() )
    {
        QList<QVariant>l;
        l.push_back( VPtr<ROI>::asQVariant( newBox ) );
        beginInsertRows( QModelIndex(), m_boxes.size(), m_boxes.size() );
        m_boxes.push_back( l );
        endInsertRows();
    }
    else
    {
        beginInsertRows( QModelIndex(), m_boxes.size(), m_boxes.size() );
        if ( parent.internalId() == -1 )
        {
            m_boxes[parent.row()].push_back( VPtr<ROI>::asQVariant( newBox ) );
        }
        else
        {
            m_boxes[parent.internalId()].push_back( VPtr<ROI>::asQVariant( newBox ) );
        }
        endInsertRows();
    }
    emit ( dataChanged( index( 0, 0 ), index( 0, 0 ) ) );
    return true;
}

void ROIModel::propChanged()
{
    emit ( dataChanged( index( 0, 0 ), index( 0, 0 ) ) );

}

<commit_msg>color roi tree view with box colors<commit_after>/*
 * roimodel.cpp
 *
 *  Created on: 02.02.2013
 *      Author: Ralph
 */
#include "roimodel.h"

#include "selectionbox.h"
#include "vptr.h"

#include <QtCore/QDebug>

ROIModel::ROIModel( QAbstractItemModel* globalProps ) :
    m_globalProps( globalProps ),
    m_count( 0 )
{

}

ROIModel::~ROIModel()
{
}

int ROIModel::rowCount( const QModelIndex &parent ) const
{
    //qDebug() << "rowCount():" << parent;
    if ( parent.isValid() )
    {
        if ( parent.internalId() == - 1 )
        {
            return m_boxes[parent.row()].size() - 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return m_boxes.size();
    }
}

int ROIModel::columnCount( const QModelIndex &parent ) const
{
    return 1;
}

QModelIndex ROIModel::index( int row, int column, const QModelIndex & parent ) const
{
    //qDebug() << "index():" << row << column << parent;
    if ( parent.isValid() )
    {
        return createIndex( row, column, parent.row() );
    }
    else
    {
        return createIndex( row, column, -1 );
    }
}

QModelIndex ROIModel::parent( const QModelIndex & index ) const
{
    //qDebug() << "parent():" << index;
    if ( index.internalId() == -1 )
    {
        return QModelIndex();
    }
    else
    {
        return createIndex( index.internalId(), index.column(), -1 );
    }
}

QVariant ROIModel::data( const QModelIndex &index, int role ) const
{
    QVariant roi;
    if ( index.internalId() == -1 )
    {
        roi = m_boxes[index.row()][0];
    }
    else
    {
        roi = m_boxes[index.internalId()][index.row()+1];
    }
    switch ( role )
    {
        case Qt::CheckStateRole:
        {
            return VPtr<ROI>::asPtr( roi )->properties()->get( Fn::ROI::ACTIVE );
            break;
        }
        case Qt::BackgroundColorRole:
        {
            return VPtr<ROI>::asPtr( roi )->properties()->get( Fn::ROI::COLOR );
            break;
        }
        case Qt::DisplayRole:
        {
            switch ( (Fn::ROI)index.column() )
            {
                case Fn::ROI::POINTER:
                {
                    return roi;
                    break;
                }
                default:
                    return VPtr<ROI>::asPtr( roi )->properties()->get( Fn::ROI::NAME );
                    break;
            }
            break;
        }
        default:
            break;
    }
    return QVariant();
}

QVariant ROIModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    switch ( role )
    {
        case Qt::DisplayRole:
        {
            if ( orientation == Qt::Horizontal )
            {
                return QString( Fn::ROI2String::s( (Fn::ROI)section ) );
            }
            break;
        }
    }
    return QVariant();
}

bool ROIModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
    switch ( role )
    {
        case Qt::CheckStateRole:
        {
            if ( index.internalId() == -1 )
            {
                return VPtr<ROI>::asPtr( m_boxes[index.row()][0] )->properties()->set( Fn::ROI::ACTIVE, !VPtr<ROI>::asPtr( m_boxes[index.row()][0] )->properties()->get( Fn::ROI::ACTIVE ).toBool() );
            }
            else
            {
                return VPtr<ROI>::asPtr( m_boxes[index.internalId()][index.row()+1] )->properties()->set( Fn::ROI::ACTIVE, !VPtr<ROI>::asPtr( m_boxes[index.internalId()][index.row()+1] )->properties()->get( Fn::ROI::ACTIVE ).toBool() );
            }
            break;
        }
        case Qt::DisplayRole:
        {
            break;
        }
    }

    emit( dataChanged( index, index ) );

    return true;
}

Qt::ItemFlags ROIModel::flags( const QModelIndex& index ) const
{
    if ( !index.isValid() )
    {
        return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled ;
    }

    Qt::ItemFlags defaultFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    if ( index.column() == 0 )
    {
        defaultFlags |= Qt::ItemIsUserCheckable;
    }

    return defaultFlags;
}

bool ROIModel::insertRows( int row, int count, const QModelIndex &parent )
{
    QString name = QString("new box") + QString::number( m_count++ );
    SelectionBox* newBox = new SelectionBox( name, m_globalProps );
    connect( newBox->properties(), SIGNAL( signalPropChanged() ), this, SLOT( propChanged() ) );
    //qDebug() << parent << parent.isValid();
    if ( !parent.isValid() )
    {
        QList<QVariant>l;
        l.push_back( VPtr<ROI>::asQVariant( newBox ) );
        beginInsertRows( QModelIndex(), m_boxes.size(), m_boxes.size() );
        m_boxes.push_back( l );
        endInsertRows();
    }
    else
    {
        beginInsertRows( QModelIndex(), m_boxes.size(), m_boxes.size() );
        if ( parent.internalId() == -1 )
        {
            m_boxes[parent.row()].push_back( VPtr<ROI>::asQVariant( newBox ) );
        }
        else
        {
            m_boxes[parent.internalId()].push_back( VPtr<ROI>::asQVariant( newBox ) );
        }
        endInsertRows();
    }
    emit ( dataChanged( index( 0, 0 ), index( 0, 0 ) ) );
    return true;
}

void ROIModel::propChanged()
{
    emit ( dataChanged( index( 0, 0 ), index( 0, 0 ) ) );

}

<|endoftext|>


<commit_before>// @(#)root/foam:$Id$
// Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>

//_____________________________________________________________________________
//                                                                            //
// Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation //
// used for the cartesian geometry of the TFoam  cells                        //
//                                                                            //
//_____________________________________________________________________________

#include "TFoamVect.h"

#include <iostream>
#include <iomanip>

/** \class TFoamVect

Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation
used for the cartesian geometry of the TFoam cells

*/

ClassImp(TFoamVect);

////////////////////////////////////////////////////////////////////////////////
/// Default constructor for streamer

TFoamVect::TFoamVect()
{
   fDim    =0;
   fCoords =0;
}

////////////////////////////////////////////////////////////////////////////////
/// User constructor creating n-dimensional vector
/// and allocating dynamically array of components

TFoamVect::TFoamVect(Int_t n)
{
   Int_t i;
   fDim=n;
   fCoords = 0;
   if (n>0) {
      fCoords = new Double_t[fDim];
      if(gDebug) {
         if(fCoords == 0)
            Error("TFoamVect", "Constructor failed to allocate\n");
      }
      for (i=0; i<n; i++) *(fCoords+i)=0.0;
   }
   if(gDebug) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
}

////////////////////////////////////////////////////////////////////////////////
/// Copy constructor

TFoamVect::TFoamVect(const TFoamVect &Vect): TObject(Vect)
{
   fDim = Vect.fDim; fCoords = 0;
   if(fDim > 0)  fCoords = new Double_t[fDim];

   if(gDebug) {
      if(fCoords == 0) {
         Error("TFoamVect", "Constructor failed to allocate fCoords\n");
      }
   }

   for(Int_t i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];

}

////////////////////////////////////////////////////////////////////////////////
/// Destructor

TFoamVect::~TFoamVect()
{
   if(gDebug) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
   delete [] fCoords; //  free(fCoords)
   fCoords=0;
}

////////////////////////////////////////////////////////////////////////////////
/// substitution operator

TFoamVect& TFoamVect::operator =(const TFoamVect& Vect)
{
   Int_t i;
   if (&Vect == this) return *this;
   if( Vect.fDim < 0 )
       Error("TFoamVect","operator= : invalid  dimensions : %d and %d \n ",fDim,Vect.fDim);
   if( fDim != Vect.fDim ) {  // cleanup
      delete [] fCoords;
      fCoords = new Double_t[Vect.fDim];
   }
   fDim=Vect.fDim;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];
   if(gDebug)  Info("TFoamVect", "SUBSITUTE operator =\n ");
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// [] is for access to elements as in ordinary matrix like a[j]=b[j]
/// (Perhaps against some strict rules but rather practical.)
/// Range protection is built in, consequently for substitution
/// one should use rather use a=b than explicit loop!

Double_t &TFoamVect::operator[](Int_t n)
{
   if ((n<0) || (n>=fDim)) {
      Error( "TFoamVect","operator[], out of range \n");
   }
   return fCoords[n];
}

////////////////////////////////////////////////////////////////////////////////
/// unary multiplication operator *=

TFoamVect& TFoamVect::operator*=(const Double_t &x)
{
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]*x;
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// unary addition operator +=; adding vector c*=x,

TFoamVect& TFoamVect::operator+=(const TFoamVect& Shift)
{
   if( fDim != Shift.fDim){
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]+Shift.fCoords[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// unary subtraction operator -=

TFoamVect& TFoamVect::operator-=(const TFoamVect& Shift)
{
   if( fDim != Shift.fDim) {
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]-Shift.fCoords[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// addition operator +; sum of 2 vectors: c=a+b, a=a+b,
/// NEVER USE IT, VERY SLOW!!!

TFoamVect TFoamVect::operator+(const TFoamVect &p2)
{
   TFoamVect temp(fDim);
   temp  = (*this);
   temp += p2;
   return temp;
}

////////////////////////////////////////////////////////////////////////////////
/// subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
/// NEVER USE IT, VERY SLOW!!!

TFoamVect TFoamVect::operator-(const TFoamVect &p2)
{
   TFoamVect temp(fDim);
   temp  = (*this);
   temp -= p2;
   return temp;
}

////////////////////////////////////////////////////////////////////////////////
/// Loading in ordinary double prec. vector, sometimes can be useful

TFoamVect& TFoamVect::operator =(Double_t Vect[])
{
   Int_t i;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// Loading in double prec. number, sometimes can be useful

TFoamVect& TFoamVect::operator =(Double_t x)
{
   if(fCoords != 0) {
      for(Int_t i=0; i<fDim; i++)
         fCoords[i] = x;
   }
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// Printout of all vector components on "std::cout"

void TFoamVect::Print(Option_t *option) const
{
   if(!option) Error("Print ", "No option set \n");
   Int_t i;
   Int_t pr = std::cout.precision(7);
   std::cout << "(";
   for(i=0; i<fDim-1; i++) std::cout  << std::setw(12) << *(fCoords+i) << ",";
   std::cout  << std::setw(12) << *(fCoords+fDim-1);
   std::cout << ")";
   std::cout.precision(pr);
}<commit_msg>Increase gDebug level needed for too frequent messages in TFoamVect.cxx<commit_after>// @(#)root/foam:$Id$
// Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>

//_____________________________________________________________________________
//                                                                            //
// Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation //
// used for the cartesian geometry of the TFoam  cells                        //
//                                                                            //
//_____________________________________________________________________________

#include "TFoamVect.h"

#include <iostream>
#include <iomanip>

/** \class TFoamVect

Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation
used for the cartesian geometry of the TFoam cells

*/

ClassImp(TFoamVect);

////////////////////////////////////////////////////////////////////////////////
/// Default constructor for streamer

TFoamVect::TFoamVect()
{
   fDim    =0;
   fCoords =0;
}

////////////////////////////////////////////////////////////////////////////////
/// User constructor creating n-dimensional vector
/// and allocating dynamically array of components

TFoamVect::TFoamVect(Int_t n)
{
   Int_t i;
   fDim=n;
   fCoords = 0;
   if (n>0) {
      fCoords = new Double_t[fDim];
      if(gDebug) {
         if(fCoords == 0)
            Error("TFoamVect", "Constructor failed to allocate\n");
      }
      for (i=0; i<n; i++) *(fCoords+i)=0.0;
   }
   if(gDebug>=3) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
}

////////////////////////////////////////////////////////////////////////////////
/// Copy constructor

TFoamVect::TFoamVect(const TFoamVect &Vect): TObject(Vect)
{
   fDim = Vect.fDim; fCoords = 0;
   if(fDim > 0)  fCoords = new Double_t[fDim];

   if(gDebug) {
      if(fCoords == 0) {
         Error("TFoamVect", "Constructor failed to allocate fCoords\n");
      }
   }

   for(Int_t i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];

}

////////////////////////////////////////////////////////////////////////////////
/// Destructor

TFoamVect::~TFoamVect()
{
   if(gDebug>=3) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
   delete [] fCoords; //  free(fCoords)
   fCoords=0;
}

////////////////////////////////////////////////////////////////////////////////
/// substitution operator

TFoamVect& TFoamVect::operator =(const TFoamVect& Vect)
{
   Int_t i;
   if (&Vect == this) return *this;
   if( Vect.fDim < 0 )
       Error("TFoamVect","operator= : invalid  dimensions : %d and %d \n ",fDim,Vect.fDim);
   if( fDim != Vect.fDim ) {  // cleanup
      delete [] fCoords;
      fCoords = new Double_t[Vect.fDim];
   }
   fDim=Vect.fDim;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];
   if(gDebug>=3)  Info("TFoamVect", "SUBSITUTE operator =\n ");
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// [] is for access to elements as in ordinary matrix like a[j]=b[j]
/// (Perhaps against some strict rules but rather practical.)
/// Range protection is built in, consequently for substitution
/// one should use rather use a=b than explicit loop!

Double_t &TFoamVect::operator[](Int_t n)
{
   if ((n<0) || (n>=fDim)) {
      Error( "TFoamVect","operator[], out of range \n");
   }
   return fCoords[n];
}

////////////////////////////////////////////////////////////////////////////////
/// unary multiplication operator *=

TFoamVect& TFoamVect::operator*=(const Double_t &x)
{
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]*x;
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// unary addition operator +=; adding vector c*=x,

TFoamVect& TFoamVect::operator+=(const TFoamVect& Shift)
{
   if( fDim != Shift.fDim){
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]+Shift.fCoords[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// unary subtraction operator -=

TFoamVect& TFoamVect::operator-=(const TFoamVect& Shift)
{
   if( fDim != Shift.fDim) {
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]-Shift.fCoords[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// addition operator +; sum of 2 vectors: c=a+b, a=a+b,
/// NEVER USE IT, VERY SLOW!!!

TFoamVect TFoamVect::operator+(const TFoamVect &p2)
{
   TFoamVect temp(fDim);
   temp  = (*this);
   temp += p2;
   return temp;
}

////////////////////////////////////////////////////////////////////////////////
/// subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
/// NEVER USE IT, VERY SLOW!!!

TFoamVect TFoamVect::operator-(const TFoamVect &p2)
{
   TFoamVect temp(fDim);
   temp  = (*this);
   temp -= p2;
   return temp;
}

////////////////////////////////////////////////////////////////////////////////
/// Loading in ordinary double prec. vector, sometimes can be useful

TFoamVect& TFoamVect::operator =(Double_t Vect[])
{
   Int_t i;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect[i];
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// Loading in double prec. number, sometimes can be useful

TFoamVect& TFoamVect::operator =(Double_t x)
{
   if(fCoords != 0) {
      for(Int_t i=0; i<fDim; i++)
         fCoords[i] = x;
   }
   return *this;
}

////////////////////////////////////////////////////////////////////////////////
/// Printout of all vector components on "std::cout"

void TFoamVect::Print(Option_t *option) const
{
   if(!option) Error("Print ", "No option set \n");
   Int_t i;
   Int_t pr = std::cout.precision(7);
   std::cout << "(";
   for(i=0; i<fDim-1; i++) std::cout  << std::setw(12) << *(fCoords+i) << ",";
   std::cout  << std::setw(12) << *(fCoords+fDim-1);
   std::cout << ")";
   std::cout.precision(pr);
}
<|endoftext|>


<commit_before>#include "mbed.h"

#define READBUFFERSIZE (32)

Serial g_serial(USBTX, USBRX);

int ReadLineString( Serial& serial,
                    char szReadBuffer[], const int ciReadBufferSize, int& riIndexChar,
                    char szLineString[], const int ciLineStringSize )
{
    while( 1 )
    {
        if( !serial.readable() )
        {
            break;
        }
        char c = serial.getc();
        if( '\r' == c  )
        {
            szReadBuffer[riIndexChar] = '\0';
            strncpy( szLineString, szReadBuffer, ciLineStringSize - 1 );
            szLineString[ciLineStringSize - 1] = '\0';
            riIndexChar = 0;
            return 1;
        }
        else if( '\n' == c )
        {
            ;
        }
        else
        {
            if( (ciReadBufferSize - 1) > riIndexChar )
            {
                szReadBuffer[riIndexChar] = c;
                riIndexChar++;
            }
        }
    }

    return 0;
}

int main()
{
    // setup
    g_serial.baud(9600);
    wait(0.001);

    // loop
    char szReadBuffer[READBUFFERSIZE] = "";
    int iIndexChar = 0;
    while(1)
    {
        char szLineString[READBUFFERSIZE];
        if( !ReadLineString( g_serial,
                             szReadBuffer, READBUFFERSIZE, iIndexChar,
                             szLineString, READBUFFERSIZE ) )
        {
            continue;
        }
    }
}
<commit_msg>前回のプログラムみたいに複雑でなくても送受信出来るため、そちらに変更<commit_after>#include "mbed.h"

#define READBUFFERSIZE (32)

Serial pc(USBTX, USBRX);

int main()
{
    char buffer[20];
    while(1){
        if (pc.readable()){
            pc.scanf("%s", &buffer);
        }
        pc.printf("%s\n", buffer);
    }
}<|endoftext|>


<commit_before>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int main () {
  int K;
  cin >> K;
  ll A[100010];
  for (auto i = 0; i < K; ++i) {
    cin >> A[i];
  }
  reverse(A, A+K);
  if (A[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  ll maxi = 3;
  ll mini = 2;
  for (auto i = 1; i < K; ++i) {
    ll tmini = (mini/A[i]) * A[i];
    if (mini > tmini) {
      tmini += A[i];
    }
    if (tmini > maxi) {
      cout << -1 << endl;
      return 0;
    }
    ll tmaxi = tmini + A[i] - 1;
    maxi = tmaxi;
    mini = tmini;
  }
  cout << mini << " " << maxi << endl;
}
<commit_msg>submit B.cpp to 'B - Ice Rink Game' (agc020) [C++14 (GCC 5.4.1)]<commit_after>#include <iostream>
#include <iomanip> // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple> // get<n>(xxx)
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set> // S.insert(M);
// if (S.find(key) != S.end()) { }
// for (auto it=S.begin(); it != S.end(); it++) { }
// auto it = S.lower_bound(M);
#include <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib> // atoi(xxx)
using namespace std;

#define DEBUG 0 // change 0 -> 1 if we need debug.
// insert #if<tab> by my emacs. #if DEBUG == 1 ... #end

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int main () {
  int K;
  cin >> K;
  ll A[100010];
  for (auto i = 0; i < K; ++i) {
    cin >> A[i];
  }
  reverse(A, A+K);
  if (A[0] != 2) {
    cout << -1 << endl;
    return 0;
  }
  ll maxi = 3;
  ll mini = 2;
  for (auto i = 1; i < K; ++i) {
    ll tmini = (mini/A[i]) * A[i];
    if (mini > tmini) {
      tmini += A[i];
    }
    if (tmini > maxi) {
      cout << -1 << endl;
      return 0;
    }
    ll tmaxi = (maxi/A[i]) * A[i] + A[i] - 1;
    maxi = tmaxi;
    mini = tmini;
  }
  cout << mini << " " << maxi << endl;
}
<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-6-2 21:52:44
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

int N;
ll X;
ll b[100010];
ll l[100010];
ll u[100010];
ll maxi = 0;
ll now = 0;
ll ans = 0;

typedef tuple<ll, ll, ll, ll> card; // maxi, b, l, u;
typedef tuple<ll, ll, ll> card2;    // b, l, u;

vector<card> V;

int main()
{
  cin >> N >> X;
  for (auto i = 0; i < N; i++)
  {
    cin >> b[i] >> l[i] >> u[i];
  }
  for (auto i = 0; i < N; i++)
  {
    maxi += b[i] * l[i];
  }
  for (auto i = 0; i < N; i++)
  {
    ll t = (l[i] * b[i] + u[i] * (X - b[i]));
    V.emplace_back(t, b[i], l[i], u[i]);
  }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  int ind = 0;
  while (true)
  {
    ll t, b, l, u;
    tie(t, b, l, u) = V[ind++];
    if (now + t >= maxi)
    {
      break;
    }
    now += t;
  }
  ans = X;
  ll dx = maxi - now;
  for (auto i = ind; i < N; i++)
  {
    ll t, b, l, u;
    tie(t, b, l, u) = V[i];
    ll tmp = 0;
    if (dx >= l * b)
    {
      tmp = b + (dx - l * b + u - 1) / u;
    }
    else
    {
      tmp = (dx + b - 1) / b;
    }
    ans = min(tmp, ans);
  }
  cout << ans + X * ind << endl;
}<commit_msg>tried C.cpp to 'C'<commit_after>#define DEBUG 1

/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-6-2 21:52:44
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

int N;
ll X;
ll b[100010];
ll l[100010];
ll u[100010];
ll maxi = 0;
ll now = 0;
ll ans = 0;

typedef tuple<ll, ll, ll, ll> card; // maxi, b, l, u;
typedef tuple<ll, ll, ll> card2;    // b, l, u;

vector<card> V;

int main()
{
  cin >> N >> X;
  for (auto i = 0; i < N; i++)
  {
    cin >> b[i] >> l[i] >> u[i];
  }
  for (auto i = 0; i < N; i++)
  {
    maxi += b[i] * l[i];
  }
  for (auto i = 0; i < N; i++)
  {
    ll t = (l[i] * b[i] + u[i] * (X - b[i]));
    V.emplace_back(t, b[i], l[i], u[i]);
  }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  int ind = 0;
  while (true)
  {
    ll t, b, l, u;
    tie(t, b, l, u) = V[ind++];
    if (now + t >= maxi)
    {
      break;
    }
    now += t;
  }
  ans = X;
  ll dx = maxi - now;
#if DEBUG == 1
  cerr << "dx = " << maxi << " - " << now << " = " << dx << endl;
#endif
  for (auto i = ind; i < N; i++)
  {
    ll t, b, l, u;
    tie(t, b, l, u) = V[i];
    ll tmp = 0;
    if (dx >= l * b)
    {
      tmp = b + (dx - l * b + u - 1) / u;
    }
    else
    {
      tmp = (dx + b - 1) / b;
    }
    ans = min(tmp, ans);
  }
  cout << ans + X * ind << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:04:45 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

const ll MOD = 1000000007;

int N, M;
bool ok[100010];
ll dp[100010];

int main()
{
  cin >> N >> M;
  fill(ok, ok + 100010, true);
  for (auto i = 0; i < M; i++)
  {
    int a;
    cin >> a;
    a--;
    ok[a] = false;
  }
  dp[0] = 1;
  for (auto i = 0; i < N; i++)
  {
    if (ok[i + 1])
    {
      dp[i + 1] += dp[i];
      dp[i + 1] %= MOD;
    }
    if (ok[i + 2])
    {
      dp[i + 2] += dp[i];
      dp[i + 2] %= MOD;
    }
  }
  cout << dp[N] << endl;
}<commit_msg>submit C.cpp to 'C - Typical Stairs' (abc129) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1

/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:04:45 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

const ll MOD = 1000000007;

int N, M;
bool ok[100010];
ll dp[100010];

int main()
{
  cin >> N >> M;
  fill(ok, ok + 100010, true);
  for (auto i = 0; i < M; i++)
  {
    int a;
    cin >> a;
    ok[a] = false;
  }
  dp[0] = 1;
  for (auto i = 0; i < N; i++)
  {
    if (ok[i + 1])
    {
      dp[i + 1] += dp[i];
      dp[i + 1] %= MOD;
    }
    if (ok[i + 2])
    {
      dp[i + 2] += dp[i];
      dp[i + 2] %= MOD;
    }
  }
  cout << dp[N] << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:09:02 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

int H, W;
string S[2010];
bool X[2010][2010];
ll A[2010][2010];
ll B[2010][2010];

ll solve()
{
  for (auto i = 0; i < H; i++)
  {
    ll t = 0;
    for (auto j = 0; j < W; j++)
    {
      if (X[i][j])
      {
        t++;
      }
      else
      {
        for (auto k = j; k > j - t; k--)
        {
          A[i][k] = t;
        }
        t = 0;
      }
    }
    for (auto k = W - 1; k > W - 1 - t; k--)
    {
      A[i][k] = t;
    }
  }
  for (auto j = 0; j < W; j++)
  {
    ll t = 0;
    for (auto i = 0; i < H; i++)
    {
      if (X[i][j])
      {
        t++;
      }
      else
      {
        for (auto k = j; k > i - t; k--)
        {
          B[k][j] = t;
        }
        t = 0;
      }
    }
    for (auto k = H - 1; k > H - 1 - t; k--)
    {
      B[k][j] = t;
    }
  }
  ll ans = 0;
  for (auto i = 0; i < H; i++)
  {
    for (auto j = 0; j < W; j++)
    {
      ans = max(ans, A[i][j] + B[i][j]);
    }
  }
  return ans;
}

int main()
{
  cin >> H >> W;
  for (auto i = 0; i < H; i++)
  {
    cin >> S[i];
  }
  for (auto i = 0; i < H; i++)
  {
    for (auto j = 0; j < W; j++)
    {
      X[i][j] = (S[i][j] == '.');
    }
  }
  cout << solve() << endl;
}<commit_msg>submit D.cpp to 'D - Lamp' (abc129) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:09:02 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

/*
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}

void No()
{
  cout << "No" << endl;
  exit(0);
}
*/

/*
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
*/

// const ll MOD = 1000000007;

int H, W;
string S[2010];
bool X[2010][2010];
ll A[2010][2010];
ll B[2010][2010];

ll solve()
{
  for (auto i = 0; i < H; i++)
  {
    ll t = 0;
    for (auto j = 0; j < W; j++)
    {
      if (X[i][j])
      {
        t++;
      }
      else
      {
        for (auto k = j; k > j - t; k--)
        {
          A[i][k] = t;
        }
        t = 0;
      }
    }
    for (auto k = W - 1; k > W - 1 - t; k--)
    {
      A[i][k] = t;
    }
  }
  for (auto j = 0; j < W; j++)
  {
    ll t = 0;
    for (auto i = 0; i < H; i++)
    {
      if (X[i][j])
      {
        t++;
      }
      else
      {
        for (auto k = j; k > i - t; k--)
        {
          B[k][j] = t;
        }
        t = 0;
      }
    }
    for (auto k = H - 1; k > H - 1 - t; k--)
    {
      B[k][j] = t;
    }
  }
  ll ans = 0;
  for (auto i = 0; i < H; i++)
  {
    for (auto j = 0; j < W; j++)
    {
      ans = max(ans, A[i][j] + B[i][j]);
    }
  }
  return ans;
}

int main()
{
  cin >> H >> W;
  for (auto i = 0; i < H; i++)
  {
    cin >> S[i];
  }
  for (auto i = 0; i < H; i++)
  {
    for (auto j = 0; j < W; j++)
    {
      X[i][j] = (S[i][j] == '.');
    }
  }
  cout << solve() - 1 << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 6/16/2019, 9:18:50 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
typedef long long ll;
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
const int MAX_SIZE = 1000010;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
mint inv[MAX_SIZE];
mint fact[MAX_SIZE];
mint factinv[MAX_SIZE];
void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = mint(i) * fact[i - 1];
    factinv[i] = inv[i] * factinv[i - 1];
  }
}
mint choose(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return fact[n] * factinv[k] * factinv[n - k];
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

int N, M;
int S[2010];
int T[2010];

mint dp[2010][2010];

int main()
{
  // init();
  cin >> N >> M;
  for (auto i = 0; i < N; i++)
  {
    cin >> S[i];
  }
  for (auto i = 0; i < M; i++)
  {
    cin >> T[i];
  }
  for (auto j = 0; j <= M; j++)
  {
    dp[0][j] = 1;
  }
  for (auto i = 1; i <= N; i++)
  {
    for (auto j = 0; j <= M; j++)
    {
      dp[i][j] = dp[i - 1][j];
      if (j > 0 && S[i - 1] == T[j - 1])
      {
        dp[i][j] += dp[i - 1][j - 1];
      }
    }
  }
#if DEBUG == 1
  for (auto i = 0; i <= N; i++)
  {
    for (auto j = 0; j <= M; j++)
    {
      cerr << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
    }
  }
#endif
  cout << dp[N][M] << endl;
}<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 6/16/2019, 9:18:50 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
typedef long long ll;
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
const int MAX_SIZE = 1000010;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
mint inv[MAX_SIZE];
mint fact[MAX_SIZE];
mint factinv[MAX_SIZE];
void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = mint(i) * fact[i - 1];
    factinv[i] = inv[i] * factinv[i - 1];
  }
}
mint choose(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return fact[n] * factinv[k] * factinv[n - k];
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

int N, M;
int S[2010];
int T[2010];

vector<int> chars[100010];

mint dp[2010][2010];
bool visited[2010][2010];

mint calc(int x, int y)
{
  if (visited[x][y])
  {
    return dp[x][y];
  }
  dp[x][y] = calc(x - 1, y);
  for (auto e : chars[S[x - 1]])
  {
    dp[x][y] += calc(x - 1, e);
  }
  visited[x][y] = true;
  return dp[x][y];
}

int main()
{
  // init();
  cin >> N >> M;
  for (auto i = 0; i < N; i++)
  {
    cin >> S[i];
  }
  for (auto i = 0; i < M; i++)
  {
    cin >> T[i];
  }
  for (auto i = 0; i < M; i++)
  {
    chars[T[i]].push_back(i);
  }
  fill(&visited[0][0], &visited[0][0] + 2010 * 2010, false);
  for (auto j = 0; j <= M; j++)
  {
    dp[0][j] = 1;
    visited[0][j] = true;
  }
  cout << calc(N, M) << endl;
#if DEBUG == 1
  for (auto i = 0; i <= N; i++)
  {
    for (auto j = 0; j <= M; j++)
    {
      cerr << "calc(" << i << ", " << j << ") = " << calc(i, j) << endl;
    }
  }
#endif
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 6/18/2019, 2:02:05 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
typedef long long ll;
void Yes()
{
  cout << "YES" << endl;
  exit(0);
}
void No()
{
  cout << "NO" << endl;
  exit(0);
}
const int MAX_SIZE = 1000010;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
mint inv[MAX_SIZE];
mint fact[MAX_SIZE];
mint factinv[MAX_SIZE];
void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = mint(i) * fact[i - 1];
    factinv[i] = inv[i] * factinv[i - 1];
  }
}
mint choose(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return fact[n] * factinv[k] * factinv[n - k];
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

typedef vector<string> ban;
const int C = 19;

enum class state
{
  black_win,
  white_win,
  black_even,
  white_even,
  error,
};

class Go
{
public:
  ban B;
  int cnt[2];
  Go() {}
  Go(ban V) : B(V)
  {
    cnt[0] = cnt[1] = 0;
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        if (B[i][j] == 'o')
        {
          cnt[0]++;
        }
        else if (B[i][j] == 'x')
        {
          cnt[1]++;
        }
      }
    }
  }

  bool win(char c)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        for (auto a = -1; a < 2; a++)
        {
          for (auto b = -1; b < 2; b++)
          {
            if (a == 0 && b == 0)
            {
              continue;
            }
            bool ok = true;
            for (auto k = 0; k < 5; k++)
            {
              int x = i + a * k;
              int y = j + b * k;
              if (0 <= x && x < C && 0 <= y && y < C && B[x][y] != c)
              {
                ok = false;
                break;
              }
            }
            if (ok)
            {
              return true;
            }
          }
        }
      }
    }
    return false;
  }

  state st()
  {
    int t = cnt[0] - cnt[1];
    if (!(t == 0 || t == 1))
    {
      return state::error;
    }
    bool black_turn = (t == 1);
    bool black_win = win('o');
    bool white_win = win('x');
    if (black_win && white_win)
    {
      return state::error;
    }
    else if (black_win)
    {
      if (black_turn)
      {
        return state::black_win;
      }
      else
      {
        return state::error;
      }
    }
    else if (white_win)
    {
      if (!black_turn)
      {
        return state::white_win;
      }
      else
      {
        return state::error;
      }
    }
    else
    {
      if (black_turn)
      {
        return state::black_even;
      }
      else
      {
        return state::white_even;
      }
    }
  }
};

int main()
{
  // init();
  ban B;
  for (auto i = 0; i < C; i++)
  {
    string S;
    cin >> S;
    B.push_back(S);
  }
  Go I(B);
  state I_state = I.st();
  if (I_state == state::error)
  {
    No();
  }
  if (I_state == state::white_even || I_state == state::black_even)
  {
    Yes();
  }
  if (I_state == state::black_win)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        Go before = I;
        if (before.B[i][j] == 'o')
        {
          before.B[i][j] = '.';
          before.cnt[0]--;
          if (before.st() == state::white_even)
          {
            Yes();
          }
        }
      }
    }
    No();
  }
  if (I_state == state::white_win)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        Go before = I;
        if (before.B[i][j] == 'x')
        {
          before.B[i][j] = '.';
          before.cnt[1]--;
          if (before.st() == state::black_even)
          {
            Yes();
          }
        }
      }
    }
    No();
  }
}<commit_msg>submit C.cpp to 'C - 五目並べチェッカー' (arc012) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : C.cpp
 * Author  : Kazune Takahashi
 * Created : 6/18/2019, 2:02:05 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
typedef long long ll;
void Yes()
{
  cout << "YES" << endl;
  exit(0);
}
void No()
{
  cout << "NO" << endl;
  exit(0);
}
const int MAX_SIZE = 1000010;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
mint inv[MAX_SIZE];
mint fact[MAX_SIZE];
mint factinv[MAX_SIZE];
void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = mint(i) * fact[i - 1];
    factinv[i] = inv[i] * factinv[i - 1];
  }
}
mint choose(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return fact[n] * factinv[k] * factinv[n - k];
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

typedef vector<string> ban;
const int C = 19;

enum class state
{
  black_win,
  white_win,
  black_even,
  white_even,
  error,
};

class Go
{
public:
  ban B;
  int cnt[2];
  Go() {}
  Go(ban V) : B(V)
  {
    cnt[0] = cnt[1] = 0;
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        if (B[i][j] == 'o')
        {
          cnt[0]++;
        }
        else if (B[i][j] == 'x')
        {
          cnt[1]++;
        }
      }
    }
  }

  bool win(char c)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        for (auto a = -1; a < 2; a++)
        {
          for (auto b = -1; b < 2; b++)
          {
            if (a == 0 && b == 0)
            {
              continue;
            }
            bool ok = true;
            for (auto k = 0; k < 5; k++)
            {
              int x = i + a * k;
              int y = j + b * k;
              if (0 <= x && x < C && 0 <= y && y < C && B[x][y] != c)
              {
                ok = false;
                break;
              }
            }
            if (ok)
            {
              return true;
            }
          }
        }
      }
    }
    return false;
  }

  state st()
  {
    int t = cnt[0] - cnt[1];
    if (!(t == 0 || t == 1))
    {
      return state::error;
    }
    bool black_turn = (t == 1);
    bool black_win = win('o');
    bool white_win = win('x');
    if (black_win && white_win)
    {
      return state::error;
    }
    else if (black_win)
    {
      if (black_turn)
      {
        return state::black_win;
      }
      else
      {
        return state::error;
      }
    }
    else if (white_win)
    {
      if (!black_turn)
      {
        return state::white_win;
      }
      else
      {
        return state::error;
      }
    }
    else
    {
      if (black_turn)
      {
        return state::black_even;
      }
      else
      {
        return state::white_even;
      }
    }
  }
};

int main()
{
  // init();
  ban B;
  for (auto i = 0; i < C; i++)
  {
    string S;
    cin >> S;
    B.push_back(S);
  }
  Go I(B);
  state I_state = I.st();
  if (I_state == state::error)
  {
    No();
  }
  if (I_state == state::white_even || I_state == state::black_even)
  {
    Yes();
  }
  if (I_state == state::black_win)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        Go before(B);
        if (before.B[i][j] == 'o')
        {
          before.B[i][j] = '.';
          before.cnt[0]--;
          if (before.st() == state::white_even)
          {
            Yes();
          }
        }
      }
    }
    No();
  }
  if (I_state == state::white_win)
  {
    for (auto i = 0; i < C; i++)
    {
      for (auto j = 0; j < C; j++)
      {
        Go before = I;
        if (before.B[i][j] == 'x')
        {
          before.B[i][j] = '.';
          before.cnt[1]--;
          if (before.st() == state::black_even)
          {
            Yes();
          }
        }
      }
    }
    No();
  }
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 5/23/2020, 3:38:16 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
// constexpr ll MOD{1000000007LL};
constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

// ----- SegTree -----

template <typename T>
class SegTree
{ // 0-indexed, [0, N).
private:
  int N;
  vector<T> dat;
  T unit;  // モノイドの単位元
  T(*func) // モノイドの演算
  (T, T);
  T(*_update) // update で値をどうするか書く
  (T, T);

public:
  SegTree() {}

  SegTree(int n, T unit, T (*func)(T, T), T (*_update)(T, T)) : N{1}, unit{unit}, func{func}, _update{_update}
  {
    while (N < n)
    {
      N *= 2;
    }
    dat = vector<T>(2 * N - 1, unit);
  }

  void update(int k, T a)
  {
    k += N - 1;
    dat[k] = _update(dat[k], a);
    while (k > 0)
    {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T find(int a, int b, int k, int l, int r)
  {
    if (r <= a || b <= l)
    {
      return unit;
    }
    if (a <= l && r <= b)
    {
      return dat[k];
    }
    T vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

public:
  T find(int a, int b)
  { // [a, b) の find をする。
    return find(a, b, 0, 0, N);
  }
};

// ----- frequently used examples -----

// for +
auto func2 = [](auto x, auto y) {
  return x + y;
};
auto _update2 = [](auto x, auto y) {
  return y;
};
constexpr ll unit2{0LL};
// SegTree<ll> tree2{N, unit2, func2, _update2};

// ----- Compressor -----
// referring to ymatsux-san's code:
// https://atcoder.jp/contests/abc168/submissions/13337691

template <typename T = ll>
class Compressor
{
  vector<T> raw;
  map<T, int> index;

public:
  Compressor() {}

  template <typename Container>
  Compressor(Container const &V) { append(V); }

  template <typename Iter>
  Compressor(Iter first, Iter last) { append(first, last); }

  template <typename Container>
  void append(Container const &V) { append(V.begin(), V.end()); }

  template <typename Iter>
  void append(Iter first, Iter last)
  {
    set<T> S(first, last);
    raw = vector<T>(S.begin(), S.end());
    sort(raw.begin(), raw.end());
    for (auto i = size_t{0}; i < raw.size(); ++i)
    {
      index[raw[i]] = i;
    }
  }

  T to_raw(int i) { return raw[i]; }
  int to_index(T t) { return index[t]; }
};

// ----- main() -----

using Point = tuple<int, int>;
const mint two{2};

int main()
{
  ll N;
  cin >> N;
  vector<Point> P(N);
  vector<int> VX, VY;
  for (auto i = 0LL; i < N; ++i)
  {
    int x, y;
    cin >> x >> y;
    P[i] = Point{x, y};
    VX.push_back(x);
    VY.push_back(y);
  }
  Compressor<int> X{VX}, Y{VY};
  vector<int> I(N);
  for (auto i = 0LL; i < N; ++i)
  {
    int x, y;
    tie(x, y) = P[i];
    I[X.to_index(x)] = Y.to_index(y);
  }
  SegTree<ll> tree{N, unit2, func2, _update2};
  mint ans{0};
  for (auto i = 0LL; i < N; ++i)
  {
    ll a{tree.find(0, i)};
    ll b{i - a};
    ll c{I[i] - a};
    ll d{N - a - b - c - 1};
    ans += two.power(N) - 1 - two.power(a + b) - two.power(a + c) - two.power(b + d) - two.power(c + d) + two.power(a) + two.power(b) + two.power(c) + two.power(d);
    tree.update(I[i], 1);
  }
  cout << ans << endl;
}
<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 5/23/2020, 3:38:16 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
// constexpr ll MOD{1000000007LL};
constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

// ----- SegTree -----

template <typename T>
class SegTree
{ // 0-indexed, [0, N).
private:
  int N;
  vector<T> dat;
  T unit;  // モノイドの単位元
  T(*func) // モノイドの演算
  (T, T);
  T(*_update) // update で値をどうするか書く
  (T, T);

public:
  SegTree() {}

  SegTree(int n, T unit, T (*func)(T, T), T (*_update)(T, T)) : N{1}, unit{unit}, func{func}, _update{_update}
  {
    while (N < n)
    {
      N *= 2;
    }
    dat = vector<T>(2 * N - 1, unit);
  }

  void update(int k, T a)
  {
    k += N - 1;
    dat[k] = _update(dat[k], a);
    while (k > 0)
    {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T find(int a, int b, int k, int l, int r)
  {
    if (r <= a || b <= l)
    {
      return unit;
    }
    if (a <= l && r <= b)
    {
      return dat[k];
    }
    T vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

public:
  T find(int a, int b)
  { // [a, b) の find をする。
    return find(a, b, 0, 0, N);
  }
};

// ----- frequently used examples -----

// for +
auto func2 = [](auto x, auto y) {
  return x + y;
};
auto _update2 = [](auto x, auto y) {
  return y;
};
constexpr ll unit2{0LL};
// SegTree<ll> tree2{N, unit2, func2, _update2};

// ----- Compressor -----
// referring to ymatsux-san's code:
// https://atcoder.jp/contests/abc168/submissions/13337691

template <typename T = ll>
class Compressor
{
  vector<T> raw;
  map<T, int> index;

public:
  Compressor() {}

  template <typename Container>
  Compressor(Container const &V) { append(V); }

  template <typename Iter>
  Compressor(Iter first, Iter last) { append(first, last); }

  template <typename Container>
  void append(Container const &V) { append(V.begin(), V.end()); }

  template <typename Iter>
  void append(Iter first, Iter last)
  {
    set<T> S(first, last);
    raw = vector<T>(S.begin(), S.end());
    sort(raw.begin(), raw.end());
    for (auto i = size_t{0}; i < raw.size(); ++i)
    {
      index[raw[i]] = i;
    }
  }

  T to_raw(int i) { return raw[i]; }
  int to_index(T t) { return index[t]; }
};

// ----- main() -----

using Point = tuple<int, int>;
const mint two{2};

int main()
{
  ll N;
  cin >> N;
  vector<Point> P(N);
  vector<int> VX, VY;
  for (auto i = 0LL; i < N; ++i)
  {
    int x, y;
    cin >> x >> y;
    P[i] = Point{x, y};
    VX.push_back(x);
    VY.push_back(y);
  }
  Compressor<int> X{VX}, Y{VY};
  vector<int> I(N);
  for (auto i = 0LL; i < N; ++i)
  {
    int x, y;
    tie(x, y) = P[i];
    I[X.to_index(x)] = Y.to_index(y);
  }
  SegTree<ll> tree{static_cast<int>(N), unit2, func2, _update2};
  mint ans{0};
  for (auto i = 0LL; i < N; ++i)
  {
    ll a{tree.find(0, i)};
    ll b{i - a};
    ll c{I[i] - a};
    ll d{N - a - b - c - 1};
#if DEBUG == 0
    cerr << "I[" << i << "] = " << I[i] << endl;
    cerr << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;
#endif
    ans += two.power(N) - 1 - two.power(a + b) - two.power(a + c) - two.power(b + d) - two.power(c + d) + two.power(a) + two.power(b) + two.power(c) + two.power(d);
    tree.update(I[i], 1);
  }
  cout << ans << endl;
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/9/1 21:13:18
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}

int N;
vector<queue<int>> V;

int main()
{
  cin >> N;
  for (auto i = 0; i < N; i++)
  {
    for (auto j = 0; j < N - 1; j++)
    {
      int a;
      cin >> a;
      --a;
      V[i].push(a);
    }
  }
#if DEBUG == 1
  cerr << "aaa" << endl;
#endif
  vector<bool> used(N, false);
  int ans{0};
  while (true)
  {
    fill(used.begin(), used.end(), false);
    for (auto i = 0; i < N; i++)
    {
      if (V[i].empty())
      {
        No();
      }
      int j = V[i].front();
      if (V[j].empty())
      {
        No();
      }
      if (V[j].front() == i)
      {
        used[i] = used[j] = true;
      }
    }
    int cnt{0};
    for (auto i = 0; i < N; i++)
    {
      if (used[i])
      {
        V[i].pop();
      }
      ++cnt;
    }
    int emp{0};
    for (auto i = 0; i < N; i++)
    {
      if (V[i].empty())
      {
        ++emp;
      }
    }
    if (emp == N)
    {
      cout << ans << endl;
      return 0;
    }
    if (cnt == 0)
    {
      No();
    }
  }
}<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/9/1 21:13:18
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}

int N;
vector<queue<int>> V;

int main()
{
  cin >> N;
#if DEBUG == 1
  cerr << "aaa" << endl;
#endif
  for (auto i = 0; i < N; i++)
  {
    for (auto j = 0; j < N - 1; j++)
    {
      int a;
      cin >> a;
      --a;
      V[i].push(a);
    }
  }
  vector<bool> used(N, false);
  int ans{0};
  while (true)
  {
    fill(used.begin(), used.end(), false);
    for (auto i = 0; i < N; i++)
    {
      if (V[i].empty())
      {
        No();
      }
      int j = V[i].front();
      if (V[j].empty())
      {
        No();
      }
      if (V[j].front() == i)
      {
        used[i] = used[j] = true;
      }
    }
    int cnt{0};
    for (auto i = 0; i < N; i++)
    {
      if (used[i])
      {
        V[i].pop();
      }
      ++cnt;
    }
    int emp{0};
    for (auto i = 0; i < N; i++)
    {
      if (V[i].empty())
      {
        ++emp;
      }
    }
    if (emp == N)
    {
      cout << ans << endl;
      return 0;
    }
    if (cnt == 0)
    {
      No();
    }
  }
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 11/18/2019, 8:58:07 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
// ----- boost -----
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using boost::rational;
using namespace std;
using ll = long long;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{3000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{x % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

void add_edge(map<int, int> &m, int h, int d)
{
  if (m.find(h) == m.end())
  {
    m[h] = d;
  }
  else
  {
    ch_min(m[h], d);
  }
}

void calc_high(vector<int> const &A, vector<map<int, int>> &M, int i, int h, int d)
{
  add_edge(M[i + 1], A[i + 1] - (A[i] - h), d);
  add_edge(M[i + 1], h, d + 1);
  add_edge(M[i + 1], 0, d + 2);
}

void calc_low(vector<int> const &A, vector<map<int, int>> &M, int i, int h, int d)
{
  if (h <= A[i + 1])
  {
    add_edge(M[i + 1], h, d);
  }
  add_edge(M[i + 1], max(0, A[i + 1] - (A[i] - h)), d + 1);
  add_edge(M[i + 1], 0, d + 2);
}

void eliminate(map<int, int> &m)
{
  int now{m[0]};
  auto k{make_unique<map<int, int>>()};
  for (auto const &x : m)
  {
    if (x.second == now)
    {
      k->insert(x);
      --now;
    }
  }
  swap(m, *k);
}

void calc(vector<int> const &A, vector<map<int, int>> &M, int i, pair<int, int> const &x)
{
  int h{x.first};
  int d{x.second};
  if (A[i] <= A[i + 1])
  {
    calc_high(A, M, i, h, d);
  }
  else
  {
    calc_low(A, M, i, h, d);
  }
#if DEBUG == 1
  for (auto const &x : M[i + 1])
  {
    cerr << "M[" << i + 1 << "][" << x.first << "] = " << x.second << endl;
  }
#endif
  eliminate(M[i + 1]);
#if DEBUG == 1
  for (auto const &x : M[i + 1])
  {
    cerr << "M[" << i + 1 << "][" << x.first << "] = " << x.second << endl;
  }
#endif
}

int main()
{
  int N;
  cin >> N;
  vector<int> A(N + 2, 0);
  for (auto i = 1; i <= N; i++)
  {
    cin >> A[i];
  }
  vector<map<int, int>> M(N + 2);
  M[0][0] = 0;
  for (auto i = 0; i <= N; i++)
  {
    for (auto const &x : M[i])
    {
      calc(A, M, i, x);
    }
  }
  cout << M[N + 1].end()->second << endl;
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 11/18/2019, 8:58:07 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
// ----- boost -----
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using boost::rational;
using namespace std;
using ll = long long;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{3000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{x % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

void add_edge(map<int, int> &m, int h, int d)
{
  if (m.find(h) == m.end())
  {
    m[h] = d;
  }
  else
  {
    ch_min(m[h], d);
  }
}

void calc_high(vector<int> const &A, vector<map<int, int>> &M, int i, int h, int d)
{
  add_edge(M[i + 1], A[i + 1] - (A[i] - h), d);
  add_edge(M[i + 1], h, d + 1);
  add_edge(M[i + 1], 0, d + 2);
}

void calc_low(vector<int> const &A, vector<map<int, int>> &M, int i, int h, int d)
{
  if (h <= A[i + 1])
  {
    add_edge(M[i + 1], h, d);
  }
  add_edge(M[i + 1], max(0, A[i + 1] - (A[i] - h)), d + 1);
  add_edge(M[i + 1], 0, d + 2);
}

void eliminate(map<int, int> &m)
{
  int now{m[0]};
  auto k{make_unique<map<int, int>>()};
  for (auto const &x : m)
  {
    if (x.second == now)
    {
      k->insert(x);
      --now;
    }
  }
  swap(m, *k);
}

void calc(vector<int> const &A, vector<map<int, int>> &M, int i, pair<int, int> const &x)
{
  int h{x.first};
  int d{x.second};
#if DEBUG == 1
  for (auto const &x : M[i + 1])
  {
    cerr << "M[" << i + 1 << "][" << x.first << "] = " << x.second << endl;
  }
#endif
  if (A[i] <= A[i + 1])
  {
    calc_high(A, M, i, h, d);
  }
  else
  {
    calc_low(A, M, i, h, d);
  }
#if DEBUG == 1
  for (auto const &x : M[i + 1])
  {
    cerr << "M[" << i + 1 << "][" << x.first << "] = " << x.second << endl;
  }
#endif
  eliminate(M[i + 1]);
#if DEBUG == 1
  for (auto const &x : M[i + 1])
  {
    cerr << "M[" << i + 1 << "][" << x.first << "] = " << x.second << endl;
  }
#endif
}

int main()
{
  int N;
  cin >> N;
  vector<int> A(N + 2, 0);
  for (auto i = 1; i <= N; i++)
  {
    cin >> A[i];
  }
  vector<map<int, int>> M(N + 2);
  M[0][0] = 0;
  for (auto i = 0; i <= N; i++)
  {
    for (auto const &x : M[i])
    {
      calc(A, M, i, x);
    }
  }
  cout << M[N + 1].end()->second << endl;
}
<|endoftext|>