cf 442 div2 F. Ann and Books(莫队算法)
2017-10-26 18:41
337 查看
cf 442 div2 F. Ann and Books(莫队算法)
题意:
\(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\)
每次查询区间\([l,r]内有多少对(i,j)满足l <= i <= j <= r 且 sum[j] - sum[i-1] = k\)
思路:
区间左右端点的挪动对答案的贡献符合加减性质,直接用莫队算法即可
复杂度\(O(n * sqrt(n) * log(maxsum))\) 过高
考虑先离散化预处理出所有位置 将\(log\)去掉
#include<bits/stdc++.h> #define LL long long using namespace std; const int N = 2e5 + 10; struct Q{ int l,r,bl,id; Q(){}; bool operator<(const Q&rhs){ if(bl == rhs.bl) return r < rhs.r; return bl < rhs.bl; } }qr ; int n,k; LL ans ,value ; int x ,y ,z ,cnt[N * 3],type ; vector<LL> se; int main(){ while(cin>>n>>k){ memset(cnt, 0, sizeof(cnt)); se.clear(); for(int i = 1;i <= n;i++) scanf("%d",&type[i]); for(int i = 1;i <= n;i++){ scanf("%d",&value[i]); if(type[i] == 1) value[i] += value[i-1]; else value[i] = value[i-1] - value[i]; } for(int i = 0;i <= n;i++) { se.push_back(value[i]); se.push_back(value[i] + k); se.push_back(value[i] - k); } sort(se.begin(), se.end()); se.erase(unique(se.begin(),se.end()),se.end()); for(int i = 0;i <= n;i++){ x[i] = lower_bound(se.begin(),se.end(),value[i]) - se.begin(); y[i] = lower_bound(se.begin(),se.end(),value[i] + k) - se.begin(); z[i] = lower_bound(se.begin(),se.end(),value[i] - k) - se.begin(); } int block_size = sqrt(n + 0.5); int q; cin>>q; for(int i = 0;i < q;i++){ scanf("%d%d",&qr[i].l,&qr[i].r); qr[i].id = i; qr[i].l--; qr[i].bl = qr[i].l / block_size; } sort(qr, qr + q); int L = 0,R = -1; LL res = 0; for(int i = 0;i < q;i++){ while(qr[i].l > L) { cnt[x[L]]--; res -= cnt[y[L++]]; } while(qr[i].l < L) { res += cnt[y[--L]]; cnt[x[L]]++; } while(qr[i].r > R){ res += cnt[z[++R]]; cnt[x[R]]++; } while(qr[i].r < R) { cnt[x[R]]--; res -= cnt[z[R--]]; } ans[qr[i].id] = res; } for(int i = 0;i < q;i++) printf("%lld\n",ans[i]); } return 0; }
相关文章推荐
- 【打CF,学算法——一星级】Codeforces Round #313 (Div. 2) A. Currency System in Geraldion
- Codeforces Round #442 (Div. 2) 877 F - Ann and Books 莫队算法 离散化
- cf 442 div2 D bfs最短路
- 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
- 【打CF,学算法——二星级】Codeforces Round #312 (Div. 2) A Lala Land and Apple Trees
- 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
- CF - 286 - div2 - C - Mr. Kitayuta, the Treasure Hunter (DP)
- CF 245 div2
- CF 196 Div 2 D (树形dp)
- cfCodeforces Round #432 (Div. 2,
- CF 159 div2 d
- CF#369(Div2) C. Coloring Trees (DP)
- 使用numpy完成item-cf算法
- 【打CF,学算法——二星级】CF 520B Two Buttons
- 【打CF,学算法——三星级】Codeforces 698A Vacations (动态规划)
- cf 301 div2
- 【CF】222 Div.1 B Preparing for the Contest
- CF 230 div2 C(圆内整点数)
- 【MZ】CF 359C - 359E #209 (Div. 2)
- CF 143 div2 C