xmu1166求第k小數 樹狀數組/線段樹

來源:互聯網
上載者:User

 http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1165

 

題意:有3中操作

1 a將a加入,如果a已經存在忽略該操作

2 a將a刪除,如果不存在忽略該操作

3 a查詢第a大的數,不存在輸出-1

 

分析:思想和pku2182一樣,比2182簡單。。。直接線段樹或樹狀數組+二分。。。

 

其實從這個角度而言,樹狀數組也可以求最大最小值了。。。呵呵,求最小值就是第1小值嘛。。。

 

代碼:

#include <stdio.h>#include <algorithm>#include <cmath>#include <iostream>using namespace std;const int N=100002;int a[N], sum[N], n;int query(int i){int tmp = 0;for( ; i>0; i-=i&(-i))tmp += sum[i];return tmp;}void update(int i, int v){for( ; i<N; i+=i&(-i))sum[i] += v;}int bs(int t){int l, r, mid;l=1;r=N-1;while(l<=r){mid = (l+r)/2;if(query(mid)>=t)r = mid-1;elsel = mid+1;}if(l==N)return -1;return l;}int main(){int i, j, x, y, ans;//freopen("D.in", "w", stdout);while(scanf("%d", &n)!=EOF){for(i=0; i<N; i++){sum[i] = 0;a[i] = 0;}while(n--){scanf("%d%d", &x, &y);if(x==1){if(a[y]==0){update(y, 1);a[y] = 1;}}else if(x==2){if(a[y]!=0){update(y, -1);a[y] = 0;}}else{ans = bs(y);printf("%d\n", ans);}}}return 0;}

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.