[Codechef October Challenge 2014] Brush paint

Source: Internet
Author: User

Problem Description

Czy finished all the answers, and the result was that he became more empty because of his mental energy consumption:). The arduous task of helping Czy regain his stature falls on your shoulders.

It happened that there was a fence in your garden consisting of a block of n blocks lined up in a straight line, and the planks were numbered 1 to n from left to right. There was a barrel of paint in front of the wooden planks of the N-block. The paint has different colors, each color can be represented by an uppercase letter (A to Z). And you asked Czy to paint the fence with his paint brush.

It is known that Czy will start his task by selecting a plank with a paint bucket in front. After the brush dipped on the paint, he began to walk randomly along the fence, and he would not walk out of the fence. Walking randomly means that czy will walk along the direction he chooses, and then randomly change direction at any time. Walk along the fence in only two directions, forward and backward.

You find that the process of czy painting always conforms to the following rules:

    • Each paint barrel is filled with an infinite number of paints;
    • Every time there is only one color paint on the brush, each dip in the paint will completely replace the paint color on the brush;
    • When Czy walked to a paint bucket, he would first dip the paint in the paint bucket with a brush;
    • Czy each plank will brush the wood into the paint color on the current brush.

It is known that the planks can be painted several times, and each time it is completely covered with the previous color. When all the planks were painted, czy could stop (he could, of course, continue to brush until he wanted to stop). You watched Czy come back to the fence and suddenly wondered how many possible different paint schemes the fence had when Czy stopped. The two paint schemes are considered different when at least one piece of wood is not in the same color.

Please output a different number of paint schemes to the value of the 109+9 modulus.

input

The input file is paint.in.

The first line of input contains two integers n and M.

The next m line, two integers x and y per line, indicates that there is a paint bucket in front of the first Y block with a paint color of x.

Output

The output file is paint.out.

The output line contains an integer that represents the result of a different paint scheme number to 109 + 9 modulo.

input/Output sample

Paint.in

Paint.out

6 2

A 2

B 6

4

Data Range

For 30% of data, 1≤m≤n≤100.

For 100% of data, 1≤m≤n≤100000.

X is an uppercase letter from A to Z; 1≤y≤n.

Ideas

Offline processing, sorted by coordinates, number of schemes = scheme number * difference in paint bucket coordinates of all colors.

Constmo=1000000009;typess=RecordCh:char;nu:int64; End;varA:Array[0..100000] ofSS;    Ans,n,m:int64; I:longint;proceduresort (l,r:longint); varI,j,x:longint;y:ss; beginI:=M; J:=R; X:=a[(L+r)Div 2].nu; Repeat            whileA[i].nu<x DoInc (I);  whileX<a[j].nu DoDec (j); if  not(I&GT;J) Then             beginy:=A[i]; A[i]:=A[j]; A[J]:=y;                Inc (I); J:=j-1; End; untilI>J; ifL<j Thensort (l,j); ifI<r Thensort (i,r); End;beginreadln (n,m);  fori:=1  toM Doreadln (a[i].ch,a[i].nu); Sort (1, M); Ans:=1;  fori:=1  toM-1  Do        ifa[i].ch<>a[i+1].ch Thenans:=ans* (ABS (a[i+1].nu-a[i].nu))MoDmo; ifans=0  Thenans:=1; Writeln (ans);End.
View Code

[Codechef October Challenge 2014] Brush paint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.