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
题意:首先给你一串长度为n的序列,序列为1.2,3,4.,5...然后D操作l,r表示将区间l-r里的数翻倍,Q操作查询l-r中的个数最多的数。。
线段树能做。。。完全想不到。。按照题目模拟做(他不说模拟吗= =)。。毫无意外TLE。。。。
建立线段树1-n,要用一个单点更新和一个区间更新,线段树里保存这个数的数量,和1-i的和用sum表示(i为当前数),还有个懒惰标记。。每次更新先找到l,r对应的l',r'在线段树里的位置,如果在同一个位置(线段树里)直接r-l+1.。。不同的话,单点更新l'和r'然后更新l'+1-r'-1这一段。。。查询差不多。。
#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)
updatesingle(n,val,k<<1);
else
updatesingle(n,val,k<<1|1);
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)
{
updatesingle(tree[k1].l,y-x+1,1);
}
else
{
ll pre_sum=querysum(1,tree[k1].l,1);
ll temp=querysum(1,tree[k2].l-1,1);
updatesingle(tree[k1].l,pre_sum-x+1,1);
updatesingle(tree[k2].l,y-temp,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;
}