CodeForces985G Team Players

G. Team Players
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn players numbered from 00 to n1n−1 with ranks. The ii-th player has rank ii.

Players can form teams: the team should consist of three players and no pair of players in the team should have a conflict. The rank of the team is calculated using the following algorithm: let iijjkk be the ranks of players in the team and i<j<ki<j<k, then the rank of the team is equal to Ai+Bj+CkA⋅i+B⋅j+C⋅k.

You are given information about the pairs of players who have a conflict. Calculate the total sum of ranks over all possible valid teams modulo 264264.

Input

The first line contains two space-separated integers nn and mm (3n21053≤n≤2⋅1050m21050≤m≤2⋅105) — the number of players and the number of conflicting pairs.

The second line contains three space-separated integers AABB and CC (1A,B,C1061≤A,B,C≤106) — coefficients for team rank calculation.

Each of the next mm lines contains two space-separated integers uiui and vivi (0ui,vi<n,uivi0≤ui,vi<n,ui≠vi) — pair of conflicting players.

It's guaranteed that each unordered pair of players appears in the input file no more than once.

Output

Print single integer — the total sum of ranks over all possible teams modulo 264264.

Examples
input
Copy
4 0
2 3 4
output
Copy
64
input
Copy
4 1
2 3 4
1 0
output
Copy
38
input
Copy
6 4
1 5 3
0 3
3 5
5 4
4 3
output
Copy
164
Note

In the first example all 44 teams are valid, i.e. triples: {0, 1, 2}, {0, 1, 3}, {0, 2, 3} {1, 2, 3}.

In the second example teams are following: {0, 2, 3}, {1, 2, 3}.

In the third example teams are following: {0, 1, 2}, {0, 1, 4}, {0, 1, 5}, {0, 2, 4}, {0, 2, 5}, {1, 2, 3}, {1, 2, 4}, {1, 2, 5}.

AC代码为:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ;i <= y; ++ i)


using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template<typename T>inline void read(T&x)
{
    char c;int sign = 1;x = 0;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
    x *= sign;
}


const int N = 2e5 + 20;
ull a,b,c,n,m;
ull u[N],v[N],ans;
ull s1[N],s2[N],s3[N];
vector<int> g[N],f[N];


int main()
{
    read(n); read(m);
    read(a); read(b); read(c);
    rep(i,1,m)
    {
        read(u[i]); read(v[i]);
        if(u[i] > v[i]) swap(u[i],v[i]);
        g[u[i]].push_back(v[i]);
        f[v[i]].push_back(u[i]);
    }


    rep(i,0,n-1)
    {
        ull x = n - i - 1;
        ans += a * i * (x * (x - 1) / 2);
        ans += b * i * i * x;
        ans += c * i * ((ull)i * (i - 1) / 2);
    }


    rep(i,1,m)
    {
        s1[  0 ] += 1;
        s1[u[i]] -= 1;


        s1[ u[i] ] += n - u[i] - 2;
        s1[u[i]+1] -= n - u[i] - 2;


        s2[u[i]+1] += 1;
        s2[ v[i] ] -= 1;


        s2[ u[i] ] += u[i];
        s2[u[i]+1] -= u[i];


        s2[ v[i] ] += n - v[i] - 1;
        s2[v[i]+1] -= n - v[i] - 1;


        s3[v[i]+1] += 1;
        s3[  n   ] -= 1;


        s3[ v[i] ] += v[i] - 1;
        s3[v[i]+1] -= v[i] - 1;
    }


    rep(i,1,n) 
        s1[i] += s1[i - 1],
        s2[i] += s2[i - 1],
        s3[i] += s3[i - 1];


    rep(i,0,n - 1)
    {
        ans -= a * i * s1[i];
        ans -= b * i * s2[i];
        ans -= c * i * s3[i];
    }


    rep(i,0,n-1) sort(g[i].begin(),g[i].end());
    rep(i,0,n-1) sort(f[i].begin(),f[i].end());
    rep(i,0,n-1)
    {
        int sz = g[i].size();
        rep(j,0,sz - 1)
        {
            int k = j + 1;
            while(k < sz)
            {
                ans += a * i;
                ans += b * g[i][j];
                ans += c * g[i][k];
                k ++ ;
            }


            int SZ = g[g[i][j]].size();
            rep(q,0,SZ - 1)
            {
                ans += a * i;
                ans += b * g[i][j];
                ans += c * g[g[i][j]][q];
            }
        }


        sz = f[i].size();
        rep(j,0,sz - 1)
        {
            int k = j + 1;
            while(k < sz)
            {
                ans += a * f[i][j];
                ans += b * f[i][k];
                ans += c * i;
                ++ k;
            }
        }
    }


    rep(i,0,n-1)
    {
        int sz = g[i].size();
        rep(j,0,sz - 1)
        {
            int t = j + 1,k = 0;
            int SZ = g[g[i][j]].size();
            while(t < sz && k < SZ)
            {
                if(g[i][t] == g[g[i][j]][k])
                {
                    ans -= a * i;
                    ans -= b * g[i][j];
                    ans -= c * g[i][t];
                    ++ t; ++ k;
                }
                else if(g[i][t] < g[g[i][j]][k]) ++ t;
                else ++ k;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/song_hai_lei/article/details/80964503