QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#655092#7878. Matrix DistancesiamarmanAC ✓1218ms120012kbC++239.5kb2024-10-19 00:28:172024-10-19 00:28:17

Judging History

你现在查看的是最新测评结果

  • [2024-10-19 00:28:17]
  • 评测
  • 测评结果:AC
  • 用时:1218ms
  • 内存:120012kb
  • [2024-10-19 00:28:17]
  • 提交

answer

                                                  //   Bismillahir Rahmanir Rahim      //
                                                 //     After hardship comes ease     //
                                                //         AUTHOR : iamarman         //

// pragmas
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC optimization ("strict-overflow")
 
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

                                                    ////       TEMPLATE       ////

//---------------------------------------------------------------------------------------------------------------------------------|


# define    el '\n'
# define    sp " "
# define    ff first
# define    ss second
# define    ll long long
# define    pb push_back
# define    mp make_pair
# define    yess1 cout<<1<<el 
# define    noo cout<<"NO"<<el
# define    yess cout<<"YES"<<el
# define    siz(x) (int)x.size()
# define    ull unsigned long long    
# define    all(v) v.begin(),v.end()
# define    allr(v) v.rbegin(),v.rend()
# define    torad(x) ((x) * ((2*acos(0))/180.0))
# define    todeg(x) ((x) * (180.0/(2*acos(0))))

constexpr ll modd=998244353;
constexpr ll INF=LLONG_MAX;
constexpr double PI= acos(-1);
constexpr double eps=1e-9;

# define mem(a,b) memset(a,b,sizeof(a))
# define sqr(a) ((a)*(a))
# define lcm(a,b) (a*b)/__gcd(a,b)

# define optimise   { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
# define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a); cout.setf(ios::fixed,ios::floatfield);
# define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

// find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
// order_of_key()  - The number of items in a set that are strictly smaller than our item 
// greater instead of less for descending order
// less_equal works as ordered multiset
// we can use pair<int,int> instead of int for pair of orderd set
// for multiset st.lower_bound(x) works as upper bound and st.upper_bound(x) works as lower bound

