I Hate It



Description

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
 

Input

本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
 

Output

对于每一次询问操作,在一行里面输出最高成绩。
 

Sample Input

5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
 

Sample Output

5 6 5 9

Hint

Huge input,the C function scanf() will work better than cin 



#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
const int N = 200005;
int n, m, bit[N], num[N];
void update(int x, int v)
{
    num[x] = v;
    while (x <= n)
    {
        bit[x] = max(bit[x], v);
        x += (x&(-x));
        //整数运算 x&(-x),当x为0时结果为0;
        //x为奇数时,结果为1;
        //x为偶数时,结果为x中2的最大次方的因子。
    }
}

int query(int l, int r)
{
    int ans = num[r];
    while (l != r)
    {
        for (r -= 1; r - (r&(-r)) >= l; r -= (r&(-r)))
            ans = max(ans, bit[r]);
        ans = max(ans, num[r]);
    }
    return ans;
}

void build()
{
    memset(bit, 0, sizeof(bit));
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &num[i]);
        update(i, num[i]);
    }
}
int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        build();
        char s[10];
        int a, b;
        while (m--)
        {
            scanf("%s%d%d", s, &a, &b);
            if (s[0] == 'U') update(a, b);
            else printf("%d\n", query(a, b));
        }
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 200000
using namespace std;
int n,m,a,b;
int maxv[maxn << 2];
void pushup(int rt)
{
    maxv[rt] = max(maxv[rt<<1], maxv[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&maxv[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int l,int r,int rt)
{
    if(l==r)
    {
        maxv[rt]=b;
        return ;
    }
    int m=(l+r)>>1;
    if(a<=m)   update(lson);
    else  update(rson);
    pushup(rt);
}
int query(int l,int r,int rt)
{
    if(a<=l&&r<=b)
    {
        return maxv[rt];
    }
    int m=(l+r)>>1;
    int max1=0;
    if(a<=m) max1 = max(query(lson), max1);
    if(b>m) max1 = max(query(rson), max1);
    return max1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        build(1,n,1);
        while(m--)
        {
            char s[3];
            scanf("%s%d%d",s,&a,&b);
            if(s[0]=='U')  update(1,n,1);
            else printf("%d\n",query(1,n,1));
        }
    }
    return 0;
}






<think>我们注意到用户想要修改C语言代码,使用switch语句来处理字符串(如'love', 'hate', 'like')并输出相应的情感回应。 但是,在C语言中,switch语句只能用于整型或枚举类型,不能直接用于字符串。因此,我们需要将字符串转换为可switch的类型,比如使用枚举或哈希值。 这里提供两种方法: 方法1:使用if-else链(但用户要求用switch,所以不采用) 方法2:将字符串映射为整数,然后使用switch。 我们可以为每个字符串设计一个哈希函数,将字符串转换为整数,然后switch这个整数。但是要注意哈希冲突。 另一种更简单的方法是:使用strcmp函数比较字符串,然后根据比较结果设置一个整型变量作为switch的条件。但是这样仍然需要多个if-else来判断字符串,然后再用switch,显得多此一举。 因此,我们考虑使用一种常见的技巧:将字符串哈希成一个整数(比如使用简单的哈希函数,然后处理可能的冲突)或者使用预定义的枚举。 步骤: 1. 定义一个枚举类型,将每个字符串映射为一个整数常量。 2. 写一个函数,输入字符串,返回对应的枚举值(通过if-else比较字符串,返回枚举常量)。 3. 然后使用switch处理这个枚举值。 虽然switch之前还是用了if-else,但是我们将字符串比较封装在一个函数中,这样主逻辑的switch看起来清晰。 但是,用户明确要求使用switch语句来处理字符串,所以我们需要在switch中处理字符串,这是不可能的。因此,我们必须将字符串转换为整数。 这里我们采用一种简单的方法:使用一个函数将字符串映射为整数(通过比较字符串,返回一个代表不同字符串的整数),然后switch这个整数。 具体实现: 定义枚举: enum Emotion {LOVE, HATE, LIKE, UNKNOWN}; 写一个函数:enum Emotion get_emotion_code(const char *str); 然后在主函数中: enum emotion_code = get_emotion_code(input_string); switch(emotion_code) { case LOVE: ... break; case HATE: ... break; case LIKE: ... break; default: ... break; } 这样,我们就用switch处理了字符串的“情况”。 下面我们写一个示例代码: 注意:用户没有提供原始代码,所以我们从头写一个。 假设程序要求用户输入一个字符串,然后输出相应的情感回应。</think>以下是一个使用 `switch` 语句处理字符串情感输入的 C 语言实现方案。虽然 C 语言的 `switch` 不支持直接处理字符串,但我们可以通过将字符串映射为枚举值来实现类似功能: ```c #include <stdio.h> #include <string.h> // 定义情感枚举类型 typedef enum { LOVE, HATE, LIKE, UNKNOWN } Emotion; // 将字符串映射为枚举值 Emotion get_emotion_code(const char* str) { if (strcmp(str, "love") == 0) return LOVE; if (strcmp(str, "hate") == 0) return HATE; if (strcmp(str, "like") == 0) return LIKE; return UNKNOWN; } int main() { char input[20]; printf("Enter your emotion: "); scanf("%s", input); // 获取情感编码 Emotion emotion = get_emotion_code(input); // 使用switch处理情感响应 switch(emotion) { case LOVE: printf("❤️ Love makes the world go round!\n"); break; case HATE: printf("😠 Hate is a strong emotion, let's talk it out.\n"); break; case LIKE: printf("👍 I like that too! What else do you enjoy?\n"); break; default: printf("🤔 I'm not sure how to respond to '%s'.\n", input); break; } return 0; } ``` ### 关键实现说明: 1. **枚举映射** 通过 `get_emotion_code()` 函数将字符串转换为枚举值,这是实现 `switch` 处理字符串的关键技术[^2] 2. **字符串比较** 使用 `strcmp()` 进行精确字符串匹配(区分大小写) 3. **扩展性** 只需在枚举和映射函数中添加新条目即可支持更多情感类型 ### 使用示例: ```shell $ gcc emotion.c -o emotion $ ./emotion Enter your emotion: love ❤️ Love makes the world go round! $ ./emotion Enter your emotion: hate 😠 Hate is a strong emotion, let's talk it out. ``` ### 改进建议: 1. 添加大小写不敏感处理: ```c #include <ctype.h> // 在比较前转换为小写 void to_lower(char* str) { for (int i = 0; str[i]; i++) { str[i] = tolower(str[i]); } } ``` 2. 支持词组匹配(如 "I love"): ```c if (strstr(input, "love") != NULL) return LOVE; ``` 3. 添加更多情感类型: ```c typedef enum { LOVE, HATE, LIKE, JOY, // 新增 ANGER, // 新增 UNKNOWN } Emotion; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值