2019ICPC南京网络赛A题

The beautiful values of the palace

https://nanti.jisuanke.com/t/41298

Here is a square matrix of n * nnn, each lattice has its value (nn must be odd), and the center value is n * nnn. Its spiral decline along the center of the square matrix (the way of spiral decline is shown in the following figure:)

The grid in the lower left corner is (1,1) and the grid in the upper right corner is (n , n)

Now I can choose mm squares to build palaces, The beauty of each palace is equal to the digital sum of the value of the land which it is located. Such as (the land value is 123213123213,the beautiful values of the palace located on it is 1+2+3+2+1+3=121+2+3+2+1+3=12) (666666 -> 1818) (456456 ->1515)

Next, we ask pp times to the sum of the beautiful values of the palace in the matrix where the lower left grid(x_1,y_1x1,y1), the upper right square (x_2,y_2x2,y2).

Input

The first line has only one number TT.Representing TT-group of test data (T\le 5)(T≤5)

The next line is three number: n \ m \ pn m p

The mm lines follow, each line contains two integers the square of the palace (x, y )(x,y)

The pp lines follow, each line contains four integers : the lower left grid (x_1,y_1)(x1,y1) the upper right square (x_2,y_2)(x2,y2)

Output

Next, p_1+p_2...+p_Tp1+p2...+p**T lines: Represent the answer in turn(n \le 10^6)(m , p \le 10^5)(n≤106)(m,p≤105)

样例输入复制

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

样例输出复制

5
18
23
17

思路:

三维偏序的题目

首先根据推公式可以把每一个点在螺旋矩阵中对应的数值求出。

然后我们把m个点当做成m个加点操作,

p个询问,每一个询问分解为4个子询问,对同一个答案计算贡献。

因为根据容斥原理,我们可以把求二维前缀和分解为4个以左下角点为(0,0)的4个前缀和来处理。,

然后对x,y进行排序,

坐标相同时,一定要加点的操作排在询问前面。

