Codeforces Round #353 (Div. 2) b. Restoring Painting __ map or set, thinking questions

Source: Internet
Author: User

B. Restoring paintingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Vasya works as a Watchman in the gallery. Unfortunately, one of the most expensive paintings is stolen while he is on duty. He doesn ' t want to being fired, so he had to quickly restore the painting. He remembers some facts about it.

  • the painting is a square  3 ? x?3 , each cell contains a single integer From 1  to  n , and different cells may contain either different or equal integers.
  • the sum of integers in each of Four Squares 2?x?2  is equal to the sum of integers in the top left Square 2?x?2 .
  • Four elements a, b, C and D is known and is located as shown on the picture below.

Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number is equal to 0, meaning Vasya remembers something wrong.

Squares is considered to being different, if there exists a cell that contains the different integers in different squar Es.

Input

The first line of the input contains five integersN,a,b,CandD(1?≤? n? ≤?100?000,1?≤? a,? b,? c,? d? ≤? N )-maximum possible value of an integer in the cell and four integers that Vasya remembers.

Output

Print one integer-the number of distinct valid squares.

Examplesinput
2 1 1) 1 2
Output
2
Input
3 3 1) 2 3
Output
6
Note

Below is all the possible paintings for the first sample.

In the second sample, only paintings displayed below satisfy all the rules.



Source

B. Restoring Painting


My Solution

Draw a 3*3 square chart, then mark A, B, C, D and then find the upper left corner marked X, the middle superscript y, and then the remaining 3 spaces can be expressed.

So you can do it with O (n).

Sweep over, using ans[][][][] in the process to indicate that state.

Finally get a different number

Then N*ans.size (), (n means the total number of possible cases in the middle of the number is N), then notice that it might overflow.

And judging, some impossible situations,?? The sample gives a very good conscience

3

1 2

3

This time, the upper left corner can't be 1.

In addition each lattice must be a number of 1<= x <= n

Solution 1 at that time with 4 heavy map of the reckless husband, hey, no mle map<int, Map<int, Map<int, Map<int, int> > > > ans; That's a bit of memory.

#include <iostream> #include <cstdio> #include <map>using namespace std;/*struct q{    int lu, RU, LD, Rd;}; */map<int, Map<int, Map<int, Map<int, int> > > > Ans;int Main () {    int n, a, B, C, D;    scanf ("%d%d%d%d%d", &n, &a, &b, &c, &d);    for (int i = 1; I <= n; i++) {/    *        val.lu = i;        val.ru = i + b-c;        Val.ld = i + a-d;        Val.rd = i + A + b-d-C;        Ans.insert (val);    *        /if (i + b-c <= n && i + b-c > 0 && i + a-d <= n && i + a-d > 0 &&am P i + A + b-d-c <= N && i + A + b-d-C > 0) {            ans[i][i + b-c][i + a-d][i + A + b-d-c]++;
   }    }    long Long val = n;    val = Val*ans.size ();    cout<<val;    return 0;}



Solution 2 Constructs a struct Q, then four members RU (rights up), Lu (lest up), LD (lest down) and RD (right-down), and then overloads good < good, a map on the line

#include <iostream> #include <cstdio> #include <set>using namespace std;struct q{int lu, RU, ld, RD;}    ; bool operator < (const q& A, const q& b) {if (a.lu! = b.lu) return a.lu < b.lu;        else{if (a.rd! = b.rd) return A.ld < B.ld;            else{if (a.rd! = b.rd) return A.rd < B.rd;        else return a.ru < b.ru;    }}//To be fully defined otherwise it may have been overwritten}set<q> Ans;int main () {int n, a, B, C, D;    Q Val;    scanf ("%d%d%d%d%d", &n, &a, &b, &c, &d);        for (int i = 1; I <= n; i++) {val.lu = i;        val.ru = i + b-c;        Val.ld = i + a-d;        Val.rd = i + A + b-d-C; if (val.ru <= n && val.ru > 0 && val.ld <= n && val.ld > 0 && val.rd <= n &A        mp;& val.rd > 0) {ans.insert (val);    }} long Long v = n;    v = v*ans.size ();    cout<<v; return 0;}


Thank you!



------ from Prolights

------ fromProlights

Codeforces Round #353 (Div. 2) b. Restoring Painting __ map or set, thinking questions

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.