zoukankan      html  css  js  c++  java
  • 卿学姐与公主 UESTC

    题意:http://acm.uestc.edu.cn/#/problem/show/1324 中文题,自己看喽。

    题解:分块模板,update时顺便更新块属性。ask时先判掉belong[l]==belong[r]。build函数时直接用模板喽。

    坑:打错了个字母,改了一下还改错了。还写错个括号。

    ac代码:

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<queue>
    #include<string.h>
    #include<iostream>
    #include<cmath>
    using namespace std;
    const long long maxn = 100005;
    int belong[maxn], num, block, l[maxn], r[maxn],  n, q;
    long long Max[maxn], a[maxn];
    void build() 
    {
        block = sqrt(n);
        num = n / block;
        if (n%block) num++;
        for (int i = 1; i <= num; i++) 
            l[i] = (i - 1) * block + 1, r[i] = i*block;
        r[num] = n;
        for (int i = 1; i <= n; i++) {
            belong[i] = (i - 1) / block + 1;
        }
            
        for (int i = 1; i <= num; i++) 
            for (int j = l[i]; j <= r[i]; j++)
                Max[i] = max(Max[i], a[j]);
        
    }
    void update(int x, int y) {
        a[x] += y;
        Max[belong[x]] = max(Max[belong[x]], a[x]);
    }
    long long ask(int x, int y) {
        long long ans = 0;
        if (belong[x] == belong[y]) {
            for (int i = x; i <= y; i++) ans = max(ans, a[i]);
            return ans;
        }
        for (int i = x; i <= r[belong[x]]; i++) {
            ans = max(ans, a[i]);
        }
        for (int i = belong[x] + 1; i < belong[y]; i++) {
            ans = max(ans, Max[i]);
        }
        for (int i = l[belong[y]]; i <= y; i++) {
            ans = max(ans, a[i]);
        }
        return ans;
    }
    int main() {
        scanf("%d%d", &n, &q);
        build();
    
        for (int i = 1; i <= q; i++) {
            int op, l, r;
            scanf("%d%d%d", &op, &l, &r);
            if (op == 1)update(l, r);
            else printf("%lld
    ", ask(l, r));
    
        }
    }

     看到了2014年6月的卿学姐刷题推荐http://www.cnblogs.com/qscqesze/p/3852521.html 1320道题吧。。。Orz

    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    YTU 2543: 数字整除
    YTU 2542: 弟弟的作业
    YTU 2541: 汽水瓶
    YTU 2535: C++复数运算符重载(+与<<)
    YTU 2530: 小勇玩lol
    YTU 2520: 小慧唱卡拉OK
    YTU 2517: 打倒魔王↖(^ω^)↗
    YTU 2516: 剪刀石头布
    reload、replace、href、assign、window.history.go(0)的区别
    js 数组排序sort方法
  • 原文地址:https://www.cnblogs.com/SuuT/p/8535433.html
Copyright © 2011-2022 走看看