20210809 Merchant,Equation,Rectangle
做过,但当时咕了 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的更多相关文章
- csp-s模拟测试56Merchant, Equation,Rectangle题解
题面:https://www.cnblogs.com/Juve/articles/11619002.html merchant: 二分答案,贪心选前m大的 但是用sort复杂度不优,会T掉 我们只是找 ...
- csp-s模拟测试56(10.2)Merchant「二分」·Equation「树状数组」
又死了......T1 Merchant 因为每个集合都可以写成一次函数的形式,所以假设是单调升的函数,那么随着t越大就越佳 而单调减的函数,随着t的增大结果越小,所以不是单调的??? 但是我们的单调 ...
- 2D Rotated Rectangle Collision
Introduction While working on a project for school, I found it necessary to perform a collision chec ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [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 ...
- [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 ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- Pytorch指定GPU的方法总结
Pytorch指定GPU的方法 改变系统变量 改变系统环境变量仅使目标显卡,编辑 .bashrc文件,添加系统变量 export CUDA_VISIBLE_DEVICES=0 #这里是要使用的GPU编 ...
- Linux的链接(入门)
Linux的链接分为两种:硬链接和软链接 硬链接:如果B是A的硬链接,那么B和A指向同一个文件,但是删除A并不会影响B->允许一个文件有多个路径 软链接:类似Windows下的快捷方式,删除原文 ...
- 获取浏览器中url的参数
例如: 浏览器的地址是:http://localhost:8080/src/views/moneyDetail?id=10 vue 获取浏览器的参数 获取id的参数:this.$route.query ...
- MySQL 事务、日志、锁、索引学习总结,
MySQL架构 MySQL可分为Server和存储引擎两部分,如图1所示. Server层:包括客户端连接器.查询缓存.解析/预处理器.优化器.执行器等,以及MySQL内置函数和所有跨引擎的功能都在这 ...
- WPF MVVM模式下路由事件
一,路由事件下三种路由策略: 1 冒泡:由事件源向上传递一直到根元素.2直接:只有事件源才有机会响应事件.3隧道:从元素树的根部调用事件处理程序并依次向下深入直到事件源.一般情况下,WPF提供的输入事 ...
- 客户端连接mysql数据库反应慢
远程客户端连接MysqL数据库太慢解决方案 局域网客户端访问mysql 连接慢问题解决 编辑mysql配置文件 # vi my.conf [mysqld] skip-name-resolve 重启my ...
- 【笔记】Bagging和Pasting以及oob(Out-of-Bag)
Bagging和Pasting以及oob(Out-of-Bag) Bagging和Pasting 前面讲到soft voting classifier和hard voting classifier两个 ...
- Java-Collection、Map和Array之间的转换
1 List -> Map 设个User类: public class User { private String userName; private String userId; privat ...
- Spring Boot 项目集成Redis
目录 集成方式 使用Jedis 使用spring-data-redis Redis的安装 绑定配置 获取Redis客户端 Redis工具的编写 使用 集成方式 使用Jedis Jedis是Redis官 ...
- 从自旋锁、睡眠锁、读写锁到 Linux RCU 机制讲解
同步自我的 csdn 博客 6.S081 从自旋锁.睡眠锁.读写锁到 Linux RCU 机制讲解_我说我谁呢 --CSDN博客 总结一下 O/S 课程里面和锁相关的内容. 本文是 6.S0 ...