inline void file() {
#ifndef ONLINE_JUDGE  
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
//----------------------------------------------------------------------------------------------------------------------------------|


                                                               // DEBUGGER //


//----------------------------------------------------------------------------------------------------------------------------------|

template < typename F, typename S > ostream& operator << ( ostream& os, const pair< F, S > & p ) { return os << "(" << p.first << ", " << p.second << ")"; }
template < typename T > ostream &operator << ( ostream & os, const vector< T > &v ) { os << "{"; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", ";  os << *it; }  return os << "}";  }
template < typename T >  ostream &operator << ( ostream & os, const set< T > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << *it;  } return os << "]"; }
template < typename T > ostream &operator << ( ostream & os, const multiset< T > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << *it; } return os << "]"; }
template < typename F, typename S > ostream &operator << ( ostream & os, const map< F, S > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << it -> first << " = " << it -> second ; } return os << "]";  }
#define dbg(args...) do {cerr << #args << " : "; iamarman(args); } while(0)
void iamarman () { cerr << endl; }
template <typename T> void iamarman( T a[], int n ) {   for(int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl;  }
template <typename T, typename ... hello>  void iamarman( T arg, const hello &... rest) {   cerr << arg << ' ';  iamarman(rest...);  }

//--------------------------------------------------------------------------------------------------------------------------------------|



                                                           /////    FUNCTIONS     /////



// ll bigmod(ll base,ll power){ ll res=1; ll p=base%mod; while(power>0) { if(power%2==1) {  res=((res%mod)*(p%mod))%mod; }  power/=2; p=((p%mod)*(p%mod))%mod; } return res; }

// ll inversemod(ll base) { return bigmod(base,mod-2); }

// ll poww(ull a,ull b) { ull ans=1; if(!b) return 1; while(b>1) {  if(b&1) { ans=ans*a%mod; } a=a*a%mod; b/=2; }return a*ans%mod; }

// ll gcd(ll a,ll b) { ll rem; while(b%a!=0)  {  rem=b%a;  b=a;  a=rem; } return a; }

// ll sqrtt(ll a){ long long x = sqrt(a) + 2; while (x * x > a) x--; return x;}

// ll sqrt(ll n) {ll low=0,high=1e10; while(high-low>1){ ll mid=low+(high-low)/2; if(mid*mid<=n) low=mid; else high=mid; }return low; }

// long double sqrtd(long double n){ long double low=0,high=n,mid; for(int i=0;i<100;i++) { mid=(low+high)/2; if(mid*mid<=n) low=mid; else high=mid;} return low;}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

inline ll getrandom(ll a,ll b) { return uniform_int_distribution<ll>(a,b)(rng); }

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

// up = { -1,0 } , down = { 1,0 } , right = { 0,1 } , left = { 0,-1 }
// up-right = { -1,1 } , up-left = { -1,-1 } , down-right = { 1,1 } , down-left = { 1,-1 }




                                                   ///  ____________CODE STARTS FROM HERE____________    ///

constexpr ll mod=1000012253;
template<typename T>
class Hashing {
private:
    int n;
    string s;
    T base;
    vector<T> prefix_hash;
    vector<T> power;
    vector<T> inv;

    vector<int> bases = {37,61,79,83,97,53,61,
                         107,127,137,163,191 ,
                         199,211,229,239,263 ,
                         271,281,293};

    T rng() {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<T> dis(0, std::numeric_limits<T>::max());
        return dis(gen);
    }

    T mul(T a, T b, T mod) {
        return ((1LL * a % mod) * (b % mod)) % mod;
    }

    T add(T a, T b, T mod) {
        return (1LL * a + b) % mod;
    }

    T sub(T a, T b, T mod) {
        return ((a % mod) - (b % mod) + 2LL * mod) % mod;
    }

    T bigmod(T base, T power, T mod) {
        T res = 1;
        while (power > 0) {
            if (power & 1) {
                res = mul(res, base, mod);
            }
            base = mul(base, base, mod);
            power >>= 1;
        }
        return res;
    }

public:
    Hashing(const std::string& str) : s(str) {
        n = s.size();
        base = 79;
        prefix_hash.resize(n + 1, 0);
        power.resize(n + 1, 0);
        inv.resize(n + 1, 0);
        precom();
    }

    void precom() {
        power[0] = 1;
        for (int i = 1; i <= n; i++) {
            power[i] = (1LL * power[i - 1] * base) % mod;
        }

        T inv_base = bigmod(base, mod - 2, mod);
        inv[0] = 1;
        for (int i = 1; i <= n; i++) {
            inv[i] = mul(inv[i - 1], inv_base, mod);
        }

        prefix_hash[0] = 0;
        for (int i = 1; i <= n; i++) {
            int ch = s[i - 1] - 'a' + 1;
            prefix_hash[i] = (1LL * prefix_hash[i - 1] + mul(ch, power[i - 1], mod)) % mod;
        }
    }

    T get_hash(int l, int r) {
        T val = sub(prefix_hash[r], prefix_hash[l - 1], mod);
        val = mul(val, inv[l], mod);
        return val;
    }
    T combine(T hash1, T hash2, int len1) {
        return add(mul(hash1, power[len1], mod), hash2, mod);
    }
};


void solve()
{  
    int n,m;
    cin>>n>>m;

    vector<int> x[n*m+1],y[n*m+1];
    map<int,int> mp;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int col;
            cin>>col;
            if(mp.find(col)==mp.end())
            {
                cnt++;
                mp[col]=cnt;
            }
            x[mp[col]].pb(i);
            y[mp[col]].pb(j);
            
        }
    }

    ll ans=0;

    for(int i=1;i<=n*m;i++)
    {
        if(siz(x[i])!=0)
        {
            sort(all(x[i]));
            ll sum=accumulate(all(x[i]),0);
            ll sum1=0;
            for(int j=0;j<siz(x[i]);j++)
            {
                ll cur=abs(sum1-x[i][j]*j);
                sum1+=x[i][j];
                sum-=x[i][j];
                cur+=abs(sum-x[i][j]*(siz(x[i])-j-1));
                ans+=cur;
            }
        }
        if(siz(y[i])!=0)
        {
            sort(all(y[i]));
            ll sum=accumulate(all(y[i]),0);
            ll sum1=0;
            for(int j=0;j<siz(y[i]);j++)
            {
                ll cur=abs(sum1-y[i][j]*j);
                sum1+=y[i][j];
                sum-=y[i][j];
                cur+=abs(sum-y[i][j]*(siz(y[i])-j-1));
                ans+=cur;
            }
        }
    }

    cout<<ans<<el;


}


