Submission #1625584
Source Code Expand
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,bg,ed) for(int i=bg;i<ed;i++)
#define REP(i,n) FOR(i,0,n)
typedef long long LL;
template <typename T>
istream& operator>>(istream& is,vector<T> &v){
for(auto &it:v)
is>>it;
return is;
}
template <typename T>
bool chmax(T &l,T r){
bool f=(l<r);
if(f)l=r;
return f;
}
typedef vector<int> V;
typedef vector<V> VV;
#define pb push_back
namespace BIT_{
LL t[512345];
inline LL* get(){
return t;
}
}
struct BIT{
using T=LL;
T* bit;
int n;
BIT(T* tb,int n):bit(tb),n(n){
REP(i,n+1)bit[i]=0;
}
BIT(int n):BIT(BIT_::get(),n){}
void add(int i,T x){
while(i<=n){
bit[i]+=x;
//cout<<"add:"<<i<<" "<<bit[i]<<endl;
i+=i&-i;
}
}
LL sum(int i){
//cout<<"q:"<<i<<endl;;
LL s=0;
while(i>0){
s+=bit[i];
//cout<<i<<" "<<bit[i]<<endl;;
i-=i&-i;
}
return s;
}
LL sum(int lb,int ub){
if(ub<lb)return 0;
return sum(ub)-sum(lb-1);
}
};
int N;
LL C;
pair<int,LL> bw[112345];
#define fi first
#define se second
BIT bit(112345);
LL f(const int l,const int r){
LL ret=0;
if(r-l<=3){
FOR(i,l,r)
FOR(j,i+1,r){
if(bw[i].fi>bw[j].fi){
LL x=min(bw[i].se,bw[j].se);
LL y=max(bw[i].se,bw[j].se);
ret+=C*x+y;
}
}
}
else{
const int m=(l+r)/2;
ret+=f(l,m);
ret+=f(m,r);
//FOR(i,l,r)cout<<"("<<bw[i].fi<<","<<bw[i].se<<")"<<endl;
{
int rr=m;
FOR(ll,l,m){
while(rr<r&&bw[ll].fi>bw[rr].fi){
bit.add(bw[rr].second,bw[rr].second);
//cout<<"add:"<<bw[rr].second<<endl;
rr++;
}
ret+=bit.sum(bw[ll].se,100010);
ret+=C*bit.sum(bw[ll].se-1);
//cout<<"sum:"<<bw[ll].se<<" "<<bit.sum(bw[ll].se+1,100010)<<endl;
}
FOR(i,m,rr)bit.add(bw[i].second,-bw[i].second);
}
{
int ll=m-1;
for(int rr=r-1;rr>=m;rr--){
while(ll>=l&&bw[rr].fi<bw[ll].fi){
bit.add(bw[ll].second,bw[ll].second);
ll--;
}
ret+=bit.sum(bw[rr].se+1,100010);
ret+=C*bit.sum(bw[rr].se);
}
for(int i=m-1;i>ll;i--)bit.add(bw[i].second,-bw[i].second);
}
}
sort(bw+l,bw+r);
return ret;
}
int main(){
cin>>N>>C;
REP(i,N){
cin>>bw[i].first>>bw[i].second;
}
cout<<f(0,N)<<endl;
}
Submission Info
| Submission Time |
|
| Task |
I - Librarian's Work |
| User |
new_game |
| Language |
C++14 (GCC 5.4.1) |
| Score |
100 |
| Code Size |
2887 Byte |
| Status |
AC |
| Exec Time |
258 ms |
| Memory |
4096 KiB |
Judge Result
| Set Name |
All |
| Score / Max Score |
100 / 100 |
| Status |
|
| Set Name |
Test Cases |
| All |
00_sample_01, 00_sample_02, 00_sample_03, 10_small_0, 10_small_1, 10_small_2, 10_small_3, 10_small_4, 10_small_5, 10_small_6, 10_small_7, 20_large_0, 20_large_1, 20_large_2, 20_large_3, 20_large_4, 20_large_5, 20_large_6, 20_large_7, 30_maximum_0, 30_maximum_1, 30_maximum_2, 30_maximum_3, 50_sorted_0, 50_sorted_1, 50_sorted_2, 50_sorted_3, 60_reverse_sorted_0, 60_reverse_sorted_1, 60_reverse_sorted_2, 60_reverse_sorted_3 |
| Case Name |
Status |
Exec Time |
Memory |
| 00_sample_01 |
AC |
2 ms |
2560 KiB |
| 00_sample_02 |
AC |
2 ms |
2560 KiB |
| 00_sample_03 |
AC |
2 ms |
2560 KiB |
| 10_small_0 |
AC |
2 ms |
2560 KiB |
| 10_small_1 |
AC |
2 ms |
2560 KiB |
| 10_small_2 |
AC |
2 ms |
2560 KiB |
| 10_small_3 |
AC |
2 ms |
2560 KiB |
| 10_small_4 |
AC |
2 ms |
2560 KiB |
| 10_small_5 |
AC |
2 ms |
2560 KiB |
| 10_small_6 |
AC |
2 ms |
2560 KiB |
| 10_small_7 |
AC |
2 ms |
2560 KiB |
| 20_large_0 |
AC |
238 ms |
3968 KiB |
| 20_large_1 |
AC |
121 ms |
3328 KiB |
| 20_large_2 |
AC |
81 ms |
3072 KiB |
| 20_large_3 |
AC |
82 ms |
3072 KiB |
| 20_large_4 |
AC |
63 ms |
2944 KiB |
| 20_large_5 |
AC |
180 ms |
3712 KiB |
| 20_large_6 |
AC |
22 ms |
2688 KiB |
| 20_large_7 |
AC |
2 ms |
2560 KiB |
| 30_maximum_0 |
AC |
258 ms |
4096 KiB |
| 30_maximum_1 |
AC |
251 ms |
4096 KiB |
| 30_maximum_2 |
AC |
249 ms |
4096 KiB |
| 30_maximum_3 |
AC |
258 ms |
4096 KiB |
| 50_sorted_0 |
AC |
3 ms |
2560 KiB |
| 50_sorted_1 |
AC |
52 ms |
3200 KiB |
| 50_sorted_2 |
AC |
42 ms |
3072 KiB |
| 50_sorted_3 |
AC |
106 ms |
3840 KiB |
| 60_reverse_sorted_0 |
AC |
72 ms |
3200 KiB |
| 60_reverse_sorted_1 |
AC |
148 ms |
3712 KiB |
| 60_reverse_sorted_2 |
AC |
124 ms |
3584 KiB |
| 60_reverse_sorted_3 |
AC |
84 ms |
3200 KiB |