【BZOJ2683】简单题

同Mokia…就是数据范围改了改= =双倍经验…
2683: 简单题
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 284 Solved: 125
[Submit][Status][Discuss]
Description
你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

命令

参数限制

内容

1 x y A

1<=x,y<=N,A是正整数

将格子x,y里的数字加上A

2 x1 y1 x2 y2

1<=x1<= x2<=N

1<=y1<= y2<=N

输出x1 y1 x2 y2这个矩形内的数字和

3

终止程序
Input
输入文件第一行一个正整数N。
接下来每行一个操作。

Output
对于每个2操作,输出一个对应的答案。

Sample Input
4

1 2 3 3

2 1 1 3 3

1 2 2 2

2 2 2 3 4

3

Sample Output
3

5

HINT

1<=N<=500000,操作数不超过200000个,内存限制20M。

对于100%的数据,操作1中的A不超过2000。

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 800010
#define SIZE 500010
#define lowbit(x) (x&(-x))
using namespace std;
int w;
int top,opt,L,R,l,r,delta,Top;
struct Query
{
    int op;
    int x,y,A;
    int t,id;
    bool operator <(const Query& a)const
    {
        if (x == a.x && y == a.y) return op < a.op;
        if (x == a.x) return y < a.y;
        return x < a.x;
    }
}que[MAXN],newq[MAXN];
long long ans[MAXN],c[SIZE];
inline void in(int &x)
{
    x=0;char ch = getchar();
    while (!(ch >= '0' && ch <= '9'))   ch = getchar();
    while (ch >= '0' && ch <= '9')  x = x * 10 + ch - '0',ch = getchar();
}
inline void add(int i,long long x)
{
    while (i && i <= w) c[i] += x,i += lowbit(i);
}
inline long long query(int i)
{
    long long ret = 0;
    while (i) ret += c[i],i -= lowbit(i);
    return ret;
}
inline void Solve(int l,int r)
{
    int mid = (l + r) >> 1,tp1 = l,tp2 = mid + 1;
    if (l == r) return;
    for (int i = l;i <= r;i++)
    {
        if (que[i].t <= mid && que[i].op == 1)  add(que[i].y,que[i].A);
        if (que[i].t > mid && que[i].op == 2)   ans[que[i].id] += query(que[i].y) * que[i].A;
    }
    for (int i = l;i <= r;i++)
        if (que[i].t <= mid && que[i].op == 1) add(que[i].y,-que[i].A);
    for (int i = l;i <= r;i++)
        if (que[i].t <= mid) newq[tp1++] = que[i];
        else newq[tp2++] = que[i];
    memcpy(que+l,newq+l,sizeof(Query)*(r - l + 1));
    Solve(l,mid);Solve(mid+1,r);
}
int main()
{
    in(w);
    while (1)
    {
        in(opt);
        if (opt == 3) break;
        switch (opt)
        {
            case 1:
                in(L);in(R);in(delta);
                que[++top].op = opt;que[top].x = L;que[top].y = R;que[top].A = delta;que[top].t = top;
                break;
            case 2:
                in(L);in(R);in(l);in(r);
                que[++top].op = opt;que[top].x = L - 1;que[top].y = R - 1;que[top].t = top;que[top].A = 1;que[top].id = ++Top;
                que[++top].op = opt;que[top].x = L - 1;que[top].y = r;que[top].t = top;que[top].A = -1;que[top].id = Top;
                que[++top].op = opt;que[top].x = l;que[top].y = R - 1;que[top].t = top;que[top].A = -1;que[top].id = Top;
                que[++top].op = opt;que[top].x = l;que[top].y = r;que[top].t = top;que[top].A = 1;que[top].id = Top;
                break;
        }
    }
    sort(que + 1,que + top + 1);
    Solve(1,top);
    for (int i = 1;i <= Top;i++)    printf("%lld\n",ans[i]);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值