第一次写分块, 写了之后觉得真的是暴力的一比。
题解:先讲n分成 sqrt(n)块,记得补上末尾的, 然后就是对于每一次更新操作, 都重新的讲这个块里面的有效楼放入vector里面, 然后再从头开始upper_bound查找就好了。
还是再说一句, 真的暴力。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 4 #define LL long long 5 #define ULL unsigned LL 6 #define fi first 7 #define se second 8 #define pb push_back 9 #define lson l,m,rt<<1 10 #define rson m+1,r,rt<<1|1 11 #define max3(a,b,c) max(a,max(b,c)) 12 #define min3(a,b,c) min(a,min(b,c)) 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const LL mod = (int)1e9+7; 16 const int N = 2e5 + 100; 17 double h[N]; 18 int belong[N]; 19 int tot, n, m; 20 struct Node{ 21 int l, r; 22 vector<double> vc; 23 }blo[N]; 24 void Build(){ 25 m = sqrt(n); 26 tot = n / m; 27 if(n%m) tot++; 28 for(int i = 1; i <= n; i++){ 29 belong[i] = (i-1)/m + 1; 30 } 31 for(int i = 1; i <= tot; i++){ 32 blo[i].l = (i-1)*m + 1; 33 blo[i].r = i*m; 34 } 35 blo[tot].r = n; 36 } 37 int solve(int k, int H){ 38 double hh = 1.0 * H / k; 39 int to = belong[k]; 40 h[k] = hh; 41 blo[to].vc.clear(); 42 double mx = 0.0; 43 for(int i = blo[to].l; i <= blo[to].r; i++){ 44 if(mx < h[i]){ 45 mx = h[i]; 46 blo[to].vc.pb(h[i]); 47 } 48 } 49 mx = 0.0; 50 int ans = 0; 51 for(int i = 1; i <= tot; i++){ 52 if(blo[i].vc.size() == 0) continue; 53 ans += blo[i].vc.end() - upper_bound(blo[i].vc.begin(), blo[i].vc.end(), mx); 54 mx = max(mx, *blo[i].vc.rbegin()); 55 } 56 return ans; 57 } 58 int main(){ 59 int q; 60 scanf("%d%d", &n, &q); 61 Build(); 62 while(q--){ 63 int k, H; 64 scanf("%d%d", &k, &H); 65 printf("%d\n", solve(k,H)); 66 } 67 return 0; 68 }