# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
61395 | zetapi | Ideal city (IOI12_city) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define itr ::iterator
typedef pair<int,int> pii;
const ll MAX=1e6;
const ll mod=1e9;
vector<pii> vec;
ll pre[MAX];
ll cal(vector<ll> arr)
{
ll sum=0,res=0;
for(int A=1;A<arr.size();A++)
pre[A]=(pre[A-1]+arr[A])%mod;
for(int A=1;A<arr.size();A++)
{
res+=(sum*arr[A])%mod;
res%=mod;
sum+=pre[A];
sum%=mod;
}
return res;
}
int DistanceSum(int N, int *X, int *Y)
{
vector<ll> arr;
ll res=0,cur,lol;
for(int A=0;A<N;A++)
vec.pb(mp(X[A],Y[A]));
sort(vec.begin(),vec.end());
cur=0;
arr.pb(0);
while(cur<vec.size())
{
lol=vec[cur].second;
while(cur+1<vec.size() and vec[cur+1].first==vec[cur].first)
++cur;
arr.pb(vec[cur].second-lol+1);
++cur;
}
//for(auto A:arr)
// cout<<A<<" ha ha\n";
res=cal(arr);
for(int A=0;A<N;A++)
swap(vec[A].first,vec[A].second);
sort(vec.begin(),vec.end());
arr.clear();
arr.pb(0);
cur=0;
while(cur<vec.size())
{
lol=vec[cur].second;
while(cur+1<vec.size() and vec[cur+1].first==vec[cur].first)
++cur;
arr.pb(vec[cur].second-lol+1);
++cur;
}
//for(auto A:arr)
// cout<<A<<" o ";
res+=cal(arr);
res%=mod;
return res;
}
/*signed main()
{
ios_base::sync_with_stdio(false);
int X[]={1,1,2,2};
int Y[]={1,2,1,2};
cout<<DistanceSum(4,X,Y);
return 0;
}*/#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define itr ::iterator
typedef pair<int,int> pii;
const ll MAX=1e6;
const ll mod=1e9;
vector<pii> vec;
ll pre[MAX];
ll cal(vector<ll> arr)
{
ll sum=0,res=0;
for(int A=1;A<arr.size();A++)
pre[A]=(pre[A-1]+arr[A])%mod;
for(int A=1;A<arr.size();A++)
{
res+=(sum*arr[A])%mod;
res%=mod;
sum+=pre[A];
sum%=mod;
}
return res;
}
int DistanceSum(int N, int *X, int *Y)
{
vector<ll> arr;
ll res=0,cur,lol;
for(int A=0;A<N;A++)
vec.pb(mp(X[A],Y[A]));
sort(vec.begin(),vec.end());
cur=0;
arr.pb(0);
while(cur<vec.size())
{
lol=vec[cur].second;
while(cur+1<vec.size() and vec[cur+1].first==vec[cur].first)
++cur;
arr.pb(vec[cur].second-lol+1);
++cur;
}
//for(auto A:arr)
// cout<<A<<" ha ha\n";
res=cal(arr);
for(int A=0;A<N;A++)
swap(vec[A].first,vec[A].second);
sort(vec.begin(),vec.end());
arr.clear();
arr.pb(0);
cur=0;
while(cur<vec.size())
{
lol=vec[cur].second;
while(cur+1<vec.size() and vec[cur+1].first==vec[cur].first)
++cur;
arr.pb(vec[cur].second-lol+1);
++cur;
}
//for(auto A:arr)
// cout<<A<<" o ";
res+=cal(arr);
res%=mod;
return res;
}
/*signed main()
{
ios_base::sync_with_stdio(false);
int X[]={1,1,2,2};
int Y[]={1,2,1,2};
cout<<DistanceSum(4,X,Y);
return 0;
}*/