Codeforces 387E 树状数组
来源:互联网 发布:淘宝兰亭序放大版字帖 编辑:程序博客网 时间:2024/06/01 10:32
http://codeforces.com/problemset/problem/387/E
记录每个数的下标,对数从小到大考虑。如果该数需要保留,那么就往set中插入该数的下标。否则可以查询出当前数左边和右边第一个比它小的数的下标,可以统计这个区间中已经删掉的数的数量,就可以计算出当前数的贡献。
#include<bits/stdc++.h>using namespace std;const int MAX_LEN = 1e6+10;typedef long long ll;int n, m;ll tree[MAX_LEN];int id[MAX_LEN], keep[MAX_LEN];void add(int pos, int value){ while(pos <= n){ tree[pos] += value; pos += (pos&-pos); } return ;}ll query(int l, int r){ int ans = 0; while(r){ ans += tree[r]; r -= (r&-r); } while(l){ ans -= tree[l]; l -= (l&-l); } return ans;}set<int> se;set<int> reverseSe;int main(){ std::ios::sync_with_stdio(false); memset(keep, false, sizeof(keep)); scanf("%d%d",&n, &m); int tmp; for (int i = 1; i <= n; i++) scanf("%d", &tmp), id[tmp] = i; for (int i = 1; i <= m; i++) scanf("%d", &tmp), keep[tmp] = true; se.insert(0); se.insert(n+1); reverseSe.insert(0); reverseSe.insert(-n-1); if (n == m){ cout << 0 << endl; return 0; } ll ans = 0; for (int i = 1; i <= n; i++){ if (keep[i]) se.insert(id[i]), reverseSe.insert(-id[i]); else{ int l = -*reverseSe.upper_bound(-id[i]); int r = *se.upper_bound(id[i]); //cout << "l: " << l << " r: " << r << endl; ans += r-l-1; ans -= query(l, r-1); add(id[i], 1); } } printf("%I64\n", ans); return 0;}
阅读全文
0 0
- Codeforces 387E 树状数组
- Codeforces 276E(树状数组)
- Codeforces 387E George and Cards(二分+树状数组)
- George and Cards - CodeForces 387E 树状数组
- Codeforces 387E George and Cards(二分+树状数组)
- codeforces 191 E (树状数组+二分)
- [二维树状数组] codeforces 707E. Garlands
- 树状数组(Garlands,codeforces 707e)
- Codeforces 785E 分块+树状数组
- codeforces 61E 树状数组 逆向
- Codeforces-831E Cards Sorting(树状数组)
- codeforces 869E(哈希&二维树状数组)
- 【codeforces】163E. e-Government AC自动机+树状数组
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- Codeforces 387E George and Cards【思维+RMQ+二分+树状数组】被卡常= =
- CodeForces 121E Lucky Array (树状数组)
- CodeForces 315E - Sereja and Subsequences 阅读理解+树状数组
- CodeForces - 369E Valera and Queries 离线+树状数组
- PDF转Word方法大盘点:看了这一篇,就不用再找转换技巧了
- App server和Web server的区别
- thinkPHP 截取字符串
- Mr. Frog’s Problem C
- python倒排索引
- Codeforces 387E 树状数组
- Mysql读写分离实例
- linux中系统延时及定时任务
- UVa439 习题 6-4 骑士的移动(Knight Moves)
- 2017杭电多校第三场1005 RXD and dividing(最大化k个斯坦纳树分块的最大权值和)HDU6060
- 聚集索引与非聚集索引的区别
- 设计并发服务器,使用多进程与多线程有什么区别?
- 在Windows的CMD中如何设置支持UTF8编码
- 通知