题意
分析
思路比较好想,维护两个优先队列,一个是可以两倍处理数,一个是还未被选中但最终一定要被选中的数,同时维护一下两个和即可
具体看代码
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<ll> VI;
const int INF = 0x3f3f3f3f;
const int N = 600;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
VI p;
priority_queue<int, vector<int>, greater<int> > q;
int l[N],r[N];
ll a[N][N];
int n,m,k;
bool cmp(int x,int y){
return x > y;
}
int main() {
int T;
read(T);
while(T--){
read(n),read(m),read(k);
ll s1 = 0,s2 = 0,ans = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++)
read(a[i][j]);
sort(a[i] + 1,a[i] + 1 + m,cmp);
}
int L = 0,R = 0;
for(int i = 1;i <= n;i++){
read(l[i]),read(r[i]);
L += l[i],R += r[i];
for(int j = 1;j <= l[i];j++){
s2 += a[i][j];
if(q.size() < k) q.push(a[i][j]),s1 += a[i][j];
else {
if(q.top() < a[i][j]){
s1 -= q.top();
q.pop();
q.push(a[i][j]),s1 += a[i][j];
}
}
}
for(int j = l[i] + 1;j <= r[i];j++) p.pb(a[i][j]);
}
sort(all(p),cmp);
ans = s1 + s2;
for(int i = L + 1;i <= R;i++){
ll t = p[i - L - 1];
s2 += t;
if(q.size() < k) q.push(t),s1 += t;
else {
if(q.top() < t){
s1 -= q.top();
q.pop();
q.push(t),s1 += t;
}
}
ans = ans ^ (s1 + s2);
}
dl(ans);
while(q.size()) q.pop();
p.clear();
}
return 0;
}