然后用树桩数组来维护偏序问题即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ ll tree[maxn];
int lowbit(int x)
{
return -x & x;
}
ll ask(int x)
{
ll res = 0ll;
while (x) {
res += tree[x];
x -= lowbit(x);
}
return res;
}
void add(int x, ll val)
{
while (x < maxn) {
tree[x] += val;
x += lowbit(x);
}
}
ll re_val(ll x)
{
ll sum = 0;
while (x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
long long index(long long y, long long x, long long n)
{
long long mid = (n + 1) / 2;
long long p = max(abs(x - mid), abs(y - mid));
long long ans = n * n - (1 + p) * p * 4;
long long sx = mid + p, sy = mid + p;
if (x == sx && y == sy) {
return ans;
} else {
if (y == sy || x == sx - 2 * p) {
return ans + abs(x - sx) + abs(y - sy);
} else {
return ans + 8 * p - abs(x - sx) - abs(y - sy);
}
}
}
int tot;
struct node {
int type;
int id;
ll k;
ll x, y;
ll val;
node() {}
node(int tt, int idd, ll kk, ll xx, ll yy, ll vv)
{
id = idd;
type = tt;
k = kk;
x = xx;
y = yy;
val = vv;
}
} a[maxn];
bool cmp(node aa, node bb)
{
if (aa.y != bb.y) {
return aa.y < bb.y;
} else if (aa.x != bb.x) {
return aa.x < bb.x;
} else {
return aa.type < bb.type;
}
}
ll ans[maxn];
void solve()
{
repd(i, 1, tot) {
if (a[i].type) {
ans[a[i].id] += a[i].k * ask(a[i].x);
} else {
add(a[i].x, a[i].val);
}
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int t;
du1(t);
while (t--) {
int n, m, p;
du3(n, m, p);
MS0(tree);
tot = 0;
repd(i, 1, m) {
int x, y;
du2(x, y);
ll val = re_val(index(x, y, n));
a[++tot] = node(0, 0, 1ll, x, y , val);
}
repd(i, 1, p) {
ans[i] = 0ll;
int lx, ly, rx, ry;
du3(lx, ly, rx); du1(ry);
a[++tot] = node(1, i, 1ll, rx, ry , 0);
a[++tot] = node(1, i, 1ll, lx - 1, ly - 1 , 0);
a[++tot] = node(1, i, -1ll, rx, ly - 1 , 0);
a[++tot] = node(1, i, -1ll, lx - 1, ry , 0);
}
sort(a + 1, a + 1 + tot, cmp);
solve();
repd(i, 1, p) {
printf("%lld\n", ans[i] );
}
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)的更多相关文章

  1. HDU 4758 Walk Through Squares (2013南京网络赛1011题,AC自动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  2. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. HDU 4750 Count The Pairs (2013南京网络赛1003题,并查集)

    Count The Pairs Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others ...

  4. 2019ICPC南京网络赛A The beautiful values of the palace

    题意:蛇形填数超大版本,需要求出一些给定坐标的值的数位和,然后q次询问,一个矩形区域内值的和是多少 解题思路:二维偏序前缀和的经典题 二维偏序:求(x,y)左下角点的个数,思路是对x,y升序排序,用树 ...

  5. 2019icpc南京网络赛 A The beautiful values of the palace(离线+树状数组)

    题意: (假设所有的点对应的值已经求出)给你一个1e6*1e6的矩阵,有m<=1e5个点有值,其余都为0 q<=1e5个询问,求子矩阵的权值和 思路: 根据二维差分,对于询问左下角(x1, ...

  6. The writing on the wall 南京网络赛2018B题

    样例输入复制 2 3 3 0 3 3 1 2 2 样例输出复制 Case #1: 36 Case #2: 20 题目来源 ACM-ICPC 2018 南京赛区网络预赛 题意: 就是求图中去掉涂黑的方格 ...

  7. 2018南京网络赛L题:Magical Girl Haze(最短路分层图)

    题目链接:https://nanti.jisuanke.com/t/31001 解题心得: 一个BZOJ的原题,之前就写过博客了. 原题地址:https://www.lydsy.com/JudgeOn ...

  8. 2018 ACM南京网络赛H题Set解题报告

    题目描述 给定\(n\)个数$a_i$,起初第\(i\)个数在第\(i\)个集合.有三种操作(共\(m\)次): 1 $u$ $v$ 将第$u$个数和第$v$个数所在集合合并 2 $u$ 将第$u$个 ...

  9. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...

随机推荐

  1. vue是一个渐进式的框架,我是这么理解的

    vue是一个渐进式的框架,我是这么理解的 原文地址 时间:2017-10-26 10:37来源:未知 作者:admin 每个框架都不可避免会有自己的一些特点,从而会对使用者有一定的要求,这些要求就是主 ...

  2. 配置cinder-volume服务使用ceph作为后端存储

    在ceph监视器上执行 CINDER_PASSWD='cinder1234!'controllerHost='controller'RABBIT_PASSWD='0penstackRMQ' 1.创建p ...

  3. 一个提高照片质量的网站和一个改变照片DPI的方法

    相信很多童鞋都有遇到过,碰到一张很喜欢的图片,想用来做背景什么的,蛋似--因为画质太AV了怕引起误会,所以不敢使用!很气--!! 这时大神就会跳出来说,画质不好?PS是用来吃si的么! 我:我才不会用 ...

  4. NDK学习笔记-JNI数据类型和属性方法的访问

    JNI实现了C/C++与Java的相互访问,那么这篇文章就从C/C++访问Java开始说起 native函数说明 每个native函数,都至少有两个参数(JNIEnv *和jclass或jobject ...

  5. 19.通过MAPREDUCE 把收集数据进行清洗

    在eclipse软件里创建一个maven项目 jdk要换成本地安装的1.8版本的 加载pom.xml文件 <project xmlns="http://maven.apache.org ...

  6. ######【Python】【基础知识】Python的介绍 ######

    Python 是一种面向对象.解释型计算机程序设计语言. Python是什么? Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言 ...

  7. hanlp添加自定义字典的步骤介绍

      本篇分享一个hanlp添加自定义字典的方法,供大家参考! 总共分为两步: 第一步:将自定义的字典放到custom目录下,然后删除CustomDicionary.txt.bin,因为分词的时候会读这 ...

  8. Hadoop集群搭建-04安装配置HDFS

    Hadoop集群搭建-05安装配置YARN Hadoop集群搭建-04安装配置HDFS  Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hado ...

  9. 数位dp详解&&LG P2602 [ZJOI2010]数字计数

    数位dp,适用于解决一类求x~y之间有多少个符合要求的数或者其他. 例题 题目描述 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除 ...

  10. 12306火车票余票查询&Python实现邮件发送

    查询余票接口 打开12306官网,并进入余票查询页面,同时开启chrome浏览器F12控制台,以北京到上海为例,搜索2018年10月1日的余票信息,点击搜索按钮,可以在控制台发送了一条GET请求,请求 ...