HDU 1892 See you~

本文介绍了一种使用二维树状数组解决区间更新及查询问题的方法。通过具体实例展示了如何进行区间加法操作和查询指定区域内的元素总和,同时提供了完整的C++代码实现。

Problem Description
Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~ ~>.<~ ~. I am very sorry, we could not advanced to the World Finals last year.
When coming into our training room, a lot of books are in my eyes. And every time the books are moving from one place to another one. Now give you the position of the books at the early of the day. And the moving information of the books the day, your work is to tell me how many books are stayed in some rectangles.
To make the problem easier, we divide the room into different grids and a book can only stayed in one grid. The length and the width of the room are less than 1000. I can move one book from one position to another position, take away one book from a position or bring in one book and put it on one position.


【题目分析】
特别萌的一道题目。题目名字~~~,符号表情也特别萌。
扯远了(尴尬)。
这道题目是要区间求和,利用容斥原理分割成四个部分很容易地求解。二位的数据结构其实和一维的数据结构相差并不是很大。只需要维护两个下标就好了。方法和原理都是同样的。而且每一个维度的下标都要从1开始(树状数组必须的,如果是线段树的话完全没有必要,个人觉得线段树会被卡掉的,常数略大)。


【代码】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1002;
int a[maxn][maxn];
int tt,kase=0,n;
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int y,int op)
{
    while (x<maxn)
    {
        int k=y;
        while (k<maxn)
        {
            a[x][k]+=op;
            k+=lowbit(k);
        }
        x+=lowbit(x);
    }
}
inline int sum(int x,int y)
{
    int res=0;
    while(x>0)
    {
        int k=y;
        while(k>0)
        {
            res+=a[x][k];
            k-=lowbit(k);
        }
        x-=lowbit(x);
    }
    return res;
}
inline int find(int x,int y)
{
    return sum(x,y)-sum(x-1,y)-sum(x,y-1)+sum(x-1,y-1);
}
int main()
{
    cin>>tt;
    while (tt--)
    {
        cout<<"Case "<<++kase<<":"<<endl;
        cin>>n;
        memset(a,0,sizeof a);
        for (int i=1;i<=maxn;++i)
            for (int j=1;j<maxn;++j)
                add(i,j,1);
        char ch[6];
        while (n--)
        {
            scanf("%s",ch);
            if (ch[0]=='A'||ch[0]=='D')
            {
                int x,y,op;
                cin>>x>>y>>op;
                if (ch[0]=='D') op=-min(op,find(x+1,y+1));
                add(x+1,y+1,op);
                continue;
            }
            if (ch[0]=='M')
            {
                int x1,y1,x2,y2,op;
                cin>>x1>>y1>>x2>>y2>>op;
                int tmp=min(op,find(x1+1,y1+1));
                add(x1+1,y1+1,-tmp),add(x2+1,y2+1,tmp);
                continue;
            }
            else
            {
                int x1,y1,x2,y2;
                cin>>x1>>y1>>x2>>y2;
                if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2);
                int tmp=sum(x2+1,y2+1)-sum(x1,y2+1)-sum(x2+1,y1)+sum(x1,y1);
                cout<<tmp<<endl;
            }
        }
    }
}
要解决被HDU WAF(Web应用防火墙)拒绝的问题并调整代码,可从以下几个方面着手: ### 网络访问规范 - **IP限制**:HDU WAF可能因IP存在异常访问行为而拒绝访问。检查IP是否被列入黑名单,若因频繁请求被临时封禁,可等待封禁时间结束或联系HDU相关技术支持人员说明情况,请求解封。 - **请求频率**:过度频繁的请求会触发WAF的防护机制。调整代码中的请求频率,添加适当的延迟。以下是Python中使用`time.sleep()`函数添加延迟的示例: ```python import time import requests for i in range(10): response = requests.get('http://acm.hdu.edu.cn') print(response.text) time.sleep(1) # 每次请求间隔1秒 ``` ### 代码合规性 - **请求头设置**:确保代码中的请求头信息符合正常浏览器的请求头格式。部分WAF会根据请求头信息判断请求是否正常。以下是Python中设置请求头的示例: ```python import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get('http://acm.hdu.edu.cn', headers=headers) print(response.text) ``` - **请求参数**:检查代码中发送的请求参数是否包含恶意字符或异常内容。确保参数符合题目要求和网站规定。 ### 代码逻辑与题目要求 - **理解题目意思**:仔细阅读题目描述,确保代码逻辑符合题目要求。如VJ的题有很多格式要求,多打一个空格、没换行系统就会评测为presentation error,所以要在代码中严格遵循题目要求的输出格式[^2]。 - **优化算法复杂度**:若代码运行时间过长或占用资源过多,可能会被WAF视为异常请求。对代码中的算法进行优化,降低时间和空间复杂度。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值