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 ;
}

gym/102091的更多相关文章

  1. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. 并发栅栏CyclicBarrier---简单问2

    并发栅栏CyclicBarrier---简单问 背景:前几天在网上看到关于Java并发包java.concurrent中一个连环炮的面试题,整理下以备不时之需. CyclicBarrier简介: 栅栏 ...

  2. Mysql执行过程总结

    总分三个阶段:Sql的解析,执行和结果获取阶段. 如下图,展开相熟.

  3. codeforces 327 A Ciel and Dancing

    题目链接 给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1. 这里要注意很多细节 比如全为1,要求必须翻转,这时候我们 ...

  4. Spring 源码学习(一)-容器的基础结构

    关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料 展示的代码摘取了一些核心方法,去掉一些默认设置和日志输出,还有大多数错误异常也去掉了,小伙伴想看详细代码 ...

  5. UR机器人通信--上位机通信(python)

    一.通信socket socket()函数 Python 中,我们用 socket()函数来创建套接字,语法格式如下: socket.socket([family[, type[, proto]]]) ...

  6. SpringMVC项目案例之---数据的获取与显示

    数据的获取与显示 (一)功能 1.对用户输入的数据进行获取 2.将获取的数据显示到页面 3.使用了SpringMVC技术的注解方式 4.使用了过滤器,处理中文乱码问题 5.在web.xml中设置了访问 ...

  7. WebGL简易教程(二):向着色器传输数据

    目录 1. 概述 2. 示例:绘制一个点(改进版) 1) attribute变量 2) uniform变量 3) varying变量 3. 结果 4. 参考 1. 概述 在上一篇教程<WebGL ...

  8. javaScript基础-02 javascript表达式和运算符

    一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...

  9. Codeforces 436D Pudding Monsters

    题意简述 开始有无限长的一段格子,有n个格子种有布丁怪兽,一开始连续的布丁怪兽算一个布丁怪兽. 每回合你可以将一个布丁怪兽向左或右移动,他会在碰到第一个布丁怪兽时停下,并与其合并. 有m个特殊格子,询 ...

  10. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...