QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133960 | #4936. Shopping Changes | ShG | WA | 2ms | 9736kb | C++14 | 2.9kb | 2023-08-02 18:49:23 | 2023-08-02 18:49:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define rep(a, b, c) for(int (a)=(b);(a)<=(c);(a)++)
#define per(a, b, c) for(int (a)=(b);(a)>=(c);(a)--)
#define mset(var, val) memset(var,val,sizeof(var))
#define ll long long
#define int ll
#define fi first
#define se second
#define no "NO\n"
#define yes "YES\n"
#define pb push_back
#define endl "\n"
#define pii pair<int,int>
#define pll pair<ll,ll>
#define dbg(x...) do{cout<<#x<<" -> ";err(x);}while (0)
void err() { cout << '\n'; }
template<class T, class... Ts>
void err(T arg, Ts... args) {
cout << arg << ' ';
err(args...);
}
const int N = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int P = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
struct node{
int x, id;
}c[N], w[N];
int n, m, L, ans1=0, ans2, mn;
int p[N], l[N], r[N];
void add(int x, int k, int nn) {
for(int i = x; i <= nn; i+=i&-x) p[i] += k;
}
int get(int x) {
int re = 0;
for(int i = x; i > 0; i-=i&-i) re += p[i];
return re;
}
bool cmp1(node x, node y) {
return x.x < y.x;
}
// bool cmp2(node x, node y) {
// return x.id < y.id;
// }
int find1(int x) {
int l = 1, r = n, re = 0;
while(l <= r) {
int mid = (l+r)>>1;
if(c[mid].x > x) r = mid-1;
else l = mid+1, re = mid;
}
return re;
}
int find2(int x) {
int l = 1, r = n, re = n+1;
while(l <= r) {
int mid = (l+r)>>1;
if(c[mid].x < x) l = mid+1;
else r = mid-1, re = mid;
}
return re;
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> c[i].x;
c[i].id = i, p[i] = 0;
}
sort(c+1, c+1+n, cmp1);
for(int i = 1; i <= n; i++) {
ans1 += get(c[i].id);
add(1, 1, n);
add(c[i].id, -1, n);
}
// cout << ans1 << endl;
while(m--) {
cin >> L;
for(int i = 1; i <= L; i++) {
cin >> w[i].x;
w[i].id = i, p[i] = 0;
l[i] = find1(w[i].x);
r[i] = n-find2(w[i].x)+1;
// dbg(i,l[i], r[i]);
}
l[0] = l[L+1] = r[0] = r[L+1] = 0;
for(int i = 2; i <= L; i++) l[i] += l[i-1];
for(int i = L-1; i > 0; i--) r[i] += r[i+1];
ans2=0;
sort(w+1, w+1+L, cmp1);
for(int i = 1; i <= L; i++) {
ans2 += get(w[i].id);
add(1, 1, L);
add(w[i].id+1, -1, L);
}
int mn = l[0]+r[1];
for(int i = 1; i <= L; i++)
mn = min(mn, l[i]+r[i+1]);
// cout << ans2 << " " << mn << endl;
cout << ans1+ans2+mn << endl;
}
}
signed main() {
IOS;
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9736kb
input:
3 3 5 6 7 6 2 3 4 8 9 10 2 100 99 3 5 6 7
output:
0 1 4
result:
wrong answer 3rd lines differ - expected: '1', found: '4'