QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#134017 | #4936. Shopping Changes | Lazy | TL | 1852ms | 39932kb | C++14 | 3.3kb | 2023-08-02 20:32:45 | 2023-08-02 20:32:54 |
Judging History
answer
#include<bits/stdc++.h>
//#define ll int
#define inf 0x3f3f3f3f
#define endl '\n'
#define ull unsigned long long
#define pll pair<ll ,ll>
#define pii pair<int, int>
#define lazy ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
const ll N = 1e5 + 7;
const ll M = 2e3 + 7;
const double eps = 1e-6;
const ll mod = 998244353;
ll _ = 1, n, m, a[N], tot, num[N], cntl[2 * N], cntr[2 * N], suml[2 * N], sumr[2 * N];
vector<ll> v[N];
map<ll, ll> mp;
template<class T>
struct BIT {
ll len;
vector<T> c;
BIT(int x){
len=x;
c.resize(len + 1);
}
void init(ll x){
len = x;
c.clear();
c.resize(len + 1);
}// 重构数组
ll lowbit(ll x){
return x & (-x);
}
void modify(ll x, ll y){// 单点加
assert(x <= len);
while (x <= len){
c[x] += y;
x += lowbit(x);
}
}
T query(ll x){// 1-x前缀和
assert(x >= 0);
if (x == 0)return 0;
T res = 0;
while (x){
res += c[x];
x -= lowbit(x);
}
return res;
}
T query(ll l, ll r){return query(r) - query(l - 1);}
};
void doit(ll x){
if (mp.find(x) == mp.end())mp[x] = ++tot;
}
void solve() {
cin >> m >> n;
vector<ll> tempv;
for (int i = 1; i <= m; i++)cin >> a[i], tempv.push_back(a[i]);
for (int i = 1; i <= n; i++){
cin >> num[i];
v[i].push_back(0);
for (int j = 1, x; j <= num[i]; j++){
cin >> x;
tempv.push_back(x);
v[i].push_back(x);
}
}
sort(tempv.begin(), tempv.end());
for (auto i : tempv)doit(i);
for (int i = 1; i <= m; i++)a[i] = mp[a[i]];
for (int i = 1; i <= n; i++){
for (int j = 1; j <= num[i]; j++){
v[i][j] = mp[v[i][j]];
}
}
BIT<ll> b(tot), bl(tot), br(tot);
ll sum = 0;
for (int i = m; i; i--){
sum += b.query(a[i] - 1);
b.modify(a[i], 1);
bl.modify(a[i], 1);// 把数放在b数组的左边的逆序对个数
br.modify(tot + 1 - a[i], 1);// 把数放在b数组的右边的逆序对个数
}
for (int i = 1; i <= n; i++){
ll cnt = 0;
BIT<ll> bn(tot);
for (int j = num[i]; j; j--){
cnt += bn.query(v[i][j] - 1);
bn.modify(v[i][j], 1);
}
for (int j = 0; j <= num[i] + 1; j++)cntl[j] = cntr[j] = suml[j] = sumr[j] = 0;
for(int j = 1; j <= num[i]; j++){
cntl[j] = bl.query(v[i][j] - 1);
suml[j] = suml[j - 1] + cntl[j];
// cerr << j << " cntl " << cntl[j] << " suml " << suml[j] << endl;
}
for (int j = num[i]; j; j--){
cntr[j] = br.query(tot + 1 - v[i][j] - 1);
sumr[j] = sumr[j + 1] + cntr[j];
// cerr << j << " cntr " << cntr[j] << " sumr " << sumr[j] << endl;
}
ll ans = 1e18;
for (int j = 0; j <= num[i]; j++){
ll all = sum + cnt;
all += suml[j];
all += sumr[j + 1];
// cerr << j << " " << all << endl;
ans = min(ans, all);
}
cout << ans << endl;
}
}
int main() {
lazy;
// cin >> _;
while (_--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 13488kb
input:
3 3 5 6 7 6 2 3 4 8 9 10 2 100 99 3 5 6 7
output:
0 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 3ms
memory: 11868kb
input:
3 2 7 6 5 6 2 3 4 8 9 10 6 10 9 8 4 3 2
output:
3 27
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 11632kb
input:
1 1 589284012 1 767928734
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 510ms
memory: 33004kb
input:
10000 9999 298772847 712804102 869795012 527998188 804246450 598105371 843966934 639805471 937482040 887551242 254734680 188704975 17408126 626523673 553956319 697723205 231690822 637079761 232393146 377026277 962223856 338922458 912529500 710873344 942955137 51167037 195729799 529674367 990599310 4...
output:
24802338 24830913 24857654 24813132 24846150 24785558 24857315 24805175 24862714 24785339 24804999 24780907 24808009 24798987 24958122 24781372 24772291 24846071 24953540 24778276 24778689 24979527 24770012 24892097 24776909 24865295 24821506 24772586 24800432 24891424 24864488 24820312 24801522 248...
result:
ok 9999 lines
Test #5:
score: 0
Accepted
time: 1852ms
memory: 39932kb
input:
42320 25000 977178721 305456426 916831455 324594376 259922325 798438534 906876242 353428436 459214642 504133134 734517252 944888626 929971853 735313273 285979369 866298401 385768124 918185862 811827492 3054135 190006456 852394509 784943097 903969029 4089198 931644108 916374905 942243264 383987411 45...
output:
448869835 448857081 449001851 448984953 448766022 449088370 448752999 448813227 448791232 448952235 448991581 448762188 449564834 448895404 448773616 448827147 448822397 448871140 448785180 448915022 448786815 448758118 448930420 449164516 448765912 448826222 448791834 448895668 448789204 448791197 ...
result:
ok 25000 lines
Test #6:
score: -100
Time Limit Exceeded
input:
50000 50000 478377125 98598664 834663974 414981508 481428682 921148788 327891419 311824685 274571883 326908479 516913707 230013528 47214051 844074075 870004539 261165376 338829696 476144552 905505033 857420851 4709519 198366271 90438077 24208873 865090108 250612383 910141082 205796257 712687049 6996...
output:
626276046 626281383 626305127 626284808 626194742 626223647 626345428 626232541 626201578 626246219 626205408 626202408 626200781 626281457 626203632 626200319 626196783 626209587 626202530 626221984 626236632 626240922 626221575 626193667 626196147 626357475 626201484 626209637 626191037 626237251 ...