码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 863F Almost Permutation

时间:2017-11-14 17:14:50      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:class   技术   lmos   sim   lower   perm   nim   imu   download   

Recently Ivan noticed an array a while debugging his code. Now Ivan can‘t remember this array, but the bug he was trying to fix didn‘t go away, so Ivan thinks that the data from this array might help him to reproduce the bug.

Ivan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers:

  • 1 li ri vi — for each x such that li ≤ x ≤ ri ax ≥ vi;
  • 2 li ri vi — for each x such that li ≤ x ≤ ri ax ≤ vi.

Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:

技术分享, where cnt(i) is the number of occurences of i in the array.

Help Ivan to determine minimum possible cost of the array that corresponds to the facts!

Input

The first line contains two integer numbers n and q (1 ≤ n ≤ 50, 0 ≤ q ≤ 100).

Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ vi ≤ n, ti denotes the type of the fact).

Output

If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.

 

把位置和数字当作点,对于数字的流量,费用是流量的平方

把数字的边拆成费用为1,3,5……流量为1的边,就做到了费用是流量的平方

codeforces 863F Almost Permutation

标签:class   技术   lmos   sim   lower   perm   nim   imu   download   

原文地址:http://www.cnblogs.com/lengtouqing/p/7798989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!