結果
| 問題 |
No.924 紲星
|
| コンテスト | |
| ユーザー |
LayCurse
|
| 提出日時 | 2019-11-08 22:52:52 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 6,568 bytes |
| コンパイル時間 | 2,777 ms |
| コンパイル使用メモリ | 225,224 KB |
| 最終ジャッジ日時 | 2025-01-08 02:42:46 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 TLE * 10 |
ソースコード
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){
int i;
pair<T1, pair<T2, T3> > *arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first = a[i];
arr[i].second.first = b[i];
arr[i].second.second = c[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first;
b[i] = arr[i].second.first;
c[i] = arr[i].second.second;
}
}
template<class T1, class T2, class T3, class T4> void sortA_L(int N, T1 a[], T2 b[], T3 c[], T4 d[], void *mem = wmem){
int i;
pair<pair<T1, T2>, pair<T3, T4> > *arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first.first = a[i];
arr[i].first.second = b[i];
arr[i].second.first = c[i];
arr[i].second.second = d[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first.first;
b[i] = arr[i].first.second;
c[i] = arr[i].second.first;
d[i] = arr[i].second.second;
}
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void wt_L(char a){
putchar_unlocked(a);
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
putchar_unlocked('-');
}
while(s--){
putchar_unlocked(f[s]+'0');
}
}
template<class T> inline T popFirst(multiset<T> &a){
T res = *(a.begin());
a.erase(a.begin());
return res;
}
template<class T> inline T getFirst(multiset<T> &a){
return *(a.begin());
}
template<class T> inline T popLast(multiset<T> &a){
T res;
typename multiset<T>::iterator it;
it = a.end();
it--;
res = *it;
a.erase(it);
return res;
}
template<class T> inline T getLast(multiset<T> &a){
typename multiset<T>::iterator it;
it = a.end();
it--;
return *it;
}
template<class T> inline T popFirst(set<T> &a){
T res = *(a.begin());
a.erase(a.begin());
return res;
}
template<class T> inline T getFirst(set<T> &a){
return *(a.begin());
}
template<class T> inline T popLast(set<T> &a){
T res;
typename set<T>::iterator it;
it = a.end();
it--;
res = *it;
a.erase(it);
return res;
}
template<class T> inline T getLast(set<T> &a){
typename set<T>::iterator it;
it = a.end();
it--;
return *it;
}
int N;
int Q;
int A[200000];
int L[200000];
int R[200000];
int lbox[200000];
int ind[200000];
long long res[200000];
multiset<int> h1;
multiset<int> h2;
long long s1;
long long s2;
inline long long calc(void){
long long m;
if(h1.size() > h2.size()){
m = getLast(h1);
}
else{
m = getFirst(h2);
}
return (long long) h1.size() * m - s1 + s2 - (long long) h2.size() * m;
}
void addval(int a){
int x;
int y;
if(h1.size() > h2.size()){
h2.insert(a);
s2 += a;
}
else{
h1.insert(a);
s1 += a;
}
if(h1.size() && h2.size() && getLast(h1) > getFirst(h2)){
x = popLast(h1);
y = popFirst(h2);
h1.insert(y);
h2.insert(x);
s1 += y - x;
s2 += x - y;
}
}
void delval(int a){
int x;
multiset<int>::iterator it;
it = h1.find(a);
if(it != h1.end()){
h1.erase(it);
s1 -= a;
}
else{
it = h2.find(a);
h2.erase(it);
s2 -= a;
}
if(h2.size() > h1.size()+1){
x = popFirst(h2);
h1.insert(x);
s1 += x;
s2 -= x;
}
if(h1.size() > h2.size()+1){
x = popLast(h1);
h2.insert(x);
s1 -= x;
s2 += x;
}
}
int main(){
int i;
wmem = memarr;
int x;
int y;
long long tmp;
rd(N);
rd(Q);
{
int Lj4PdHRW;
for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
rd(A[Lj4PdHRW]);
}
}
{
int e98WHCEY;
for(e98WHCEY=(0);e98WHCEY<(Q);e98WHCEY++){
rd(L[e98WHCEY]);L[e98WHCEY] += (-1);
rd(R[e98WHCEY]);
}
}
for(i=(0);i<(Q);i++){
lbox[i] = L[i] / 500;
}
for(i=(0);i<(Q);i++){
ind[i] = i;
}
sortA_L(Q, lbox, R, L, ind);
x = y = 0;
for(i=(0);i<(Q);i++){
while(y < R[i]){
addval(A[y++]);
}
while(L[i] < x){
addval(A[--x]);
}
while(y > R[i]){
delval(A[--y]);
}
while(L[i] > x){
delval(A[x++]);
}
res[ind[i]] = calc();
}
{
int ZIeRIny5;
for(ZIeRIny5=(0);ZIeRIny5<(Q);ZIeRIny5++){
wt_L(res[ZIeRIny5]);
wt_L('\n');
}
}
return 0;
}
// cLay varsion 20191108-1
// --- original code ---
// int N, Q, A[2d5];
// int L[2d5], R[2d5];
// int lbox[2d5], ind[2d5];
// ll res[2d5];
//
// multiset<int> h1, h2;
// ll s1, s2;
//
// inline ll calc(void){
// ll m;
// if(h1.size() > h2.size()) m = getLast(h1); else m = getFirst(h2);
// return (ll) h1.size() * m - s1 + s2 - (ll) h2.size() * m;
// }
//
// void addval(int a){
// int x, y;
// if(h1.size() > h2.size()){
// h2.insert(a);
// s2 += a;
// } else {
// h1.insert(a);
// s1 += a;
// }
// if(h1.size() && h2.size() && getLast(h1) > getFirst(h2)){
// x = popLast(h1);
// y = popFirst(h2);
// h1.insert(y);
// h2.insert(x);
// s1 += y - x;
// s2 += x - y;
// }
// }
//
// void delval(int a){
// int x;
// multiset<int>::iterator it;
//
// it = h1.find(a);
// if(it != h1.end()){
// h1.erase(it);
// s1 -= a;
// } else {
// it = h2.find(a);
// h2.erase(it);
// s2 -= a;
// }
// if(h2.size() > h1.size()+1){
// x = popFirst(h2);
// h1.insert(x);
// s1 += x;
// s2 -= x;
// }
// if(h1.size() > h2.size()+1){
// x = popLast(h1);
// h2.insert(x);
// s1 -= x;
// s2 += x;
// }
// }
//
// {
// int x, y;
// ll tmp;
// rd(N,Q,A(N),(L--,R)(Q));
//
// rep(i,Q) lbox[i] = L[i] / 500;
// rep(i,Q) ind[i] = i;
// sortA(Q, lbox, R, L, ind);
//
// x = y = 0;
// rep(i,Q){
// while(y < R[i]) addval(A[y++]);
// while(L[i] < x) addval(A[--x]);
// while(y > R[i]) delval(A[--y]);
// while(L[i] > x) delval(A[x++]);
// res[ind[i]] = calc();
// }
//
// wtLn(res(Q));
//
// }
LayCurse