# HDU4973 A simple simulation problem.

20 篇文章 0 订阅
17 篇文章 0 订阅

## A simple simulation problem.

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 307 Accepted Submission(s): 126

Problem Description
There are n types of cells in the lab, numbered from 1 to n. These cells are put in a queue, the i-th cell belongs to type i. Each time I can use mitogen to double the cells in the interval [l, r]. For instance, the original queue is {1 2 3 3 4 5}, after using a mitogen in the interval [2, 5] the queue will be {1 2 2 3 3 3 3 4 4 5}. After some operations this queue could become very long, and I can’t figure out maximum count of cells of same type. Could you help me?

Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases.

For each case, the first line contains 2 integers (1 <= n,m<= 50000) indicating the number of cell types and the number of operations.

For the following m lines, each line represents an operation. There are only two kinds of operations: Q and D. And the format is:

“Q l r”, query the maximum number of cells of same type in the interval [l, r];
“D l r”, double the cells in the interval [l, r];

(0 <= r – l <= 10^8, 1 <= l, r <= the number of all the cells)

Output
For each case, output the case number as shown. Then for each query "Q l r", print the maximum number of cells of same type in the interval [l, r].

Take the sample output for more details.

Sample Input
1
5 5
D 5 5
Q 5 6
D 2 3
D 1 2
Q 1 7

Sample Output
Case #1:
2
3

Source
2014 Multi-University Training Contest 10

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll __int64
using namespace std;
const int MAXN=40010;
struct Node
{
int l,r;
ll sum,mark,num;
int mid()
{
return (l+r)>>1;
}
}tree[MAXN<<2];
void pushup(int k)
{
tree[k].num=max(tree[k<<1].num,tree[k<<1|1].num);
tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushdown(int k)
{
if(tree[k].mark>1)
{
tree[k<<1].sum*=tree[k].mark;
tree[k<<1].num*=tree[k].mark;
tree[k<<1].mark*=tree[k].mark;
tree[k<<1|1].sum*=tree[k].mark;
tree[k<<1|1].num*=tree[k].mark;
tree[k<<1|1].mark*=tree[k].mark;
tree[k].mark=1;
}
}
void build(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].mark=1;
if(l==r)
{
tree[k].num=1;
tree[k].sum=1;
return;
}
int mid=(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
pushup(k);
}
void update(int l,int r,int k)
{
if(r<l)
return;
if(tree[k].l>=l&&tree[k].r<=r)
{
tree[k].mark*=2;
tree[k].num*=2;
tree[k].sum*=2;
return;
}
pushdown(k);
int mid=tree[k].mid();
if(r<=mid)
update(l,r,k<<1);
else if(l>mid)
update(l,r,k<<1|1);
else
{
update(l,mid,k<<1);
update(mid+1,r,k<<1|1);
}
pushup(k);
}
void updatesingle(int n,ll val,int k)
{
if(tree[k].l==tree[k].r)
{
tree[k].sum+=val;
tree[k].num+=val;
return;
}
pushdown(k);
int mid=tree[k].mid();
if(n<=mid)
else
pushup(k);
}
ll query(int l,int r,int k)
{
if(r<l)
return 0;
if(tree[k].l>=l&&tree[k].r<=r)
return tree[k].num;
pushdown(k);
int mid=tree[k].mid();
if(r<=mid)
return query(l,r,k<<1);
else if(l>mid)
return query(l,r,k<<1|1);
else
{
ll t1=query(l,mid,k<<1);
ll t2=query(mid+1,r,k<<1|1);
ll t=max(t1,t2);
return t;
}
}
ll querysum(int l,int r,int k)
{
if(r<l)
return 0;
if(tree[k].l>=l&&tree[k].r<=r)
return tree[k].sum;
pushdown(k);
int mid=tree[k].mid();
if(r<=mid)
return querysum(l,r,k<<1);
else if(l>mid)
return querysum(l,r,k<<1|1);
else
return querysum(l,mid,k<<1)+querysum(mid+1,r,k<<1|1);
}
int findloc(int k,ll val)
{
if(tree[k].l==tree[k].r)
return k;
pushdown(k);
if(val>tree[k<<1].sum)
return findloc(k<<1|1,val-tree[k<<1].sum);
else
return findloc(k<<1,val);
}
int main()
{
int t,n,m,flag=1;
char s[2];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
build(1,n,1);
printf("Case #%d:\n",flag++);
while(m--)
{
scanf("%s",s);
ll x,y;
scanf("%I64d%I64d",&x,&y);
int k1=findloc(1,x);
int k2=findloc(1,y);
if(s[0]=='D')
{
if(k1==k2)
{
}
else
{
ll pre_sum=querysum(1,tree[k1].l,1);
ll temp=querysum(1,tree[k2].l-1,1);
update(tree[k1].l+1,tree[k2].l-1,1);
}
}
else
{
ll ans=0;
if(k1==k2)
ans=y-x+1;
else
{
ll pre_sum=querysum(1,tree[k1].l,1);
ans=pre_sum-x+1;
pre_sum=querysum(1,tree[k2].l-1,1);
ans=max(ans,y-pre_sum);
pre_sum=query(tree[k1].l+1,tree[k2].l-1,1);
ans=max(ans,pre_sum);
}
printf("%I64d\n",ans);
}
}
}
return 0;
}


• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
09-23
09-02 664
08-22 577
08-22 446
09-22
09-23
09-20
09-20
09-22

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

eeeaaaaa

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

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