I Hate It

Problem 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。="" <="" div="">
 

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[/hint]
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MX 200005
using namespace std;
struct node {
    int l, r, v;
} a[MX << 2];
int Max;
int max(int x, int y)
{
    return x > y ? x : y;
}
void PashUp(int root)
{
    a[root].v = max(a[root << 1].v, a[root << 1 | 1].v);
}
void build_tree(int l, int r, int root)
{
    a[root].l = l;
    a[root].r = r;
    a[root].v = 0;
    if (l == r ) {
        scanf("%d", &a[root].v);
        return ;
    }
    int mid = (l + r) >> 1;
    build_tree(l, mid, root << 1);
    build_tree(mid + 1, r, root << 1 | 1);
    PashUp(root);
}
void update(int l, int r, int root, int k)
{
    if (a[root].l == l && a[root].r == r) {
        a[root].v = k;
        return ;
    }
    int mid = (a[root].l + a[root].r) >> 1;
    if (r <= mid)update(l, r, root << 1, k);
    else if (l > mid)update(l, r, root << 1 | 1, k);
    PashUp(root);
}
int Query(int l, int r, int root)
{
    if (a[root].l >= l && a[root].r <= r) {
        return a[root].v;
    }
    int mid = (a[root].l + a[root].r) >> 1;
    if (r <= mid)return Query(l, r, root << 1);
    else if (l > mid)return Query(l, r, root << 1 | 1);
    else {
    return max(Query(l, r, root << 1),Query(l, r, root << 1 | 1));
    }
}
int main()
{
    int n, m, l, r;
    char ch;
    while (~scanf("%d%d", &n, &m)) {
        build_tree(1, n, 1);
        while (m--) {
            scanf(" %c", &ch);
            if (ch == 'Q') {
                scanf("%d%d", &l, &r);
                printf("%d\n", Query(l, r, 1));
            }
            if (ch == 'U') {
                scanf("%d%d", &l, &r);
                update(l, l, 1, r);
            }
        }
    }
    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、付费专栏及课程。

余额充值