楼房重建

https://www.lydsy.com/JudgeOnline/problem.php?id=2957

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 inline int read()
 5 {
 6     int x=0,f=1;char ch=getchar();
 7     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 8     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 9     return x*f;
10 }
11 
12 /********************************************************************/
13 
14 const int maxn = 1e6+7;
15 int n,m,sum,sz;
16 //sum: kuai(s),  sz:   ge(s)/kuai
17 int num[maxn];
18 double k[maxn];
19 double maxv[maxn];
20 vector<double> see[500];
21 int l[500],r[500];
22 
23 void makeblock()
24 {
25     memset(maxv,0,sizeof(maxv));
26     sz=sqrt((double)n*1.05);
27     for(sum=1;sum*sz<n;sum++)
28     {
29         l[sum]=(sum-1)*sz+1;
30         r[sum]=sum*sz;
31         for(int i=l[sum];i<=r[sum];i++) num[i]=sum;
32     }
33     l[sum]=sz*(sum-1)+1;
34     r[sum]=n;
35     for(int i=l[sum];i<=r[sum];i++) num[i]=sum;
36 }
37 
38 void update(int a, int b)
39 {
40     double aa=a,bb=b;
41     k[a]=bb/aa;
42     maxv[num[a]]=0;
43     see[num[a]].clear();
44     double tmp=0;
45     for(int i=l[num[a]];i<=r[num[a]];i++)
46     {
47         if(k[i]>maxv[num[a]])
48         {
49             maxv[num[a]]=k[i];
50             see[num[a]].push_back(k[i]);
51         }
52     }
53 }
54 
55 void query()
56 {
57     int ans=0;
58     double tmp=0;
59     for(int i=1;i<=sum;i++)
60     {
61         if(!see[i].empty())
62         {
63             ans+=see[i].end()-upper_bound(see[i].begin(),see[i].end(),tmp);
64             tmp=max(tmp,maxv[i]);
65         }
66     }
67     printf("%d\n", ans);
68 }
69 
70 int main(){
71     n=read(),m=read();
72     makeblock();
73     while(m--)
74     {
75         int a, b;
76         scanf("%d%d",&a,&b);
77         update(a, b);
78         query();
79     }
80 }

 

posted @ 2018-09-08 10:16  ouyang_wsgwz  阅读(130)  评论(0编辑  收藏  举报