[CQOI2017] 老C的任务(差分 + 树状数组 / K-D tree)

problem

luogu-P3755

solution

这题第一眼矩阵内的点权值和,马上就是 K-D tree\text{K-D tree}K-D tree 不过脑子的敲。

这其实就是个二维数点问题,完全可以树状数组。

将矩阵差分成四个以原点为左下角的矩阵。

然后将基站按 xxx 轴排序,矩阵按右上角的 (x,y)(x,y)(x,y)xxx 排序。

树状数组维护 yyy 轴信息即可。

code-树状数组

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 1000005
int n, m, cnt, tot;
struct point { int x, y, w; }p[maxn];
struct node { int x, y, k, id; }g[maxn];
int val[maxn], ret[maxn];void read( int &x ) {x = 0; int f = 1; char s = getchar();while( s < '0' or s > '9' ) {if( s == '-' ) f = -1;s = getchar();}while( '0' <= s and s <= '9' ) {x = ( x << 1 ) + ( x << 3 ) + ( s ^ 48 );s = getchar();}x *= f;
}void print( int x ) {if( x < 0 ) putchar('-'), x = -x;if( x > 9 ) print( x / 10 );putchar( x % 10 + '0' );
}namespace BIT {int t[maxn];void add( int x, int y ) {for( ;x <= cnt;x += x & -x ) t[x] += y;}	int ask( int x ) {int ans = 0;for( ;x;x -= x & -x ) ans += t[x];return ans;}
}signed main() {read( n ), read( m );for( int i = 1;i <= n;i ++ ) {read( p[i].x ), read( p[i].y ), read( p[i].w );val[++ cnt] = p[i].y;}for( int i = 1, xi, yi, xj, yj, w;i <= m;i ++ ) {read( xi ), read( yi ), read( xj ), read( yj );g[++ tot] = (node){ xj, yj, 1, i };g[++ tot] = (node){ xi - 1, yj, -1, i };g[++ tot] = (node){ xj, yi - 1, -1, i };g[++ tot] = (node){ xi - 1, yi - 1, 1, i };val[++ cnt] = yj;val[++ cnt] = yi - 1;}sort( val + 1, val + cnt + 1 );cnt = unique( val + 1, val + cnt + 1 ) - val - 1;for( int i = 1;i <= n;i ++ ) p[i].y = lower_bound( val + 1, val + cnt + 1, p[i].y ) - val;for( int i = 1;i <= tot;i ++ )g[i].y = lower_bound( val + 1, val + cnt + 1, g[i].y ) - val;sort( p + 1, p + n + 1, []( point a, point b ) { return a.x < b.x; } );sort( g + 1, g + tot + 1, []( node a, node b ) { return a.x < b.x; } );for( int i = 1, j = 1;i <= tot;i ++ ) {for( ;j <= n and p[j].x <= g[i].x;j ++ ) BIT :: add( p[j].y, p[j].w );ret[g[i].id] += BIT :: ask( g[i].y ) * g[i].k;}for( int i = 1;i <= m;i ++ ) print( ret[i] ), putchar('\n');return 0;
}

code-KDtree

