树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019
题意:
给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少。
思路:
以后记住:二维前缀和sort+树状数组就行了!!!。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; ll c[N],ans[N];
void Init(int n)
{
for(int i=;i<=n;++i)
c[i]=ans[i]=;
}
void add(int i,ll t)
{
while(i<=N)
{
c[i]+=t;
i+=lowbit(i);
}
}
ll sum(int i)
{
ll sum=;
while(i)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
//-----------------------------------树状数组;
struct node
{
int f;
int x,y,ans_id;
ll val;
friend bool operator<(node a,node b)
{
if(a.y==b.y)
{
if(a.x==b.x)
return a.f<b.f;
return a.x<b.x;
}
return a.y<b.y;
}
}p[N];
ll re_val(ll x)
{
ll sum=;
while(x>)
{
sum+=x%;
x/=;
}
return sum;
}
long long index(long long y,long long x,long long n)
{
long long mid=(n+)/;
long long p=max(abs(x-mid),abs(y-mid));
long long ans=n*n-(+p)*p*;
long long sx=mid+p,sy=mid+p;
if(x==sx&&y==sy)
return ans;
else
{
if(y==sy||x==sx-*p)
return ans+abs(x-sx)+abs(y-sy);
else
return ans+*p-abs(x-sx)-abs(y-sy);
}
}
void solve(int n)
{
for(int i=;i<=n;++i)
{
if(p[i].f) ans[p[i].ans_id]+=sum(p[i].x)*p[i].val;
else add(p[i].x,p[i].val);
}
} int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n,m,ask;
sc("%d%d%d",&n,&m,&ask);
Init(N-);
int cnt=;
for(int i=;i<=m;++i)
{
int x,y;
sc("%d%d",&x,&y);
p[++cnt]={,x,y,-,re_val(index(x,y,n))};
}
for(int i=;i<=ask;++i)
{
int xl,yl,xr,yr;
sc("%d%d%d%d",&xl,&yl,&xr,&yr);
p[++cnt]={,xl-,yl-,i,};
p[++cnt]={,xl-,yr,i,-};
p[++cnt]={,xr,yl-,i,-};
p[++cnt]={,xr,yr,i,};
}
sort(p+,p++cnt);
solve(cnt);
for(int i=;i<=ask;++i)
pr("%lld\n",ans[i]);
}
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019的更多相关文章
- The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)
Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center val ...
- [The Preliminary Contest for ICPC Asia Nanjing 2019] A-The beautiful values of the palace(二维偏序+思维)
>传送门< 前言 这题比赛的时候觉得能做,硬是怼了一个半小时,最后还是放弃了.开始想到用二维前缀和,结果$n\leq 10^{6}$时间和空间上都爆了,没有办法.赛后看题解用树状数组,一看 ...
- F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...
- bzoj4785:[ZJOI2017]树状数组:二维线段树
分析: "如果你对树状数组比较熟悉,不难发现可怜求的是后缀和" 设数列为\(A\),那么可怜求的就是\(A_{l-1}\)到\(A_{r-1}\)的和(即\(l-1\)的后缀减\( ...
- 树状数组 二维偏序【洛谷P3431】 [POI2005]AUT-The Bus
P3431 [POI2005]AUT-The Bus Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 ...
- BZOJ4822[Cqoi2017]老C的任务——树状数组(二维数点)
题目描述 老 C 是个程序员. 最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C 轻松 地完成了系统的大部分功能,并把其中一个功能交给你来实 ...
- BZOJ1935: [Shoi2007]Tree 园丁的烦恼(树状数组 二维数点)
题意 题目链接 Sol 二维数点板子题 首先把询问拆成四个矩形 然后离散化+树状数组统计就可以了 // luogu-judger-enable-o2 #include<bits/stdc++.h ...
- bzoj 4822: [Cqoi2017]老C的任务【扫描线+树状数组+二维差分】
一个树状数组能解决的问题分要用树套树--还写错了我别是个傻子吧? 这种题还是挺多的,大概就是把每个矩形询问差分拆成四个点前缀和相加的形式(x1-1,y1-1,1)(x2.y2,1)(x1-1,y2,- ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
随机推荐
- sh_19_字符串拆分和拼接
sh_19_字符串拆分和拼接 # 假设:以下内容是从网络上抓取的 # 要求: # 1. 将字符串中的空白字符全部去掉 # 2. 再使用 " " 作为分隔符,拼接成一个整齐的字符串 ...
- vue中render: h => h(App)的详细解释
2018年06月20日 10:54:32 H-L 阅读数 5369 render: h => h(App) 是下面内容的缩写: render: function (createEleme ...
- highcharts柱状图、饼状图
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- Linux TCP自连接问题
[参考文章]:net.ipv4.ip_local_port_range 的值究竟影响了啥 [参考文章]:Linux内核参数优化 最近卸载MySQL服务偶尔会遇到MySQL端口自连接问题.导致MySQL ...
- Solr 5.2.1 部署并索引Mysql数据库
1.Solr简介 Solr是一个高性能,采用Java5开发,SolrSolr基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查 ...
- 基本CSS布局三
基本CSS布局三------图片视频网格 <!DOCTYPE html> <html> <head> <meta charset="utf-8&qu ...
- LC 932. Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- openerp学习笔记 视图中字段只变化(on_change)联动其他字段值、选择和过滤
1.修改产品数量时,自动计算产品销售金额.销售成本和销售利润<field name="num" on_change="on_change_product(produ ...
- APT软件包管理-在线安装
APT (Advanced Packaging Tool高级软件包工具) 是一个强大的包管理系统,而那些图形化程序如 添加/删除 应用程序 都是建立 在它的基础之上的.有了dpkg后,Debian再次 ...
- 基于Bootstrap 3可预览的HTML5文件上传插件
前端常用资源地址: http://www.htmleaf.com/ http://www.htmleaf.com/html5/html5muban/201505091801.html 源代码地址 ht ...