做过,但当时咕了 T3

Merchant

先特判 \(t=0\),之后斜率一定会起作用。

考虑最终选择的物品集合,它们的斜率和一定大于 \(0\),因此答案具有单调性,可以二分。

实现的时候注意细节

const int N = 1e6+5;
int n,m;
LL s,k[N],b[N]; LL y[N]; bool check(int x) {
For(i,1,n) y[i] = k[i] * x + b[i];
nth_element(y+1,y+m,y+n+1,greater<LL>()); // O(n)找前m大
LL sum = 0;
For(i,1,m) {
if( y[i] >= 0 ) sum += y[i]; // 可能选不满m个
if( sum >= s ) return 1; // 如果不在这里判会爆LL
}
return 0;
} signed main() {
freopen("merchant.in","r",stdin);
freopen("merchant.out","w",stdout);
read(n,m,s);
For(i,1,n) read(k[i],b[i]);
if( check(0) ) { puts("0"); return 0; }
int l = 1, r = 1e9;
while( l < r ) {
int mid = l+r>>1;
if( check(mid) ) r = mid;
else l = mid+1;
}
write(l);
return iocl();
}

Equation

乍一看不好做,但题目只求 \(x_1\) 的值,可以把每个方程改写成 \(x_i=k_ix_1+b_i\) 的形式,修改时用 BIT 维护 \(b\) 即可。注意分深度的奇偶来判断正负。

const int N = 1e6+5;
int n,q,fa[N],val[N]; int ind,dfn[N],siz[N],k[N];
vector<int> to[N]; struct BIT {
LL t[N];
void add(int i,LL x) { for(;i<=n;i+=i&-i)t[i]+=x; }
void add(int l,int r,LL x) { add(l,x), add(r+1,-x); }
LL operator [] (int i) { LL res=0; for(;i;i-=i&-i)res+=t[i]; return res; }
} b; void none() { putchar('n'),putchar('o'),putchar('n'),putchar('e'),putchar(10); }
void inf() { putchar('i'),putchar('n'),putchar('f'),putchar(10); } void dfs(int u) {
dfn[u] = ++ind, siz[u] = 1;
for(int v : to[u]) k[v] = -k[u], dfs(v), siz[u] += siz[v];
b.add(dfn[u],dfn[u]+siz[u]-1,k[u]*val[u]);
} signed main() {
freopen("equation.in","r",stdin);
freopen("equation.out","w",stdout);
read(n,q);
For(i,2,n) {
read(fa[i]), read(val[i]);
to[fa[i]].pb(i);
}
k[1] = 1, dfs(1);
while( q-- ) {
int op; read(op);
if( op == 1 ) {
int u,v,s; read(u,v,s);
int kk = k[u]+k[v]; LL bb = s-k[u]*b[dfn[u]]-k[v]*b[dfn[v]];
if( !kk ) !bb ? inf() : none();
else if( bb % kk ) none();
else !bb ? none() : write(bb/kk);
} else {
int u,w; read(u,w);
b.add(dfn[u],dfn[u]+siz[u]-1,k[u]*(w-val[u])), val[u] = w;
}
}
return iocl();
}

rectangle

发现 \(n\) 很大而值域很小,考虑直接在值域上找矩形。

枚举列来确定矩形左右边界,根据左右边界上的点的纵坐标来确定合法的上下边界。

具体做法

时间复杂度 \(O(2500n\log2500)\)

const int N = 1e4+5, X = 2500, mod = 1e9+7;
int n,cnt[N],mp[X+5][X+5]; bool vis[X+5][X+5];
int ans; struct BIT {
int t[X+5];
void add(int i,int x) { for(;i<=X;i+=i&-i)t[i]+=x; }
int query(int l,int r) {
int res=0; for(--l;r>l;r-=r&-r)res+=t[r];
for(;l>r;l-=l&-l)res-=t[l]; return res; }
} siz[X+5],sum[X+5]; signed main() {
freopen("rectangle.in","r",stdin);
freopen("rectangle.out","w",stdout);
read(n);
For(i,1,n) {
int x; read(x);
read(mp[x][++cnt[x]]);
}
For(i,1,X) {
sort(mp[i]+1,mp[i]+cnt[i]+1);
mp[i][cnt[i]+1] = X+1;
}
For(r,1,X) if( cnt[r] ) {
For(i,1,cnt[r]) if( !vis[r][mp[r][i]] ) vis[r][mp[r][i]] = 1,
siz[r].add(mp[r][i],1), sum[r].add(mp[r][i],mp[r][i]);
rFor(l,r-1,1) if( cnt[l] ) {
For(i,1,cnt[l]) if( !vis[r][mp[l][i]] ) vis[r][mp[l][i]] = 1,
siz[r].add(mp[l][i],1), sum[r].add(mp[l][i],mp[l][i]);
int i = 1, j = 1, now = max(mp[l][1],mp[r][1]);
while( mp[l][i+1] <= now ) ++i;
while( mp[r][j+1] <= now ) ++j;
while( i <= cnt[l] && j <= cnt[r] ) {
int up = min(mp[l][i+1],mp[r][j+1]), down = min(mp[l][i],mp[r][j]);
ans += (r-l) *
(((LL)sum[r].query(now,up-1) * siz[r].query(1,down) -
(LL)siz[r].query(now,up-1) * sum[r].query(1,down)) %mod) %mod;
if( ans >= mod ) ans -= mod;
now = up;
if( mp[l][i+1] <= now ) ++i;
if( mp[r][j+1] <= now ) ++j;
}
}
}
write(ans);
return iocl();
}

