ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps (弱线段树)
线段树节点维护区间最小值,查找时优先从左侧的区间寻找.
每一次循环都在树中不停寻找第一个小于等于当前持有数的值,然后抹去,直到找不到为止.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define lson rt<<1
#define rson rt<<1|1
#define Lson l,m,lson
#define Rson m+1,r,rson
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+5;
int val[maxn];
struct Node{
int num;
}tree[maxn<<2];
int n;
void pushup(int rt)
{
tree[rt].num = min(tree[lson].num,tree[rson].num);
}
void build(int l=1,int r=n,int rt=1)
{
if(l==r){
tree[rt].num = val[l];
return ;
}
int m = (l+r)>>1;
build(Lson);
build(Rson);
pushup(rt);
}
bool flag;
int query(int val,int l=1,int r=n,int rt=1)
{
int res=-1,m = (l+r)>>1;
if(tree[rt].num<=val){
if(l==r){
int tmp = tree[rt].num;
tree[rt].num = INF;
return tmp;
}
res = query(val,Lson);
if(res!=-1) {
pushup(rt);
return res;
}
res = query(val,Rson);
if(res!=-1){
pushup(rt);
return res;
}
}
return -1;
}
int ans1[maxn];
int ans2[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int m,q;
while(scanf("%d %d",&n,&m)==2){
for(int i=1;i<=n;++i){
scanf("%d",&val[i]);
}
build();
flag = false;
int sum = 0,tot=0;
for(int i=1;i<=100000;++i){
//cout<<tree[1].num<<endl;
if(tree[1].num ==INF) flag = true;
if(!flag) {
sum += m;
int tmp = query(sum);
while(tmp!=-1){
sum-=tmp;
tot++;
tmp = query(sum);
}
}
ans1[i] = tot;
ans2[i] = sum;
}
int id;
scanf("%d",&q);
for(int i=1;i<=q;++i){
scanf("%d",&id);
printf("%d %d\n",ans1[id],ans2[id]);
}
}
return 0;
}
ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps (弱线段树)的更多相关文章
- ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(线段树)
题目链接:https://nanti.jisuanke.com/t/30996 中文题目: 在喝茶的过程中,公主,除其他外,问为什么这样一个善良可爱的龙在城堡里被监禁Lpl?龙神秘地笑了笑,回答说这是 ...
- ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)
https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...
- ACM-ICPC 2018 南京赛区网络预赛 G. Lpl and Energy-saving Lamps(二分+线段树区间最小)
During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Drag ...
- 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)
G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K During tea-drinking, princess, amongst other t ...
- ACM-ICPC 2018 南京赛区网络预赛 I Skr (马拉车+hash去重)或(回文树)
https://nanti.jisuanke.com/t/30998 题意 给一串由0..9组成的数字字符串,求所有不同回文串的权值和.比如说“1121”这个串中有“1”,“2”,“11”,“121” ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ACM-ICPC 2018 南京赛区网络预赛(12/12)
ACM-ICPC 2018 南京赛区网络预赛 A. An Olympian Math Problem 计算\(\sum_{i=1}^{n-1}i\cdot i!(MOD\ n)\) \(\sum_{i ...
- ACM-ICPC 2018 南京赛区网络预赛 J.sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...
- ACM-ICPC 2018 南京赛区网络预赛 E题
ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...
随机推荐
- hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)
Mirror and Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- WebService远程调用(命令行调用)
远程调用webservice 方法, 通过wsdl命令,生成webservice 对应asmx 文件的对应类(cs文件),然后将此cs文件引用到当前项目中,项目其他地方需要调用webservice方法 ...
- Hibernate传递list参数的例子
public Map<String, String> getAllFeedBack(Object[] obj){ Map<String, String> map = new H ...
- 160427、CSS3实战笔记--多列布局
通过阅读和学习书籍<CSS3实战>总结 <CSS3实战>/成林著.—北京机械工业出版社2011.5 多列布局适合纯文字版式设计,如报纸内和杂志类网页布局,不适合做网页结构布 ...
- PHP array_merge() 函数
<?php $a1=array("a"=>"red","b"=>"green"); $a2=array ...
- web测试点--摘录
转载地址:Web测试到底是在测什么 一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&am ...
- Struts2中获取Web元素request、session、application对象的四种方式
我们在学习web编程的时候,一般都是通过requet.session.application(servletcontext)进行一系列相关的操作,request.session.和applicatio ...
- 聚币网API[Python3版]
代码 #!/usr/bin/env python # -*- coding:utf-8 -*- import hashlib import requests import time import ur ...
- Android中TextView设置最大长度,超出显示省略号
今天在项目中碰到一个问题,在一个页面的顶部的标题栏显示公司的名字,但由于公司名称较长,显示不开,影响美观.故在网上查阅资料,在此做个小的总结. TextView中有个ellipsize属性,作用是当文 ...
- sql server中使用xp_cmdshell
关键词:sql server开启高级配置,使用Bat,cmdshell 1.sql server中使用xp_cmdshell --允许配置高级选项 GO RECONFIGURE GO . --开启xp ...