HDU-Yuna's confusion 树状数组 Or Multiset
生活随笔
收集整理的這篇文章主要介紹了
HDU-Yuna's confusion 树状数组 Or Multiset
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
比賽的時候竟然沒有想到這題可以用樹狀數(shù)組過,由于數(shù)字的區(qū)間比較小,所以直接開設(shè)一個樹狀數(shù)組,定義sum(i) 表示小于i的數(shù)的總數(shù)。那么判定一個數(shù)是否有的條件就是計算sum(i+1) == sum(i) 便可以了,查找第K大的數(shù),也就可以直接二分了。
代碼如下:
#include <cstdlib> #include <cstring> #include <cstdio> #include <algorithm> #define MAXN 100000 using namespace std;int c[MAXN+5];int lowbit(int x) {return x & -x; }void modify(int x, int val) {for (int i = x; i <= MAXN; i += lowbit(i)) {c[i] += val;} }int sum(int x) {int ret = 0;for (int i = x; i > 0; i -= lowbit(i)) {ret += c[i];}return ret; }int bsearch(int l, int r, int k) {int mid, ret = -1;while (l <= r) {mid = (l + r) >> 1;// printf("mid = %d, sum() = %d\n", mid, sum(mid));// getchar();if (sum(mid) >= k) {ret = mid;r = mid - 1; }else {l = mid + 1; }}return ret; }int main() {int M, x, y, ret;char op[5];while (scanf("%d", &M) == 1) {memset(c, 0, sizeof (c));for (int i = 1; i <= M; ++i) {scanf("%s", op);if (op[0] == 'A') {scanf("%d", &x);modify(x, 1); }else if (op[0] == 'D'){scanf("%d", &x);// printf("%d %d\n", sum(x), sum(x-1));if (sum(x) == sum(x-1)) {puts("No Elment!");}else {modify(x, -1);}}else { // query通過二分查找來對付 scanf("%d %d", &x, &y);y += sum(x);ret = bsearch(x+1, MAXN, y);if (ret == -1) {puts("Not Find!");}else {printf("%d\n", ret);}}} }return 0; }還有一種做法,就是直接用STL中的multiset.
代碼如下:
#include<iostream> #include<algorithm> #include<set> using namespace std;multiset<int> s; multiset<int>::iterator it;int main(){int Q;while( ~scanf("%d",&Q)){s.clear();char ope[10];int x,k;for(int i = 1; i <= Q; i++){scanf("%s", ope);if( ope[0] == 'A' ){scanf("%d",&x);s.insert(x); }else if( ope[0] == 'D' ){scanf("%d",&x);int cnt = s.count(x);if( cnt == 0 ){puts("No Elment!");continue; }s.erase(x);for(int i = 1; i <= cnt-1; i++)s.insert(x); }else {scanf("%d%d",&x,&k);it = s.upper_bound(x);if( it == s.end() ){//|| (it+(k-1) >= s.end() ) ){puts("Not Find!");continue;} int i=1;for(; it != s.end() && i < k; i++, it++); if( it == s.end() ){puts("Not Find!");continue;}printf("%d\n", *(it) );}} } }轉(zhuǎn)載于:https://www.cnblogs.com/Lyush/archive/2012/08/12/2635387.html
總結(jié)
以上是生活随笔為你收集整理的HDU-Yuna's confusion 树状数组 Or Multiset的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx基本原理介绍
- 下一篇: tableau