最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

??透傎愵}目講解_Different Integers(樹狀數(shù)組)

2022-04-24 10:40 作者:Clayton_Zhou  | 我要投稿

//? https://ac.nowcoder.com/acm/contest/20322/J??

#include <algorithm>

#include <iostream>

#include <cstring>

?#include <vector>

?#define endl '\n'

?

using namespace std;

const int maxn = 1e5 + 9;?

int n, m;

int first[maxn], lst[maxn], a[maxn], ans[maxn];

int cnt;

int c[maxn];

struct node{

? ? int l, r, id;

? ? bool operator<(const node &B){

? ? ? ? return r < B.r;

? ? }

}d[maxn];

?// 樹狀數(shù)組更新

/*

for(? i=1;i<=16;i++)

? ? ? ? printf("i =%2d,i&(-i) =%2d\n",i,i&(-i));

i&(-i) 給出 i 最低位的權(quán)重

*/

void update(int i, int k){

? ? while(i <= n) c[i] += k, i += i & (-i);? ? // 父節(jié)點

}

int qry(int i){

? ? int ans = 0;

? ? // 子節(jié)點, 如果i=3, 下一個節(jié)點為i=2

? ? while(i) ans += c[i], i -= i & (-i);? ??

? ? return ans;?

}

?

void work()

{

? ? ?cnt = 0;

? ? memset(first,0 ,sizeof first);

? ? ?memset(lst,0 ,sizeof lst);

? ? memset(c,0 ,sizeof c);

? ??

? ? for(int i = 1; i <= n; ++i){

? ? ? ? cin >> a[i];

? ? ? ? if(!first[a[i]]) first[a[i]] = i, ++cnt;

? ? ? ? lst[a[i]] = i;??

? ? }

? ? for(int i = 1; i <= m; ++i){

? ? ? ? cin >> d[i].l >> d[i].r;

? ? ? ? d[i].id = i;

? ? }

? ? sort(d + 1, d + 1 + m);

? ? int j = 1;

? ? for(int i = 1; i <= n; ++i){

? ? ? ? while(i == d[j].r && j <= m){// 因為數(shù)據(jù)包含 [r,n],所以先求答案再更新?

? ? ? ? ? ? ans[d[j].id] = cnt + qry(d[j].l);

? ? ? ? ? ? ++j;? ? // j不能減少,所以按照d[j].r排序

? ? ? ? }

? ? ? ? if(lst[a[i]] == i){

? ? ? ? ? ? update(first[a[i]], 1);

? ? ? ? ? ? --cnt;

? ? ? ? }

? ? }

? ? for(int i = 1; i <= m; ++i) //cout << ans[i] << endl;

? ? ? ?printf("%d\n" ,ans[i] );

}?

int main()

{? ?

? ? while(cin >> n >> m)

? ? work();

? ? return 0;

}


??透傎愵}目講解_Different Integers(樹狀數(shù)組)的評論 (共 條)

分享到微博請遵守國家法律
山阳县| 安乡县| 通榆县| 惠东县| 连城县| 平远县| 南澳县| 屏东市| 平昌县| 英山县| 杨浦区| 鲜城| 青海省| 阿拉善左旗| 玉屏| 陆良县| 台山市| 镇江市| 巩留县| 老河口市| 舒兰市| 和政县| 莱州市| 清河县| 新源县| 柯坪县| 阜阳市| 澄江县| 颍上县| 林周县| 黑河市| 宜州市| 左权县| 濮阳县| 河间市| 三明市| 高州市| 莎车县| 曲阜市| 腾冲县| 阿克陶县|