#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define int long long
struct point { int x, y, p; }g[maxn];
struct node { point Max, Min, id; int lson, rson, sum; }t[maxn];
int n, m, dim, X1, Y1, X2, Y2, ans;void read( int &x ) {x = 0; int f = 1; char s = getchar();while( s < '0' or s > '9' ) {if( s == '-' ) f = -1;s = getchar();}while( '0' <= s and s <= '9' ) {x = ( x << 1 ) + ( x << 3 ) + ( s ^ 48 );s = getchar();}x *= f;
}void print( int x ) {if( x < 0 ) putchar( '-' ), x = -x;if( x > 9 ) print( x / 10 );putchar( x % 10 + '0' );
}bool cmp( point a, point b ) {if( dim == 0 ) return a.x < b.x;if( dim == 1 ) return a.y < b.y; 
}void chkmin( point &a, point b ) {a.x = min( a.x, b.x );a.y = min( a.y, b.y );
}void chkmax( point &a, point b ) {a.x = max( a.x, b.x );a.y = max( a.y, b.y );
}void pushup( int now ) {t[now].sum = t[now].id.p;if( t[now].lson ) {chkmin( t[now].Min, t[t[now].lson].Min );chkmax( t[now].Max, t[t[now].lson].Max );t[now].sum += t[t[now].lson].sum;}if( t[now].rson ) {chkmin( t[now].Min, t[t[now].rson].Min );chkmax( t[now].Max, t[t[now].rson].Max );t[now].sum += t[t[now].rson].sum;}
}int build( int l, int r, int d ) {if( l > r ) return 0;dim = d; int mid = l + r >> 1;nth_element( g + l, g + mid, g + r + 1, cmp );t[mid].Max = t[mid].Min = t[mid].id = g[mid];t[mid].lson = build( l, mid - 1, d ^ 1 );t[mid].rson = build( mid + 1, r, d ^ 1 );pushup( mid );return mid;
}void query( int now ) {if( ! now ) return;if( t[now].Max.x < X1 or t[now].Min.x > X2 or t[now].Max.y < Y1 or t[now].Min.y > Y2 ) return;if( X1 <= t[now].Min.x and t[now].Max.x <= X2 andY1 <= t[now].Min.y and t[now].Max.y <= Y2 ) {ans += t[now].sum; return;}if( X1 <= t[now].id.x and t[now].id.x <= X2 andY1 <= t[now].id.y and t[now].id.y <= Y2 ) ans += t[now].id.p;query( t[now].lson );query( t[now].rson );
}signed main() {read( n ), read( m );for( int i = 1;i <= n;i ++ ) read( g[i].x ), read( g[i].y ), read( g[i].p );int root = build( 1, n, 0 );for( int i = 1;i <= m;i ++ ) {read( X1 ), read( Y1 ), read( X2 ), read( Y2 );ans = 0, query( root );print( ans );putchar( '\n' );}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/316347.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.net core 并发下的线程安全问题

抱歉&#xff0c;其实内容并不如题&#xff01;&#xff01;&#xff01;背景&#xff08;写测试demo所出现的异常&#xff0c;供大家学习与拍砖&#xff09;&#xff1a;.net core webapi项目&#xff0c;做了一个授权的filter&#xff08;真正的生产项目的话&#xff0c;JWT很…

cf1555B. Two Tables

cf1555B. Two Tables 题意&#xff1a; 一个大矩阵空间内放置一个矩阵a&#xff0c;现在要再往这个空间内放一个矩阵b&#xff0c;a移动距离len才能放下b&#xff0c;问len最小是多少 题解&#xff1a; 不难发现左右或上下移动是最佳的&#xff0c;斜着移动是最不好的。此时…

cf1555C Coin Rows

cf1555C Coin Rows 题意&#xff1a; 有一个两行m列的地图&#xff0c;每个格子都有对应的价值&#xff0c;有a&#xff0c;b两个人&#xff0c;都从左上角到右下角&#xff0c;且都只能向右向下走&#xff0c;a先出发&#xff0c;a每到一个格子&#xff0c;就会获得这个地方…

C#并行编程(2):.NET线程池

线程 Thread在总结线程池之前&#xff0c;先来看一下.NET线程。.NET线程与操作系统(Windows)线程有什么区别&#xff1f;.NET利用Windows的线程处理功能。在C#程序编写中&#xff0c;我们首先会新建一个线程对象System.Threading.Thread&#xff0c;并为其指定一个回调方法&…

[CQOI2015] 任务查询系统(主席树)

problem luogu-P3168 solution 主席树板题。 将一个任务拆成 lil_ili​ 秒开始&#xff0c;ri1r_i1ri​1 秒结束的两个任务。 但不建议以每一秒作为一个主席树版本&#xff0c;因为一秒中可能有若干个任务开始或结束。 不妨将所有任务按时刻排序&#xff0c;然后以每个任…

ASP.NET Core launchsettings.json文件(8)《从零开始学ASP.NET CORE MVC》:

本文出自《从零开始学ASP.NET CORE MVC》推荐文章&#xff1a;ASP.NET Core 进程外(out-of-process)托管ASP.NET Core launchsettings.json文件在本视频中&#xff0c;我们将讨论在ASP.NET Core项目中launchsettings.json文件的重要性。launchsettings.json文件您将在项目根文件…

[CQOI2017] 老C的方块(网络流染色建图)

problem luogu-P3756 solution 据说要做网络流 24\text{24}24 题中的《方格取数问题》和《骑士共存问题》。 &#xff1f;&#xff1f;&#xff1f;那个不是直接最小割吗&#xff1f;哦原来是从黑白染色来理解的。我还是太水了。 这种题之所以能用网络流做&#xff0c;是因…

cf1555D. Say No to Palindromes

cf1555D. Say No to Palindromes 题意&#xff1a; 给出一个字符串&#xff0c;长度为n&#xff0c;而且都是a,b,c三个字符构成的&#xff0c;然后有m个询问 每个询问给出l r&#xff0c;问要想这个区间内任意长度字串都不是回文子串&#xff0c;至少要改多少个字符 题解&am…

江湖召集:.NET开发者们看过来,这场长沙的开发者技术大会正是为你精心准备的大餐...

看过去&#xff0c;历史的尘埃与沧海桑田古语有云“近代中国&#xff0c;湖南独撑半边天”&#xff0c;湖南长沙&#xff0c;作为湖南省的省会&#xff0c;自古以来便是各界风云人士兴起之地。随着互联网时代的到来&#xff0c;长沙&#xff0c;这座历史悠久的文化名城&#xf…

[HEOI2012] 朋友圈(最大团 + 结论 + 二分图 + 网络流)

problem luogu-P2423 solution 本题即求无向图最大团问题。这是个 NP hard\text{NP hard}NP hard 问题&#xff0c;所以必须从图的特殊性质出发&#xff0c;否则只能暴搜。 异或运算等价于二进制下不进位的加法运算。 observation1:\text{observation1}:observation1: AAA …

cf1555 E. Boring Segments

cf1555 E. Boring Segments 题意&#xff1a; 给你n个线段&#xff0c;最大点是m&#xff0c;每一个线段有一个权值w&#xff0c;你能选择线段来覆盖1-m这个区间的&#xff0c;选择的代价为最大权值和最小权值的差。问你最小的的代价是多少。 题解&#xff1a; 尺取线段树 …

C#并行编程(3):并行循环

初识并行循环并行循环主要用来处理数据并行的&#xff0c;如&#xff0c;同时对数组或列表中的多个数据执行相同的操作。在C#编程中&#xff0c;我们使用并行类System.Threading.Tasks.Parallel提供的静态方法Parallel.For和Parallel.ForEach来实现并行循环。从方法名可以看出&…

[NOI Online 2022 提高组] 丹钓战(单调栈 + 树状数组 / 主席树)

problem luogu-P8251 solution 按照题意模拟单调栈。 求出对于 iii 而言&#xff0c;当时单调栈的栈顶元素记为 pip_ipi​。 如果到 iii 时&#xff0c;栈顶已经为 pip_ipi​ 了&#xff0c;意味着这中间的所有元素要么是被 iii 弹出&#xff0c;要么就是被 iii 前面的某些…

Acwing 252. 树

Acwing 252. 树 题意&#xff1a; 给定一个有 N 个点&#xff08;编号 0,1,…,N−1&#xff09;的树&#xff0c;每条边都有一个权值&#xff08;不超过 1000&#xff09;。 树上两个节点 x 与 y 之间的路径长度就是路径上各条边的权值之和。 求长度不超过 K 的路径有多少条…

.net 4.5部署到docker容器

.NET FX 应用程序也是可以容器化的&#xff0c;容器化的选项有两个&#xff1a;部署到windows容器部署到linux容器部署到windows容器由于.net本身就是运行在windows平台的&#xff0c;所以它与windows容器也是更加适合&#xff0c;你可以以iis镜像为基础&#xff0c;去编写你的…

[NOI Online 2022 提高组] 讨论(巧妙的切入方式)

problem luogu-P8252 solution 本题最难处理的就是两个人会做的题目集合是包含关系的限制。 将所有人按会做的题数从大到小排序。 然后枚举 iii&#xff0c;只要这个人和之前某个人存在有至少一道公共的题目&#xff0c;并且保证这个人有新的题目&#xff0c;那么这两个人…

P4149 [IOI2011]Race

P4149 [IOI2011]Race 题意&#xff1a; 给一棵树&#xff0c;每条边有权。求一条简单路径&#xff0c;权值和等于 k&#xff0c;且边的数量最小。 题解&#xff1a; 用t[i]:长度为i的路径包含的最少边数 按照子树顺序&#xff0c;依次用dep[u]t[K-d[u]]更新ans&#xff0c;…

将传统 WPF 程序迁移到 DotNetCore 3.0

介绍由于历史原因&#xff0c;基于 Windows 平台存在着大量的基于 .NetFramework 开发的 WPF 和 WinForm 相关程序&#xff0c;如果将这些程序全部基于 DotNetCore 3.0 重写一遍显然是不现实的&#xff0c;但是 DotNetCore 是未来发展的趋势。所以本文通过以 WPF 为例&#xff…

[CodeForces 1603C] Extreme Extension(贪心 + 数论分块优化dp)

problem CodeForces solution observation1:\text{observation1}:observation1: 对于一个非空子段 [l,r][l,r][l,r]&#xff0c;最后一个元素 ara_rar​ 一定不会被操作。 observation2:\text{observation2}:observation2: 基于上一条进一步地有&#xff0c;对于一个非空子段…

.NET Core 时代已经到了,你准备好了吗

今天很多人都收到了阿里云函数计算支持.NET Core的短信了。通过访问 https://help.aliyun.com/document_detail/112379.html 你可以看到最新的说明。现在和过去的两年不同&#xff0c;因为最恶劣的时期已经过去&#xff0c;经历过了最黑暗的时刻&#xff0c;我们正在走向光明的…