20210809 Merchant,Equation,Rectangle的更多相关文章

  1. csp-s模拟测试56Merchant, Equation,Rectangle题解

    题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...

  2. csp-s模拟测试56(10.2)Merchant「二分」·Equation「树状数组」

    又死了......T1 Merchant 因为每个集合都可以写成一次函数的形式,所以假设是单调升的函数,那么随着t越大就越佳 而单调减的函数,随着t的增大结果越小,所以不是单调的??? 但是我们的单调 ...

  3. 2D Rotated Rectangle Collision

    Introduction While working on a project for school, I found it necessary to perform a collision chec ...

  4. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  5. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. Python学习的十个阶段,学完神功大成,对应一下看看你自己在哪个阶段

    大家好,我是白云. 今天给大家整理了Python学习的十个阶段内容,看看你现在正处于哪个阶段,想学习的朋友也可以根据这个阶段规划学习. 阶段一:Python基础[ 初入江湖] Linux基础 目标: ...

  2. [WesternCTF2018]shrine(SSTI+过滤)

    记一道存在过滤的模板注入的题.直接给源代码 import flask import os app = flask.Flask(__name__) app.config['FLAG'] = os.env ...

  3. 谈谈网络协议 - 数据链路层( Data Link)

    数据链路层( Data Link) 链路:从1个节点到相邻节点的一段物理线路(有线或无线),中间没有其他交换节点 案例 上图可以看出,总共由5条链路组成: 第1条:计算机0 => 路由器0,使用 ...

  4. 不懂Ribbon原理的可以进来看看哦,分析SpringBoot自动装配完成了Ribbon哪些核心操作

      前面详细的给大家介绍了SpringBoot的核心内容,有了这部分的基础支持的话,我们再来分析SpringCloud中的相关组件就很容器了,本文我们来给大家开始介绍Ribbon的相关内容,首先来介绍 ...

  5. Java横向技术 网络【笔记】

    Java横向技术 网络[笔记] 计算机网络 服务器返回给客户端 http 响应包的状态码有哪几大类?302.304 分别是什么意思? ​ 状态码分为五大类: ​(1)信息性状态码(Informatio ...

  6. 解决docker-compose下载过慢

    https://blog.csdn.net/baidu_21349635/article/details/104628772

  7. Windows注册表内容详解(转载)

    (关于windows注册表的整理,来源网络) 前提 一.什么是注册表 ​ 注册表是windows操作系统.硬件设备以及客户应用程序得以正常运行和保存设置的核心"数据库",也可以说是 ...

  8. 一种简易但设计全面的ID生成器思考

    分布式系统中,全局唯一 ID 的生成是一个老生常谈但是非常重要的话题.随着技术的不断成熟,大家的分布式全局唯一 ID 设计与生成方案趋向于趋势递增的 ID,这篇文章将结合我们系统中的 ID 针对实际业 ...

  9. 做iOS自动化测试必须知道的一些知识

    WDA facebook wda 2015年Facebook 开源了一款 iOS 移动测试框架WebDriverAgent,WebDriverAgent 在 iOS 端实现了一个 WebDriver ...

  10. RabbitMQ和Elasticsearch的使用笔记

    Demo介绍 学习rabbitmq和elasticsearch后的小练习,主要功能点介绍: 1.elasticsearch实现搜索.条件查询和分页: 2.搜索周边酒店信息 3.酒店竞价排名: 4.后台 ...