https://codeforces.com/gym/102091

2018-2019 ACM-ICPC, Asia Nakhon Pathom Regional Contest

A Flying Squirrel

# 题意

有n个柱子。m次询问。

每次询问从x号柱子跳到y号柱子,最多能踩几个柱子。

每次跳跃只能向低的柱子跳,且中间不能有高于起跳点的柱子。

# 思路

化数列为DAG,对于一个柱子u来说,向左跳到能跳的区域中最高的柱子v,我们连边u->v。然后就类似树上的操作。

注意dfs和bfs中都要做好打标记的操作。

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); } template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} const int inf = 0x3f3f3f3f; const int mod = 1e9+; /**********showtime************/
const int maxn = 1e5+;
int a[maxn];
vector<int>mp[maxn];
vector<int>hi[maxn]; int mx[maxn<<];
void build(int le, int ri, int rt){
if(le == ri) {
mx[rt] = a[le];
return;
}
int mid = (le + ri) >> ;
build(le, mid, rt<<);
build(mid+,ri,rt<<|);
mx[rt] = max(mx[rt<<], mx[rt<<|]);
}
int query(int L, int R, int le, int ri, int rt){
if(le >= L && ri <= R) {
return mx[rt];
}
int mid = (le + ri) >> ;
int res = ;
if(mid >= L) res = max(res, query(L, R, le, mid, rt<<));
if(mid < R) res = max(res, query(L, R, mid+, ri, rt<<|));
return res;
}
int low[maxn],up[maxn];
int st[maxn];
int top = ;
int dp[maxn], mdp[maxn];
int vis[maxn],used[maxn];
void dfs(int u, int o) {
vis[u] = true;
dp[u] = dp[o] + ;
mdp[u] = ;
for(int v : mp[u]) {
if(!vis[v]) dfs(v, u);
mdp[u] = max(mdp[u], mdp[v] + );
}
}
bool check(int x, int y) {
if(y <= up[x] && y >= low[x]) return true;
if(x <= up[y] && x >= low[y]) return true;
return false;
}
int main(){
int n,m;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) {
scanf("%d", &a[i]);
hi[a[i]].pb(i);
} for(int i=; i<=n; i++) {
while(top > && a[st[top]] < a[i]) top--;
if(top == ) low[i] = ;
else low[i] = st[top] + ;
st[++top] = i;
} top = ;
for(int i=n; i>=; i--) {
while(top > && a[st[top]] < a[i])top--;
if(top == ) up[i] = n;
else up[i] = st[top] - ;
st[++top] = i;
} build(, n, );
int big = query(, n, , n, ); int root = n + ;
queue<int>que;
for(int v : hi[big]) {
mp[root].pb(v);
que.push(v);
} while(!que.empty()) {
int u = que.front(); que.pop();
int le = low[u], ri = up[u];
if(le < u) {
int mx = query(le, u - , , n, );
int id = lower_bound(hi[mx].begin(), hi[mx].end(), le) - hi[mx].begin(); for(int i=id; i<hi[mx].size(); i++) {
if(hi[mx][i] >= u) break;
int v = hi[mx][i];
mp[u].pb(v);
if(used[v] == )
{
que.push(v);
used[v] = ;
}
}
}
if(u < ri) {
int mx = query(u+, ri, , n, );
int id = lower_bound(hi[mx].begin(), hi[mx].end(), u+) - hi[mx].begin();
for(int i=id; i<hi[mx].size(); i++) {
if(hi[mx][i] > ri) break;
int v = hi[mx][i];
mp[u].pb(v);
if(used[v] == ) {
used[v] = ;
que.push(v);
}
}
}
} dfs(root, root); for(int i=; i<=m; i++) {
int x,y;
scanf("%d%d", &x, &y);
if(y == ) {
printf("%d\n", mdp[x]);
}
else {
if(!check(x, y)) puts("");
else printf("%d\n", abs(dp[x] - dp[y]));
}
}
return ;
}

E How Many Groups

K The Stream of Corning 2

# 题意

有n个事物会出现在河流中,每个事物会出现于le到ri秒。问第i秒第K小是多少。保证le和i在出现顺序中是递增的。

# 思路

