codeforces 420D Cup Trick
codeforces 420D Cup Trick
题意
题解
官方做法需要用到线段树+平衡树(? 如果数据小的话似乎可以用莫队)。然后代码好长好长。我补了一个只要用到树状数组的做法。
代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N=1000007, S=N-4;
int n,m;
int cnt[N<<1], ans[N], pre[N<<1];
bool vis[N];
void upd(int p,int c) {
for(int i=p;i<=S+S;i+=(i&-i)) cnt[i]+=c;
}
int qry(int p) {
int res=0;
for(int i=p;i;i-=(i&-i)) res+=cnt[i];
return res;
}
int main() {
scanf("%d%d",&n,&m);
rep(i,1,S+1) upd(i+S, 1);
int p=S;
rep(i,1,m+1) {
int v, x;scanf("%d%d",&v,&x);
int l=1, r=S+S, res=-1;
while(l<=r) {
int mid=l+r>>1;
if(qry(mid)<x) {
res=mid+1;
l=mid+1;
} else {
r=mid-1;
}
}
if(res>S) {
if(vis[v]) {
puts("-1");
return 0;
}
vis[v]=1;
ans[res-S]=v;
} else {
if(pre[res]!=v) {
puts("-1");
return 0;
}
}
upd(res, -1);
upd(p, 1);
pre[p]=v;
--p;
}
for(int i=1,j=1;i<=n;++i) if(!ans[i]) {
while(vis[j]) ++j;
ans[i]=j;
vis[j]=1;
}
rep(i,1,n+1) printf("%d%c",ans[i]," \n"[i==n]);
return 0;
}
codeforces 420D Cup Trick的更多相关文章
- Codeforces 420D Cup Trick 平衡树
Cup Trick 平衡树维护一下位置. #include<bits/stdc++.h> #include <bits/extc++.h> #define LL long lo ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)
题目链接:http://codeforces.com/problemset/problem/164/A 思路:用vector分别保留原图和发图,然后分别从val值为1的点正向遍历,va值为2的点反向遍 ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学
H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题
D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP
F. Bulbo Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/F Des ...
- codeforces VK Cup 2015 - Qualification Round 1 B. Photo to Remember 水题
B. Photo to Remember Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/522/ ...
- CodeForces Canada Cup 2016【A,B,C,D】
CodeForces 725A: 思路就是如果"最左"不是'>'这个了,那么这个右边的一定不可能到达左边了: 同理最右: CodeForces 725B: 有两个空姐,一个从 ...
随机推荐
- HaspMap的新奇用法
HashMap<String, String> map = new HashMap<String,String>(){ private static final long se ...
- 《Think Python》第16章学习笔记
目录 <Think Python>第16章学习笔记 16.1 Time 16.2 纯函数(Pure functions) 16.3 修改器(Modifiers) 16.4 原型 vs. 方 ...
- mysql导入外部.sql文件时错误
当前环境: 操作系统:windows 7 mysql版本:5.5.36 MySQL Community Server (GPL) 当我第一次导入.sql文件时报错: mysql> source ...
- 【PhotoShop】模糊图片清晰处理
1.Ctrl+J复制出背景副本.对副本模式选择“亮度”. 2.选择“滤镜”菜单下的“锐化>USM锐化”命令,在设置窗口中适当调节一下锐化参数,根据你原图模糊的情况来调节,本图采用锐化数量为“15 ...
- 如何应用ASP.NET MVC中的分部视图
概述: 在ASP.NET Web Form的开发经验中,对于User Control使用比较频繁,可以减少重复的代码,利于页面模块化,这个概念也被引入了ASP.NET MVC.即“分部视图”. 1.创 ...
- 九、curator recipes之不可重入锁InterProcessSemaphoreMutex
简介 recipes的InterProcessSemaphoreMutex是一种不可重入的互斥锁,也就意味着即使是同一个线程也无法在持有锁的情况下再次获得锁,所以需要注意,不可重入的锁很容易在一些情况 ...
- MySQL数据源驱动报错
报错信息:MySQL数据源驱动报错: 1.mysql8.0以上版本需要连接数据库的JDBC驱动也是8.0版本以上 com.mysql.cj.jdbc.Driver 2.MySQL高版本需要指明是否需要 ...
- aabb问题
输出所有aabb型的完全平方数 这个问题主要是提供一个判断完全平方的思路,就是用floor函数 #include <iostream> #include <string> #i ...
- HDU 2680(最短路)(多个起始点)
这道题也是死命TLE.. http://acm.hdu.edu.cn/showproblem.php?pid=2680 /* 使用pair代替结构 */ #include <iostream&g ...
- 【C++并发实战】(三) std::future和std::promise
std::future和std::promise std::future std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::f ...