codeforce 1234 B2、D

1 篇文章 0 订阅
1 篇文章 0 订阅

D. Distinct Characters Queries
time limit per test: 2 seconds

memory limit per test: 256 megabytes

You are given a string ss consisting of lowercase Latin letters and qq queries for this string.

Recall that the substring s[l;r] of the string ss is the string SlSl+1…SrSlSl+1…Sr. For example, the substrings of “codeforces” are “code”, “force”, “f”, “for”, but not “coder” and “top”.

There are two types of queries:

1 pos c (1 ≤ pos ≤ |s|, c is lowercase Latin letter): replace Spos with c (set Spos:=c);
2 l r (1 ≤ l ≤ r ≤ |s|): calculate the number of distinct characters in the substring s[l;r].
Input
The first line of the input contains one string s consisting of no more than 10^5 lowercase Latin letters.

The second line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries.

The next qq lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output
For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.

Examples
input
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
output
3
1
2
input
dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11
output
5
2
5
2
6
线段树:

#include<bits/stdc++.h>
using namespace std;
 
const int MAXN = 1e5;
 
map<int, int> tree[4 * MAXN + 10];
int fans[26];
string s;
 
map<int, int> combine(map<int, int> a, map<int, int> b) {
        for(int i = 0; i < 26; i++) {
                a[i] += b[i];
        }
        return a;
}
 
void build(int id, int l, int r) {
        if(l == r) {
                tree[id][s[l] - 'a']++; 
                return;
        }
        int mid = (l + r) / 2;
        build(2 * id, l, mid);
        build(2 * id + 1, mid + 1, r);
        tree[id] = combine(tree[2 * id], tree[2 * id + 1]);
} 
 
void update(int id, int l, int r, int x, int v) {
		tree[id][s[x] - 'a']--;
        tree[id][v]++;
        if(l == r) {
        		s[x] = char(v + (int)'a');
                return;
        }
        int mid = l + (r - l) / 2;
        if(x <= mid) {
                update(2 * id, l, mid, x, v);
        } else {
                update(2 * id + 1, mid + 1, r, x, v);
        }
        //tree[id] = combine(tree[2 * id], tree[2 * id + 1]);
}
 
void query(int id, int l, int r, int i, int j) {
        if(i > j) {
                return;
        } 
        if(i == l && j == r) {
        		for(int it = 0; it < 26; it++)
        			fans[it] += tree[id][it];
        			return;
        }
        int mid = l + (r - l) / 2;
        query(2 * id, l, mid, i, min(mid, j));
        query(2 * id + 1, mid + 1, r, max(mid + 1, i), j);
}
 
int main() {
        ios_base:: sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        for(int i = 0; i <= 4 * MAXN; i++)
                tree[i].clear();
        cin >> s;
        build(1, 0, s.length() - 1);
        int q;
        cin >> q;
        while(q--) {
                int type;
                cin >> type;
                if(type == 1) {
                        int i;
                        char c;
                        cin >> i >> c;
                        update(1, 0, s.length() - 1, i - 1, c - 'a');
                } else if(type == 2) {
                		memset(fans, 0, sizeof(fans));
                        int i, j, ans = 0;
                        cin >> i >> j;
                        query(1, 0, s.length() - 1, i - 1, j - 1);
                        for(int i = 0; i < 26; i++) {
                                if(fans[i] > 0) {
                                        ans++;
                                }
                        } 
                        cout << ans << '\n';
                }
        }
        return 0;
}

树状数组:

#include <iostream>
#define N 100005
 
using namespace std;
int b[N][30],q,status,pos,l,r;
string s;
char x;
 
void update(int u, int v, int val)
{
    for (int i=u; i<=N; i+=i&(-i)) b[i][v]+=val;
}
 
int get(int p, int v)
{
    int ans = 0;
    for (int i=p; i; i-=i&(-i)) ans+=b[i][v];
    return ans;
}
 
int main()
{
    //freopen("1234D.inp", "r", stdin);
    //freopen("1234D.out", "w", stdout);
    cin >> s;
    cin >> q;
    for (int i=0; i<s.size(); ++i) update(i+1,s[i]-97,1);
    for (int j=1; j<=q; ++j) {
        cin >> status;
        if (status==1) {
            cin >> pos >> x;
            update(pos,s[pos-1]-97,-1);
            update(pos,x-97,1);
            s[pos-1] = x;
        }
        else {
            cin >> l >> r;
            int res = 0;
            for (int i=0; i<30; ++i) {
                int c = get(r,i);
                int d = get(l-1,i);
                if (c>d) ++res;
            }
            cout << res << "\n";
        }
    }
    return 0;
}

set:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include <cmath>
#include<set>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
set<int> h[28];
char s[100009];
int main(){
    int i, j, n, m, q, pos, l, r;
    char c;
    scanf("%s",s+1);
    int mx = strlen(s+1);
    for(i = 1; i<=mx; i++){
        h[s[i]-'a'].insert(i);
    }
    for(scanf("%d",&q);q--;){
        scanf("%d",&pos);
        if(pos==1){
            scanf("%d %c",&l,&c);
            h[s[l]-'a'].erase(l);
            s[l] = c;
            h[c-'a'].insert(l);
        } else {
            scanf("%d%d",&l,&r);
            int ans = 0;
            for(i=0;i<=25;i++){
                auto it = h[i].lower_bound(l);
                if(it!=h[i].end()&& *it<=r){
                    ans++;
                }
            }
            printf("%d\n",ans);
        }
        
    }
    
}

B2. Social Network (hard version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions are constraints on n and k.

You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).

Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.

You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID idi (1≤idi≤109).

If you receive a message from idi in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.

Otherwise (i.e. if there is no conversation with idi on the screen):

Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend idi is not displayed on the screen.
The conversation with the friend idi appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.

Input
The first line of the input contains two integers n and k (1≤n,k≤2⋅105) — the number of messages and the number of conversations your smartphone can show.

The second line of the input contains n integers id1,id2,…,idn (1≤idi≤109), where idi is the ID of the friend which sends you the i-th message.

Output
In the first line of the output print one integer m (1≤m≤min(n,k)) — the number of conversations shown after receiving all n messages.

In the second line print m integers ids1,ids2,…,idsm, where idsi should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.

Examples
inputCopy
7 2
1 2 3 2 1 3 2
outputCopy
2
2 1
inputCopy
10 4
2 3 3 1 1 2 1 2 3 3
outputCopy
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):

[];
[1];
[2,1];
[3,2];
[3,2];
[1,3];
[1,3];
[2,1].
In the second example the list of conversations will change in the following way:

[];
[2];
[3,2];
[3,2];
[1,3,2];
and then the list will not change till the end.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include <cmath>
#include<set>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int  maxn = 25;
int tmp[25][maxn];
int len[25],name[25],vis[25];
deque<int> q;
set<int> s;
 
int main(){
    int i, j, n, k;
    scanf("%d%d",&n,&k);
    set<int>::iterator it;
    for(i = 1; i <= n; i++){
        scanf("%d",&j);
        it = s.find(j);
        if(it==s.end()){
            if(q.size()<k){
                s.insert(j);
                q.push_back(j);
            } else{
                q.push_back(j);
                s.erase(q.front());
                s.insert(j);
				q.pop_front();
            }    
        }
        
    }
	printf("%d\n",q.size());
    while(!q.empty()){
        printf("%d ",q.back());
        q.pop_back();
	}
    printf("\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值