国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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ù)組)的評論 (共 條)

分享到微博請遵守國家法律
峨眉山市| 平乡县| 岑溪市| 新昌县| 邛崃市| 保山市| 子长县| 玛曲县| 塔城市| 波密县| 炉霍县| 蓝山县| 晋江市| 龙游县| 石楼县| 彭阳县| 简阳市| 周口市| 北票市| 高雄县| 湄潭县| 临朐县| 霍林郭勒市| 富源县| 道孚县| 碌曲县| 任丘市| 来宾市| 桦南县| 林周县| 砀山县| 乌恰县| 孟津县| 和硕县| 馆陶县| 南澳县| 特克斯县| 津南区| 郸城县| 旬邑县| 高邮市|