欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Gym - 102448B Beza‘s Hangover (树状数组/线段树)

程序员文章站 2022-06-15 08:46:24
DescriptionFriday nights are tricky for UFPE’s ICPC competitors - they must be careful with their plans, after all, they train on saturdays and must be in good shape to help their team. Beza, however, went to a party on his friday night and said that no b...

Description
Friday nights are tricky for UFPE’s ICPC competitors - they must be careful with their plans, after all, they train on saturdays and must be in good shape to help their team. Beza, however, went to a party on his friday night and said that no booze could bring him down. He was wrong.

Beza’s night had N hours, the party’s bar had M distinct beverages and, at each of the N hours, Beza went to the bar and got one of the M drinks. Lucas, who is an experienced drinker, said that a competitor would not be hungover the next day if the total volume of alcohol he drank did not exceed half the number of minutes he spent drinking.

Saturday morning, Beza woke up and noticed he was terribly hungover, and this made him think about the things he did on that night. Since Beza has got a nasty headache, he’s not able to think clearly, and, therefore, asks you to help him answer some questions he has about that night.

Beza’s questions can be of two types:

1 X Y - he wonders if it would be a good idea to drink Y at the X-th hour. Therefore, he changes the beverage of the X-th hour to Y. It is guaranteed that the beverage Y was available at the bar. (1XN)(1≤X≤N)
2 L R - he asks himself if he would be hungover on saturday, if his night only consisted of drinking from the L-th to the R-th hour, given his night’s “drink schedule” at the moment of this query. (1LRN)(1≤L≤R≤N)

Input
The first line of input consists of three integers N, M, and Q, (1N,M,Q2105)(1≤N,M,Q≤2⋅10^5). The next line contains N strings DiD_i, 1Di201≤|D_i|≤20, each one describing the name of the i-th drink Beza had that night. Then, M lines follow, each one containing two entries S and V, 1S201≤|S|≤20, 1V1001≤V≤100, which describe, respectively, the name of a drink the bar had and how many liters of alcohol it had. Finally, Q lines follow, each one containing three integers, as described on the problem’s statement.

Output
For all queries of type 2, you must answer “YES” if Beza would be hungover the next day, given the interval of hours on which he would be drinking, and “NO” otherwise. It is guaranteed that there will be at least one query of type 2.

Example

Input
6 6 5
vodka pitu beats whisky vodka cuba
vodka 30
caipirinha 10
pitu 35
beats 15
whisky 20
cuba 50
2 3 4
1 3 cuba
2 3 3
1 5 cuba
2 1 5

Output
NO
YES
YES

Note
The total amount of hours passed by on a given interval [L,R] is R−L+1.

Solution
单点修改,区间查询,用树状数组会方便一些

Code

#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 7;

int lowbit(int x){return x&-x;}
int n,m,q;
map<string,int>mp;
vector<string>orin;
int val[MX];
int sum[MX<<1];
void add(int pos,int k){
    while(pos <= n){
        sum[pos] += k;
        pos += lowbit(pos);
    }
    return ;
}

int query(int pos){
    int res = 0;
    while(pos > 0){
        res += sum[pos];
        pos -= lowbit(pos);
    }
    return res;
}

bool judge(int L,int R){
    int tmp = query(R) - query(L - 1);
    int min = (R - L + 1) * 60;
    return tmp * 2 > min;
}

int main(){
    std::ios::sync_with_stdio(false);
    cin >> n >> m >> q;
    for(int i = 1;i <= n;++i){
        string s;cin >> s;
        orin.pb(s);
    }
    for(int i = 1;i <= m;++i){
        string s;int tmp;cin >> s >> tmp;
        mp[s] = i;val[i] = tmp;
    }
    for(int i = 1;i <= n;++i){
        int tmp = val[mp[orin[i-1]]];
        add(i,tmp);
    }
    while(q--){
        int op;cin >> op;
        if(op == 1){
            int pos;string s;cin >> pos >> s;
            int pre = query(pos) - query(pos-1);
            int now = val[mp[s]];
            add(pos,-pre);add(pos,now);
        } else{
            int L,R;cin >> L >> R;
            if(judge(L,R)) printf("YES\n"); else printf("NO\n");
        }
    }
}

本文地址:https://blog.csdn.net/qq_43521140/article/details/107120448