ICPC昆明 M.Stone Games(主席树)

题目描述

There are nn piles of stones, the ii-th of which contains si stones. The tiles are numbered from 1 to n. Rika and Satoko are playing a game on it.
During each round of the game, Rika chooses some piles from all n piles. Let denote the set of all chosen piles as S. Satoko then writes a non-negative integer x. If Rika takes some piles from S with the number of stones among all the taken piles equal to x, she wins the game, while otherwise (Rika cannot find such piles) Satoko wins the game. Note that each tile can be taken at most once during a round, and it is possible that Rika does not pick up any tile (when x=0).
There are Q rounds of game in total, and for the i-th round of game Rika will let SS be the set of all piles with index between li and ri. For each round, Satoko wonders the minimum integer x she can write to win the game. As you are a master of programming, it is your turn to solve the problem!

输入描述

The first line of input contains two integers n and Q, where n(1≤n≤106) is the number of tiles and Q(1≤Q≤105) is the number of the rounds of the games.
The second line contains nn integers s1,…,sn, the i-th of which, si (1≤si≤109), indicates the number of stones in the tile with index i.
Satoko wants you to compute the answers immediately after Rika chooses the interval, so she uses the following method to encrypt the input. The i-th of the next Q lines contains two integers li′,ri′ (1≤li′,ri′≤n). The chosen index range for the ii-th round, li,ri, can be computed by the following formula:
在这里插入图片描述
where ans[i] denotes the answer for the i-th round of the game (and thus ans[i-1] is the answer for the previous round). You can assume that ans[0]=0. It is clear that under the given constraints, 1≤li≤ri≤n holds.

输出描述

Output Q lines, the i-th of which contains a single integer ansi , denoting the minimum integer x Satoko can choose to win the i-th round of the game.

输入
5 5
1 4 2 1 6
1 3
2 1
2 4
1 4
3 4
输出
8
15
4
9
4

说明

In the example above, the actual query intervals are [2,4], [1,5], [3,5], [1,4] and [3,4].

题目大意

给定长度为n的序列,q次询问,每次询问给出L,R,问[L,R]中选择一个子集求和,无法凑出的最小数是多少。
询问强制在线。

题目分析

假设当前能够组成 [ 1 , x ] [1,x] [1,x],且当前区间 [ l , r ] [l,r] [l,r] 内有 x + 1 x+1 x+1,那么就能够凑出 x + ( x + 1 ) ∗ n ( n 为 区 间 内 x + 1 的 个 数 ) x+(x+1)*n(n为区间内 x+1的个数) x+(x+1)nnx+1。反之,如果区间内没有x+1,那么不能够凑出的最小数就是 x + 1 x+1 x+1

这道题我们可以用主席树来做:
当要查询 [ l , r ] [l,r] [l,r] 的答案时,并且当前区间已经凑到x了,我们就可以查询 r o o t [ r ] root[r] root[r] 版本与 r o o t [ l − 1 ] root[l-1] root[l1] 版本在 [ 1 , x + 1 ] [1,x+1] [1,x+1] 区间内和的差值。

如果该差值还是为x,那么说明该区间凑不出 x + 1 x+1 x+1 ,那么答案即为x+1。

如果该差值为 y = x + ( x + 1 ) ∗ n y=x+(x+1)*n y=x+(x+1)n ,那么说明当前区间可以凑到y,因此我们可以继续查询 [ 1 , y + 1 ] [1,y+1] [1,y+1] 区间内的差值。

代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#define LL long long
#define PII pair<int,int>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=1e6+5,INF=1e9;
struct Node{
	int l,r;
	LL sum;			//记录区间和
}tr[N*31];
int root[N],idx;
int insert(int p,int l,int r,int x)			//主席树的插入操作
{
	int u=++idx;
	tr[u]=tr[p];
	tr[u].sum=x+tr[p].sum;
	if(l==r) return u;
	int mid=l+r>>1;
	if(x<=mid) tr[u].l=insert(tr[p].l,l,mid,x);
	else tr[u].r=insert(tr[p].r,mid+1,r,x);
	return u;
}
LL query(int p,int q,int l,int r,LL v)		//查询[1,v]区间内root[p]版本与root[q]版本的差值
{
	if(1<=l&&r<=v) return tr[p].sum-tr[q].sum;
	int mid=l+r>>1;
	LL ans=0;
	if(mid>=1) ans=query(tr[p].l,tr[q].l,l,mid,v);
	if(mid<v) ans+=query(tr[p].r,tr[q].r,mid+1,r,v);
	return ans;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)			//输入数据,建立主席树
	{
		int x;
		scanf("%d",&x);
		root[i]=insert(root[i-1],1,INF,x);
	}
	LL ans=0;
	while(m--)
	{
		int l,r;
		scanf("%d%d",&l,&r);
		l=(l+ans)%n+1,r=(r+ans)%n+1;
		if(l>r) swap(l,r);
		ans=0;
		while(true)
		{
			LL t=query(root[r],root[l-1],1,INF,ans+1);	//查看询问区间用[1,ans+1]区间内的数可以凑出什么数来
			if(t==ans) break;		//如果与[1,ans]区间内凑出的数相等,说明无法凑出ans+1。
			ans=t;				//记录当前值,继续查询t+1
		}
		printf("%lld\n",++ans); 
	}
	return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值