先离散化,然后开一个树状数组,出现了的事物就插入权值树状数组,并记下消失的时间ri,等查询操作前把消失的事物清除。 
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
struct FastIO {
static const int S = 4e6;
int wpos;
char wbuf[S];
FastIO() : wpos() {}
inline int xchar() {
static char buf[S];
static int len = , pos = ;
if (pos == len)
pos = , len = fread(buf, , S, stdin);
if (pos == len) exit();
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = ;
while (c <= ) c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x;
}
inline int xint()
{
int s = , c = xchar(), x = ;
while (c <= ) c = xchar();
if (c == '-') s = -, c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while (c <= ) c = xchar();
for (; c > ; c = xchar()) * s++ = c;
*s = ;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, , S, stdout), wpos = ;
wbuf[wpos++] = x;
}
inline void wint(int x)
{
if (x < ) wchar('-'), x = -x;
char s[];
int n = ;
while (x || !n) s[n++] = '' + x % , x /= ;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, , wpos, stdout), wpos = ;
}
} io;
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e6+;
vector<int>v;
int getid(int x){
return lower_bound(v.begin(), v.end(), x) - v.begin() + ;
}
struct ask
{
int op;
int a,b,c;
}e[maxn];
int sum[maxn];
int lowbit(int x){
return x & (-x);
}
void add(int x,int c){
while(x < maxn){
sum[x] += c;
x = x + lowbit(x);
}
}
int cal(int x){
int res = ;
while(x > ){
res += sum[x];
x -= lowbit(x);
}
return res;
}
vector<int>er[maxn]; int main(){
int T,n; //scanf("%d", &T);
T = io.xint();
rep(cas, , T){
printf("Case %d:\n", cas);
// scanf("%d", &n);
n = io.xint();
v.clear();
for(int i=; i<maxn; i++) er[i].clear();
memset(sum, , sizeof(sum));
rep(i, , n) {
// scanf("%d", &e[i].op);
e[i].op = io.xint();
if(e[i].op == ) {
// scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
e[i].a = io.xint(); e[i].b = io.xint(); e[i].c = io.xint();
v.pb(e[i].a),v.pb(e[i].c);
}
else {
// scanf("%d%d", &e[i].a, &e[i].b);
e[i].a = io.xint(); e[i].b = io.xint();
v.pb(e[i].a);
}
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int la = ;
rep(i, , n){ if(e[i].op == ) {
add(e[i].b, );
int t = getid(e[i].c);
er[t+].pb(e[i].b);
}
else {
for(int k=la; k <= getid(e[i].a); k++)
for(int j=; j<er[k].size(); j++){
add(er[k][j], -);
}
la = getid(e[i].a)+;
int res = -, le = , ri = maxn-; while(le <= ri){
int mid = (le + ri) >> ;
if(cal(mid) >= e[i].b) {res = mid, ri = mid - ;}
else le = mid + ;
}
if(res == -) puts("-1");
else printf("%d\n", res);
}
} } return ;
}

最新文章

  1. 关于BigDecimal 和 double 类型保存金钱,以及精度问题,银行家舍入法
  2. 系统无法开始服务器进程。请检查用户名和密码。 (Exception from HRESULT: 0x8000401A)
  3. JQuery获取元素的方法总结
  4. [CareerCup] 16.4 A Lock Without Deadlocks 无死锁的锁
  5. centos重启不能自动联网的解决方法
  6. c#泛型方法返回null的问题
  7. 另类加载dll---快捷方式启动参数
  8. HDU 5317 RGCDQ (质数筛法,序列)
  9. MyEclipse server窗口 Could not create the view: An unexpected exception was thrown 错误解决
  10. windows 挂载linux nfs
  11. Android:设置背景图和标题
  12. shell脚本练习题
  13. Gazebo機器人仿真學習探索筆記(一)安裝與使用
  14. 如何在ASP.NET Core中使用JSON Patch
  15. js实现浏览器调用电脑的摄像头拍照
  16. 性能测试学习 第九课--LR12中controller基础知识
  17. ubuntu安装QGIS
  18. 【软件工程1916|W(福州大学)_助教博客】团队第四次作业(第7次)成绩公示
  19. mysql操作命令梳理(5)-执行sql语句查询即mysql状态说明
  20. Codeforces Round #467 (Div. 2) E -Lock Puzzle

热门文章

  1. 阿里云nas使用记录
  2. 现代c++与模板元编程
  3. zabbix监控WEB网站性能
  4. Dubbo里面线程池的拒绝策略
  5. spark shuffle写操作之SortShuffleWriter
  6. luogu2279_[HNOI2003]消防局的设立 贪心
  7. LR(1)语法分析器生成器(生成Action表和Goto表)java实现(一)
  8. python中下标和切片的使用
  9. .Net Core 最优 MD5 打开方式!初学者建议收藏(支持 SHA1,SHA256,.Net Framework)
  10. 使用Prerender.io进行网站预加载