[BZOJ2683]简单题

题目大意:

一个\(n\times n(n\le5\times10^5)\)的矩阵,初始时每个格子里的数全为\(0\)。\(m(m\le2\times10^5)\)次操作,操作包含以下两种:

  1. 将某个格子加上一个数;
  2. 询问某个子矩阵的值。

思路:

CDQ分治+树状数组。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int M=8e5+1,C=2e5,N=5e5+1;
struct Query {
int type,t,id,x,y,v;
};
Query a[M];
int n,ans[C];
inline bool cmp1 (const Query &p1,const Query &p2) {
if(p1.t==p2.t) {
if(p1.x==p2.x) return p1.y<p2.y;
return p1.x<p2.x;
}
return p1.t<p2.t;
}
inline bool cmp2 (const Query &p1,const Query &p2) {
if(p1.x==p2.x) return p1.y<p2.y;
return p1.x<p2.x;
}
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) val[p]+=x;
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) ret+=val[p];
return ret;
}
};
FenwickTree t;
void cdq(const int &b,const int &e) {
if(b==e) return;
const int mid=(b+e)>>1;
cdq(b,mid);
cdq(mid+1,e);
int p=b,q=mid+1;
for(;q<=e;q++) {
if(a[q].type==1) continue;
for(;p<=mid&&a[p].x<=a[q].x;p++) {
if(a[p].type==1) t.modify(a[p].y,a[p].v);
}
ans[a[q].id]+=t.query(a[q].y)*a[q].v;
}
while(--p>=b) {
if(a[p].type==1) t.modify(a[p].y,-a[p].v);
}
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp2);
}
int main() {
n=getint();
int m=0,cnt=-1;
for(register int opt=getint(),i=0;opt!=3;opt=getint(),i++) {
if(opt==1) {
const int x=getint(),y=getint(),v=getint();
a[++m]=(Query){1,i,cnt,x,y,v};
}
if(opt==2) {
const int x1=getint(),y1=getint(),x2=getint(),y2=getint();
cnt++;
if(x1!=1&&y1!=1) a[++m]=(Query){2,i,cnt,x1-1,y1-1,1};
if(x1!=1) a[++m]=(Query){2,i,cnt,x1-1,y2,-1};
if(y1!=1) a[++m]=(Query){2,i,cnt,x2,y1-1,-1};
a[++m]=(Query){2,i,cnt,x2,y2,1};
}
}
std::sort(&a[1],&a[m]+1,cmp1);
cdq(1,m);
for(register int i=0;i<=cnt;i++) {
printf("%d\n",ans[i]);
}
return 0;
}

[BZOJ1176][BalkanOI2007]Mokia

改一下输入格式和数据范围,没什么区别。

[BZOJ2683]简单题/[BZOJ1176][BalkanOI2007]Mokia的更多相关文章

  1. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  2. bzoj2683简单题 cdq分治

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1803  Solved: 731[Submit][Status][Discuss] ...

  3. bzoj2683简单题

    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...

  4. BZOJ2683: 简单题(cdq分治 树状数组)

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2142  Solved: 874[Submit][Status][Discuss] Descripti ...

  5. BZOJ2683 简单题(CDQ分治)

    传送门 之前听别人说CDQ分治不难学,今天才知道果真如此.之前一直为自己想不到CDQ的方法二很不爽,今天终于是想出来了一道了,太弱-- cdq分治主要就是把整段区间分成两半,然后用左区间的值去更新右区 ...

  6. Bzoj2683 简单题 [CDQ分治]

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  7. 【对询问分块】【主席树】bzoj2683 简单题

    对操作序列分块,每S次暴力重建主席树. 当S=sqrt(n*log(n))时,复杂度为O(m*sqrt(n*log(n))). 在线的. #include<cstdio> #include ...

  8. cdq分治——bzoj2683简单题

    https://www.lydsy.com/JudgeOnline/problem.php?id=2683 知识点:1.以操作的顺序进行分治  2.cdq分治维护矩阵 3.计算比mid小的给比mid大 ...

  9. 【BZOJ1176】[Balkan2007]Mokia/【BZOJ2683】简单题 cdq分治

    [BZOJ1176][Balkan2007]Mokia Description 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=1600 ...

随机推荐

  1. 10种CSS3实现的Loading效果

    原文链接:http://www.cnblogs.com/jr1993/p/4622039.html 第一种效果: 代码如下: <div class="loading"> ...

  2. 【FCS NOI2018】福建省冬摸鱼笔记 day2

    第二天. 同学还是不带本子记笔记.dalao. 第二天:图论,讲师:@ExfJoe 全程划水,前面都讲水算法[虽然我可能已经忘记了]什么最短路,Tarjan,最小生成树,2SAT,差分约束啥的,我现在 ...

  3. Dapper实用教程

    Dapper是什么? Dpper是一款.Net平台简单(Simple)的对象映射库,并且Dapper拥有着“微型ORM之王”的称号.就速度而言与手写ADO.NET SqlDateReader相同.OR ...

  4. RobotFramework安装扩展库包Selenium2Library(三)

    Robot Framework扩展库包 http://robotframework.org/#libraries 一,自动化测试web端 1,pip安装SeleniumLibrary pip inst ...

  5. error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

    一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如: tmux: error while loading shared libraries: libevent-1.4.s ...

  6. Java WebService Axis 初探

    最近在学习WebService 开始了: 一:服务端的编写与发布 1. 工具准备: java的开发环境(这里就不多说了).   axis2官网上下载最新的就可以了(我这里用的是axis2-1.4.1- ...

  7. SHELL 中的变量

    变量的分类 系统环境变量 系统本身所有,通常为大写字母 系统变量通过 set 或 declare 指令进行查看 UDV 变量(user defined variable ) 用户创建和维护,建议大写 ...

  8. Python元组与字典详解

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup ...

  9. cuowu

    ngFor不能用于Object rowspan colspan不能绑定变量,要用attr.colspan https://stackoverflow.com/questions/35615751/wh ...

  10. 左列動態添加菜單Repeater

    前台代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="left.aspx. ...