比赛 20241128 评测结果 AAATAAAATTTTTTTTTTTT
题目名称 魔法卡片 最终得分 35
用户昵称 ┭┮﹏┭┮ 运行时间 28.510 s
代码语言 C++ 内存使用 76.37 MiB
提交时间 2024-11-28 09:48:04
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,ll>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e6+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}


int n,m,q,B;
int c[N];
ll sum,mx,su;
vector<int>a[N],b[N];
vector<ll>s[N];

void dfs(int x,int r){
	if(x == r + 1)return mx = max(mx,su),void();
	for(int w : a[x]){
		if(!c[w])su += (ll)w * w;
		c[w]++;
	}
	dfs(x + 1,r);
	for(int w : a[x]){
		c[w]--;
		if(!c[w])su -= (ll)w * w;
	}
	for(int w : b[x]){
		if(!c[w])su += (ll)w * w;
		c[w]++;
	}
	dfs(x + 1,r);
	for(int w : b[x]){
		c[w]--;
		if(!c[w])su -= (ll)w * w;
	}
}

int main(){
	freopen("magic.in","r",stdin);
	freopen("magic.out","w",stdout);
	n = read(),m = read(),q = read();
	B = ceil(log2(m));
	for(int i = 1;i <= n;i++){
		int x = read();
		for(int j = 1;j <= x;j++){
			int w = read();
			a[i].pb(w);
			c[w]++;
		}
		for(int j = 1;j <= m;j++)
			if(!c[j])b[i].pb(j);
		for(int w : a[i])c[w]--;
	}
	//2^B = m
	for(int i = 1;i <= n;i++){
		s[i].pb(0);
		for(int j = i;j <= i + B - 1 && j <= n;j++){
			mx = su = 0;
			dfs(i,j);
			s[i].pb(mx);
		}
	}
	for(int i = 1;i <= m;i++)sum += (ll)i * i;
	for(int i = 1;i <= q;i++){
		int l = read(),r = read();
		if(r - l + 1 > B)printf("%lld\n",sum);
		else printf("%lld\n",s[l][r-l+1]);
	}

	return 0;

}