• CDOJ 1324 卿学姐与公主(分块)


    CDOJ 1324 卿学姐与公主(分块)

    传送门:

    UESTC Online Judge
    http://acm.uestc.edu.cn/#/problem/show/1324

    某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏

    在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。

    英勇的卿学姐拔出利刃冲向了拯救公主的道路。

    走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。

    在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。

    卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从LL到RR这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。

    最开始每个士兵的受到的伤害都是0

    Input

    第一行两个整数N,QN,Q表示总共有NN个士兵编号从11到NN,和QQ个操作。

    接下来QQ行,每行三个整数,首先输入一个tt,如果tt是11,那么输入p,xp,x,表示卿学姐攻击了pp这个位置的士兵,并造成了xx的伤害。如果tt是22,那么输入L,RL,R,表示卿学姐想知道现在[L,R][L,R]闭区间内,受伤最严重的士兵受到的伤害。

    1N1000001≤N≤100000

    1Q1000001≤Q≤100000

    1pN1≤p≤N

    1x1000001≤x≤100000

    1LRN1≤L≤R≤N

    Output

    对于每个询问,回答相应的值

    Sample input and output

    Sample InputSample Output
    5 4
    2 1 2
    1 2 4
    1 3 5
    2 3 3
    
    0
    5
    

    Hint

    注意可能会爆int哦

    Source

    2016 UESTC Training for Data Structures
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5+7;
     4 int belong[maxn], num, l[maxn], r[maxn];
     5 int n, q, block;
     6 long long a[maxn], Max[maxn];
     7 //num 分块的个数
     8 //block 块的大小
     9 //belong[i]表示i属于哪一块
    10 //l[i]表示i这块的左端点位置
    11 //r[i]表示i这块的右端点位置
    12 void build(){
    13     block = sqrt(n);
    14     num = n / block;
    15     if(n % block) num++;
    16     for(int i = 1; i <= num; i++){
    17         l[i] = (i - 1)*block + 1, r[i] = i*block;
    18     }
    19     r[num] = n;
    20     for(int i = 1; i <= n; i++){
    21         belong[i] = (i - 1)/block + 1;
    22     }
    23     for(int i = 1; i <= num; i++){
    24         for(int j = l[i]; j <= r[i]; j++){
    25             Max[i] = max(Max[i], a[j]);
    26         }
    27     }
    28 }
    29 void update(int x, int y){
    30     a[x] += y;
    31     Max[belong[x]] = max(Max[belong[x]], a[x]);
    32 }
    33 long long query(int x, int y){
    34     long long ans = 0;
    35     if(belong[x] == belong[y]){
    36         for(int i = x; i <= y; i++){
    37             ans = max(ans, a[i]);
    38         }
    39         return ans;
    40     }
    41     for(int i = x; i <= r[belong[x]]; i++){
    42         ans = max(ans, a[i]);
    43     }
    44     for(int i = belong[x] + 1; i < belong[y]; i++){
    45         ans = max(ans, Max[i]);
    46     }
    47     for(int i = l[belong[y]]; i <= y; i++){
    48         ans = max(ans, a[i]);
    49     }
    50     return ans;
    51 }
    52 int main(){
    53     scanf("%d%d", &n, &q);
    54     for(int i = 1; i <= q; i++){
    55         int op, l, r;
    56         scanf("%d%d%d", &op, &l, &r);
    57         if(op == 1) update(l, r);
    58         else printf("%lld
    ", query(l, r));
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    Nginx 服务器安装及配置文件详解
    Linux 之 压缩解压缩
    RPM方式安装MySQL5.6
    Linux 之 搜索
    zabbix客户端安装
    JAVA输出指定目录下的子目录和子文件
    LoadRunner 11 安装破解
    SVN合并操作实践
    (转)SVN分支/合并原理及最佳实践
    MYSQL 免安装版(windows 7/64)
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8117443.html
Copyright © 2020-2023  润新知