做过,但当时咕了 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. SAS 常用字符串函数

    原文链接:https://www.cnblogs.com/snoopy1866/p/15085466.html CAT(item-1 <, -, item-n>) : 在保留首尾空格的情况 ...

  2. phpMyAdmin 4.0.x—4.6.2 远程代码执行漏洞(CVE-2016-5734)

    影响范围 4.0.10.16之前4.0.x版本 4.4.15.7之前4.4.x版本 4.6.3之前4.6.x版本(实际上由于该版本要求PHP5.5+,所以无法复现本漏洞) 需要登录,且能够写入数据 p ...

  3. 「必知必会」最细致的 ArrayList 原理分析

      从今天开始也正式开 JDK 原理分析的坑了,其实写源码分析的目的不再是像以前一样搞懂原理,更重要的是看看他们编码风格更进一步体会到他们的设计思想.看源码前先自己实现一个再比对也许会有不一样的收获! ...

  4. C# WinForm 数据库连接及对数据库的相关操作

    1.首先在App.config配置文件中配置数据库连接字符串: <appSettings> <add key="connectionstring" value=& ...

  5. tomcat启动时启动窗口出现乱码一招搞定

    先来看看问题(图示),在tomcat的启动窗口打印的启动信息中包含了大量的中文乱码,虽然这些对tomcat本身的使用没有任何影响,但却非常碍眼,影响视觉效果! tomcat启动时启动窗口出现乱码的解决 ...

  6. HDFS总结

    hadoop分布式文件存储系统,用来解决海量数据的存储问题 HDFS的组成------核心配置文件:hdfs-site.xml.core-site.xml NameNode:负责整个HDFS集群的管理 ...

  7. JAVA 调用第三方短信平台接口发送短信

    做了几个调用三方短信平台发送短信的例子,大部分需要 携带参数,向指定URL发送请求 回顾对接第一个平台时痛苦的乱码经历,这里放一份代码,算是个模版,再用到的时候过来copy一下就OK. 在进入主题之前 ...

  8. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画

    前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出-   自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...

  9. MySQL学习02(操作数据库)

    操作数据库 结构化查询语句分类 名称 解释 命令 DDL(数据库定义语言) 定义和管理数据对象,例如数据库和数据表 create.drop.alter DML(数据操作语言) 用于操作数据库对象中所包 ...

  10. 如何请求一个需要登陆才能访问的接口(基于cookie)---apipost

    在后台在开发.调试接口时,常常会遇到需要登陆才能请求的接口. 比如:获取登陆用户的收藏列表,此时,我们就需要模拟登陆状态进行接口调试了.如图: 今天,我们讲解利用ApiPost的环境变量,解决这种需要 ...