刷题总结——mokia(bzoj1176)

题目:

维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.

Input

第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小

接下来每行为一下三种输入之一(不包含引号):

“1 x y a”

“2 x1 y1 x2 y2”

“3”

输入1:你需要把(x,y)(第x行第y列)的格子权值增加a

输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出

输入3:表示输入结束

Output

对于每个输入2,输出一行,即输入2的答案

Sample Input

0 4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3

Sample Output

3
5

题解:

又一道用cdq来解决三维偏序的题,三维分别是时间,x,y;

首先肯定要拆掉询问····我们用sum(x,y)来表示包括该点的左上角的矩形值之和,然后针对一个询问x1,y1,x2,y2,答案明显是sum(x1-1,y1-1)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x2,y2),因此可以将询问拆成对应x,y的四个点,然后就可以先以默认的时间为第一顺序,每次cdq分治时两边以x为第二顺序,最后用树状数组维护y的第三顺序,计算左边部分的修改对右边部分询问的贡献从而解决问题

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=2e5+5;
const int M=2e6+5;
struct node
{
  int type,x,y,val,pos,id;
}query[N],temp[N];
inline int R()
{
  char c;int f=0,i=1;
  for(c=getchar();(c<‘0‘||c>‘9‘)&&c!=‘-‘;c=getchar());
  if(c==‘-‘)  i=-1,c=getchar();
  for(;c<=‘9‘&&c>=‘0‘;c=getchar())
    f=(f<<3)+(f<<1)+c-‘0‘;
  return f*i;
}
int w,s,tree[M],n,m,X1,Y1,X2,Y2,ans[10005],tim,tag[M];
bool cmp(node a,node b)
{
  if(b.x==a.x)  return a.id<b.id;
  else return a.x<b.x;
}
inline void insert(int u,int v)
{
  for(int i=u;i<=w;i+=(i&(-i)))
    if(tag[i]!=tim)  tag[i]=tim,tree[i]=v;
    else tree[i]+=v;
}
inline int ask(int u)
{
  int temp=0;
  for(int i=u;i;i-=(i&(-i)))
    if(tag[i]!=tim)  continue;
    else temp+=tree[i];
  return temp;
}
inline void solve(int l,int r)
{
  if(l==r)  return;
  int mid=(l+r)/2;
  solve(l,mid),solve(mid+1,r);
  int i=l,j=mid+1,k=l;tim++;
  while(i<=mid&&j<=r)
  {
    if(cmp(query[i],query[j]))
    {
      if(!query[i].pos)
        insert(query[i].y,query[i].val);
      temp[k++]=query[i++];
    }
    else
    {
      if(query[j].pos)
        ans[query[j].pos]+=ask(query[j].y)*query[j].val;
      temp[k++]=query[j++];
    }
  }
  while(i<=mid)  temp[k++]=query[i++];
  while(j<=r)
  {
    if(query[j].pos)
      ans[query[j].pos]+=ask(query[j].y)*query[j].val;
    temp[k++]=query[j++];
  }
  for(i=l;i<=r;i++)  query[i]=temp[i];
}
int main()
{
  //freopen("a.in","r",stdin);
  s=R(),w=R();
  while(true)
  {
    query[++n].type=R();
    if(query[n].type==3)  break;
    if(query[n].type==1)
    {
      query[n].x=R(),query[n].y=R(),query[n].val=R(),query[n].id=n;
    }
    else
    {
      int X1=R()-1,Y1=R()-1,X2=R(),Y2=R();m++;
      query[n].x=X1,query[n].y=Y1,query[n].val=1,query[n].pos=m,query[n].id=n;
      query[++n].x=X1,query[n].y=Y2,query[n].val=-1,query[n].pos=m,query[n].id=n;
      query[++n].x=X2,query[n].y=Y1,query[n].val=-1,query[n].pos=m,query[n].id=n;
      query[++n].x=X2,query[n].y=Y2,query[n].val=1,query[n].pos=m,query[n].id=n;
      ans[m]=(X2-X1)*(Y2-Y1)*s;
    }
  }
  solve(1,n);
  for(int i=1;i<=m;i++)
    printf("%d\n",ans[i]);
  return 0;
}
时间: 2024-08-05 07:08:45

刷题总结——mokia(bzoj1176)的相关文章

用js刷题的一些坑

leecode可以用js刷题了,我大js越来越被认可了是吧.但是刷题中会因为忽略js的一些特性掉入坑里.我这里总结一下我掉过的坑. 坑1:js中数组对象是引用对象 js中除了object还有数组对象也是引用对象,这点常常被忽视,所以在递归的时候传递数组要用arr.slice(0)这样复制一个一样的新数组,不然会出现你传入的数组会被同级的递归改变,结果就不对了. 所以只要数组复制的地方最好都要这么写,除非你真的想引用.而且注意是slice不是splice这两个方法差别很大,你如果用splice(0

LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来. 题目为: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it withou

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

刷题记录

刷题啦,刷题啦,咱也刷算法题. 先从牛客网的JS方面刷起,接着刷数据结构和算法,然后去刷leetcode,这儿记载自己从出错的地方. 1.题目描述 移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 . 没有认真思考,写下了如下的答案 function removeWithoutCopy(arr, item) { for(i = 0; i < arr.length; i++) { if( arr[i] === item) { arr.spli

停课刷题总结-给自己一点鼓励吧

嗯,我已经停了四五天课在家刷BZOJ准备复赛了,感觉压力好大.但是,实际上感觉效率并不高,每天就是7-8题的样子,而且并不是每题都有质量.而且这几天刷下来,我貌似因为刷了太多水题的关系,打字写题的速度变慢了,有一点悠闲没有紧迫感了,要赶快把这个习惯给改掉!今天去学校做题被虐了,竟然一个简单的Hash没有调对[虽然我现在还是不知道为什么会死循环QAQ.]感觉吧,可能因为刷题有点不在状态了.[其实也因为刷题的间隙玩了几盘LOL,游戏这东西QAQ]被虐了,感觉很不爽,有点难受,毕竟我付出了那么多努力,

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

半个暑假的刷题有感

这半个多月一来,主要是在刷DP.开始是一些简单的DP(可是我没有感觉有多简单=_=!!),然后是最大连续子序列,最大公共子序列,最大子矩阵等等,这些题目还好,有的题目甚至可以模板化. 还有一些没有解决的难题: 1024 Max Sum Plus Plus 最大m段不重叠子段和](可不连续)1244 Max Sum Plus Plus Plus [最大m段不重叠子段和](连续) 1074 Doing Homework [压缩dp](这个题整个程序都在用位运算,让我这个小白情何以堪啊) 还有记忆化搜