洛谷 [T21778] 过年
离线扫描线+查分+线段树
我们发现,这个题的询问都是离线的,所以我们尝试用离线扫描线的方法来处理
对于每一次操作,我们维护一个差分数组,
在询问的时候,我们用一根扫描线,从左往右扫,并用线段树维护,每种礼物的次数,
每扫到一个人,先处理在这个人处的操作,然后查询最大值即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
using namespace std;
const int MAXN = 200005;
int init() {
int rv = 0, fh = 1;
char c = getchar ();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <='9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
int n, m, cnt, ma;
struct opt {
int loc, k;
bool f;
bool operator < (const opt &a) const{
return this -> loc < a.loc;
}
}num[MAXN<<1];
struct SGT{
int sum[MAXN<<2], pos[MAXN<<2];
void PushUp(int rt) {
if(sum[rt<<1] >= sum[rt<<1|1]) {
sum[rt] = sum[rt<<1];
pos[rt] = pos[rt<<1];
}else {
sum[rt] = sum[rt<<1|1];
pos[rt] = pos[rt<<1|1];
}
}
void build(int l, int r, int rt) {
if(l == r) {
pos[rt] = l;
sum[rt] = 0;
return;
}
int mid = (l + r) >>1;
build(lson);
build(rson);
PushUp(rt);
}
void Update(int p, bool op, int l, int r, int rt) {
if(l == r) {
if(op) {
sum[rt]--;
}else sum[rt]++;
return;
}
int mid = (l + r) >>1;
if(p <= mid) Update(p, op, lson);
else Update(p, op, rson);
PushUp(rt);
}
}sgt;
int main() {
freopen("in.txt", "r", stdin);
n = init(); m = init();
for(int i = 1 ; i <= m ; i++) {
int l = init(), r = init(), k = init();
ma = max(ma, k);
num[++cnt].loc = l; num[cnt].k = k; num[cnt].f = 0;
if(r != n){
num[++cnt].loc = r+1; num[cnt].k = k; num[cnt].f = 1;
}
}
sort(num+1, num+cnt+1); //注意:这里加的是cnt
sgt.build(1, ma, 1);
int cur = 0;
for(int i = 1 ; i <= n ; i++) {
while(num[++cur].loc == i) sgt.Update(num[cur].k, num[cur].f, 1, ma, 1);
cur--;
//printf("%d %d %d \n", sgt.sum[1], sgt.sum[2], sgt.sum[3]);
if(!sgt.sum[1]) printf("-1\n");
else printf("%d\n", sgt.pos[1]);
}
fclose(stdin);
return 0;
}
洛谷 [T21778] 过年的更多相关文章
- 洛谷T21778 过年
题目描述 有 n(1 \leq n \leq 10^5)n(1≤n≤105) 个小朋友,过年了,要发放 m(1 \leq m \leq 10^5)m(1≤m≤105) 次礼物. 每次发放,会给出三个参 ...
- 洛谷.T21778.过年(线段树 扫描线)
题目链接或者这吧.. 被数据坑了 /* 操作按左端点排个序 依次进行即可 不是很懂 为什么不写Build 而在Add时改mp[rt]=p 会WA(too short on line 251..) 找到 ...
- 洛谷-P5357-【模板】AC自动机(二次加强版)
题目传送门 -------------------------------------- 过年在家无聊补一下这周做的几道AC自动机的模板题 sol:AC自动机,还是要解决跳fail边产生的重复访问,但 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
- 洛谷P1371 NOI元丹
P1371 NOI元丹 71通过 394提交 题目提供者洛谷OnlineJudge 标签云端评测 难度普及/提高- 提交 讨论 题解 最新讨论 我觉得不需要讨论O long long 不够 没有取 ...
随机推荐
- nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati
root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx --add-mod ...
- java 核心技术卷一笔记 6 .1接口 lambda 表达式 内部类
6.1 接口不是类,是对类的一组需求的描述,这些类需要遵守接口描述的统一格式进行定义.例如:Arrays类中sort方法(可以对对象数组进行排序)前提是对象所属的类必须实现了Comparable 接口 ...
- 新数据的GT列表
制作新数据集时需要重新制作train_GT,test_GT 代码: dic = {} with open('/home/bnrc/all_image_GT.txt','r') as file: for ...
- Bootstrap-datepicker设置开始时间结束时间范围
$('.form_datetime').datepicker({ format: 'yyyy-mm-dd', weekStart: 1, startDate: '+1', endD ...
- synchronized 和ReentrantLock的区别
历史知识:JDK5之前,只有synchronized 可以用,之后就有了ReetrantLock可以用了 ReetrantLock (再入锁) 1.位于java.util.concurrnt.lock ...
- html中常见符号的代码表示
HTML中空格的集中代码表示: HTML中空格 不断行的空白(1个字符宽度) 半个空白(1个字符宽度) 一个空白(2个字符宽度) 窄空白(小于1个字符宽度) 其他常见的 ...
- java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并
实现效果图: eclipse项目中初步整合之前的各个客户端和服务端的窗口与工具类,效果如下图: 已将注册服务器线程RegServer功能放到LoginServer中,使用客户端与服务端的request ...
- 【图论】hdu6370Werewolf
有理有据的结论题 Problem Description "The Werewolves" is a popular card game among young people.In ...
- Perl学习之四:语句(续)
循环控制:1.last 退出标签的语句块2.next 3.redo不推荐,循环次数不可控 4.goto不推荐.***************************************标签: 先 ...
- git 常用命令及虚拟机服务器仓库搭建
$ git config --global user.email "you@example.com" $ git config --global user.name "Y ...