Submission #13867

#TimeUsernameProblemLanguageResultExecution timeMemory
13867gs14004사회적 불평등 (TOKI14_social)C++14
100 / 100
69 ms2176 KiB
#include <cstdio> #include <cstring> #include <utility> #include <algorithm> using namespace std; typedef pair<int,int> pi; const int MAX = 10000; typedef long long lint; int n; pi a[100005]; struct bit{ lint tree[10005]; int lim; void init(int n){ memset(tree,0,sizeof(tree)); lim = n + 1; } void add(int x, int v){ x++; while(x <= lim){ tree[x] += v; x += x & -x; } } lint S(int x){ x++; lint ret = 0; while(x){ ret += tree[x]; x -= x & -x; } return ret; } lint sum(int x, int y){ return S(y) - S(x-1); } }bit1, bit2, bit3, bit4; lint solve(){ lint ret = 0; bit1.init(MAX); bit2.init(MAX); bit3.init(MAX); bit4.init(MAX); for (int i=n-1; i>=0; i--) { ret += 1ll * bit1.sum(a[i].second+1,MAX) * a[i].first * a[i].second; ret += 1ll * bit2.sum(a[i].second+1,MAX); ret -= 1ll * a[i].first * bit3.sum(a[i].second+1,MAX); ret -= 1ll * a[i].second * bit4.sum(a[i].second+1,MAX); bit1.add(a[i].second,1); bit2.add(a[i].second,a[i].first * a[i].second); bit3.add(a[i].second,a[i].second); bit4.add(a[i].second,a[i].first); } return ret; } int main(){ scanf("%d",&n); for (int i=0; i<n; i++) { scanf("%d %d",&a[i].first,&a[i].second); } sort(a,a+n); lint ret = solve(); for (int i=0; i<n; i++) { a[i].second = MAX + 1 - a[i].second; } ret += solve(); printf("%lld",ret); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...