hdu3577 区间加值,区间查询最大值

裸==

1.从x更新到y-1

2.注意格式

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 #define n 1000000
 6 int ans[100005],maxv[4000005],addv[4000005];
 7 void update(int o,int l,int r,int y1,int y2,int v)
 8 {
 9   int mid=l+(r-l)/2;
10   if (y1<=l&&y2>=r) addv[o]+=v;
11   else{
12     if (y1<=mid) update(o*2,l,mid,y1,y2,v);
13     if (y2>mid) update(o*2+1,mid+1,r,y1,y2,v);
14   }
15   maxv[o]=0;
16   if (r>l) maxv[o]=max(maxv[o*2],maxv[o*2+1]);
17   maxv[o]+=addv[o];
18 }
19 int query(int o,int l,int r,int y1,int y2,int add)
20 {
21   int mid=l+(r-l)/2,tmp=0;
22   if (y1<=l&&y2>=r) return maxv[o]+add;
23   else{
24     if (y1<=mid)tmp=max(tmp,query(o*2,l,mid,y1,y2,add+addv[o]));
25     if (y2>mid)tmp=max(tmp,query(o*2+1,mid+1,r,y1,y2,add+addv[o]));
26     return tmp;
27   }
28 }
29 int main()
30 {
31   int T,t,p,k,m,i,tmp,x,y;
32   scanf("%d",&T);
33   for (t=1;t<=T;t++)
34   {
35     memset(maxv,0,sizeof(maxv));
36     memset(addv,0,sizeof(addv));
37     scanf("%d%d",&k,&m);
38     p=0;
39     for (i=1;i<=m;i++)
40     {
41       scanf("%d%d",&x,&y);
42       y--;
43       tmp=query(1,1,n,x,y,0);
44       if (tmp>=k) continue;
45       ans[++p]=i;
46       update(1,1,n,x,y,1);
47     }
48     printf("Case %d:\n",t);
49     for (i=1;i<=p;i++) printf("%d ",ans[i]);
50     printf("\n\n");
51   }
52   return 0;
53 }
View Code

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577

posted on 2015-01-02 17:48  xiao_xin  阅读(123)  评论(0编辑  收藏  举报

导航