int main()
{ 
    optimise;
    file();

    clock_t start= clock();
    int t=1;
  //  cin>>t;
    for(int i=1;i<=t;i++)
    {
       // cout<<"Case "<<i<<": ";
        solve();
       
    }
    cerr << "Run Time : " <<((double)(clock() - start) / CLOCKS_PER_SEC)<<el;
  
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3960kb

input:

2 2
1 1
2 2

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3996kb

input:

4 4
1 3 2 4
2 1 2 3
1 3 3 2
3 2 1 4

output:

152

result:

ok 1 number(s): "152"

Test #3:

score: 0
Accepted
time: 116ms
memory: 59828kb

input:

1000 1000
227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 329001637 227980299 227980299 227980299 329001637 227980299 227980299 227980299 227980299 227980299 ...

output:

506784086339644

result:

ok 1 number(s): "506784086339644"

Test #4:

score: 0
Accepted
time: 126ms
memory: 58336kb

input:

1000 1000
701031019 701031019 902550481 515963342 902550481 701031019 902550481 701031019 701031019 701031019 701031019 701031019 701031019 902550481 701031019 701031019 701031019 902550481 701031019 902550481 701031019 902550481 902550481 902550481 902550481 515963342 701031019 701031019 701031019 ...

output:

293351133301656

result:

ok 1 number(s): "293351133301656"

Test #5:

score: 0
Accepted
time: 115ms
memory: 58252kb

input:

1000 1000
584147147 584147147 771066621 584147147 814776600 814776600 584147147 814776600 814776600 814776600 771066621 814776600 814776600 771066621 584147147 814776600 814776600 771066621 814776600 584147147 771066621 584147147 814776600 771066621 814776600 584147147 814776600 584147147 771066621 ...

output:

222221591276684

result:

ok 1 number(s): "222221591276684"

Test #6:

score: 0
Accepted
time: 124ms
memory: 59488kb

input:

1000 1000
451748258 451748258 205399494 451748258 451748258 451748258 451748258 451748258 953934338 451748258 580264463 451748258 451748258 451748258 451748258 588594555 451748258 451748258 451748258 451748258 451748258 451748258 451748258 451748258 451748258 451748258 953934338 451748258 451748258 ...

output:

450794820267988

result:

ok 1 number(s): "450794820267988"

Test #7:

score: 0
Accepted
time: 128ms
memory: 58624kb

input:

1000 1000
548583482 635446288 548583482 635446288 635446288 548583482 548583482 635446288 801198618 548583482 635446288 635446288 548583482 635446288 548583482 548583482 548583482 548583482 635446288 635446288 635446288 635446288 635446288 635446288 548583482 726064808 635446288 801198618 548583482 ...

output:

237272499061426

result:

ok 1 number(s): "237272499061426"

Test #8:

score: 0
Accepted
time: 148ms
memory: 58232kb

input:

1000 1000
596800174 215475167 727165477 215475167 596800174 479596632 596800174 479596632 340778824 596800174 340778824 340778824 596800174 727165477 792552295 215475167 825136342 215475167 15133890 15133890 792552295 215475167 15133890 15133890 215475167 792552295 186221070 596800174 825136342 7925...

output:

66666979915604

result:

ok 1 number(s): "66666979915604"

Test #9:

score: 0
Accepted
time: 132ms
memory: 60076kb

input:

1000 1000
283321818 144879541 144879541 144879541 144879541 144879541 144879541 144879541 351529453 755622562 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 144879541 ...

output:

429192094084416

result:

ok 1 number(s): "429192094084416"

Test #10:

score: 0
Accepted
time: 151ms
memory: 58260kb

input:

1000 1000
161743512 416078170 855756681 889314339 416481165 889314339 855756681 889314339 855756681 889314339 855756681 264524276 855756681 855756681 855756681 889314339 855756681 889314339 889314339 889314339 494796476 889314339 889314339 889314339 889314339 855756681 855756681 855756681 855756681 ...

output:

216113198146626

result:

ok 1 number(s): "216113198146626"

Test #11:

score: 0
Accepted
time: 178ms
memory: 62012kb

input:

1000 1000
111711290 83219409 652958381 160096781 438510184 979014841 96096837 793714095 266288718 28912730 675001370 464693704 642313963 565598413 429830978 233570610 775904524 969001190 158819374 451251731 210397449 83219409 766749559 473545004 517036030 851023554 166176740 96096837 160096781 63151...

output:

6666591233498

result:

ok 1 number(s): "6666591233498"

Test #12:

score: 0
Accepted
time: 162ms
memory: 61776kb

input:

1000 1000
206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 484082914 206885994 737061221 206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 206885994 885540330 206885994 15312403 206885994 206885994 206885994 2...

output:

426348228198774

result:

ok 1 number(s): "426348228198774"

Test #13:

score: 0
Accepted
time: 152ms
memory: 58844kb

input:

1000 1000
119021511 262340498 119021511 571262579 262340498 119021511 119021511 262340498 262340498 262340498 262340498 262340498 119021511 119021511 119021511 262340498 262340498 262340498 119021511 119021511 16968046 262340498 262340498 119021511 616963806 119021511 119021511 119021511 616963806 2...

output:

213349390292676

result:

ok 1 number(s): "213349390292676"

Test #14:

score: 0
Accepted
time: 251ms
memory: 61280kb

input:

1000 1000
294834416 710159087 71576000 771158418 734800928 519231261 66958341 846593614 842762889 266263223 449970247 393627406 452797162 702159979 103716509 528121669 411977733 41875663 181065802 366920699 973794257 627741675 408432509 319085147 24347426 209592388 529269033 798345726 138118120 1776...

output:

666600092404

result:

ok 1 number(s): "666600092404"

Test #15:

score: 0
Accepted
time: 227ms
memory: 60072kb

input:

1000 1000
774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 991718494 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 774400533 ...

output:

427191601662978

result:

ok 1 number(s): "427191601662978"

Test #16:

score: 0
Accepted
time: 206ms
memory: 59996kb

input:

1000 1000
786051867 749266154 786051867 336663633 786051867 786051867 336663633 786051867 786051867 786051867 336663633 786051867 786051867 800962293 786051867 786051867 786051867 223735788 786051867 336663633 336663633 786051867 148679559 336663633 786051867 336663633 336663633 786051867 73949921 6...

output:

213692206718908

result:

ok 1 number(s): "213692206718908"

Test #17:

score: 0
Accepted
time: 412ms
memory: 63352kb

input:

1000 1000
589040181 665125243 509194312 722650288 704530640 677647760 941069621 844686966 269308659 817746365 703209818 600393306 651385133 624370978 615782335 583567534 607685482 258818547 960735386 920234049 589990452 396294522 54789277 611895702 401553455 423080336 45108579 47104502 265933176 774...

output:

66671759372

result:

ok 1 number(s): "66671759372"

Test #18:

score: 0
Accepted
time: 266ms
memory: 66036kb

input:

1000 1000
340219853 340219853 340219853 439933175 340219853 340219853 340219853 321321381 68352466 340219853 340219853 340219853 340219853 340219853 3205688 340219853 262286151 340219853 340219853 921123028 340219853 340219853 340219853 831324481 740878043 340219853 340219853 340219853 809209277 340...

output:

427237509950002

result:

ok 1 number(s): "427237509950002"

Test #19:

score: 0
Accepted
time: 266ms
memory: 66252kb

input:

1000 1000
312807013 312807013 671631100 312807013 312807013 312807013 671631100 671631100 671631100 671631100 671631100 707041060 312807013 671631100 312807013 671631100 671631100 671631100 312807013 312807013 671631100 671631100 671631100 671631100 312807013 315186901 312807013 312807013 312807013 ...

output:

213105139802108

result:

ok 1 number(s): "213105139802108"

Test #20:

score: 0
Accepted
time: 733ms
memory: 73248kb

input:

1000 1000
968334296 124281306 602304677 823629809 4594569 888524150 464787481 66467938 740368986 98259485 609892270 10390448 306213053 649927667 507023218 743709660 259153419 9610640 278544598 763827885 452113052 834395597 591175799 165403867 53538577 69976033 120747719 800800356 966618685 95535293 ...

output:

6665934902

result:

ok 1 number(s): "6665934902"

Test #21:

score: 0
Accepted
time: 299ms
memory: 76868kb

input:

1000 1000
917320864 917320864 917320864 917320864 917320864 917320864 917320864 917320864 279445549 917320864 917320864 917320864 917320864 917320864 917320864 917320864 917320864 917320864 917320864 168955665 917320864 917320864 67798067 451366625 917320864 917320864 917320864 917320864 917320864 9...

output:

426406609284792

result:

ok 1 number(s): "426406609284792"

Test #22:

score: 0
Accepted
time: 292ms
memory: 76224kb

input:

1000 1000
563910894 248691777 502346207 502346207 563910894 502346207 563910894 124707688 563910894 502346207 563910894 674899337 502346207 563910894 322714828 563910894 880755660 563910894 502346207 563910894 502346207 502346207 563910894 623520096 821743885 563910894 563910894 563910894 563910894 ...

output:

213337139288624

result:

ok 1 number(s): "213337139288624"

Test #23:

score: 0
Accepted
time: 1218ms
memory: 120012kb

input:

1000 1000
73364669 160660181 892922167 705497300 862628574 77538387 281208292 899898205 132405130 749782378 940763807 919799196 943173556 657398506 921058988 192689732 309195568 999117035 726766947 8645595 568429846 372621995 976592332 626494982 740333029 234304386 641643560 572521033 682509489 1057...

output:

665074264

result:

ok 1 number(s): "665074264"

Test #24:

score: 0
Accepted
time: 0ms
memory: 3892kb

input:

1 1
1

output:

0

result:

ok 1 number(s): "0"

Extra Test:

score: 0
Extra Test Passed