gym/102091
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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } const int inf = 0x3f3f3f3f; const int mod = 1e9+7; /**********showtime************/ const int maxn = 1e5+9; int a[maxn]; vector<int>mp[maxn]; vector<int>hi[maxn]; int mx[maxn<<2]; void build(int le, int ri, int rt){ if(le == ri) { mx[rt] = a[le]; return; } int mid = (le + ri) >> 1; build(le, mid, rt<<1); build(mid+1,ri,rt<<1|1); mx[rt] = max(mx[rt<<1], mx[rt<<1|1]); } int query(int L, int R, int le, int ri, int rt){ if(le >= L && ri <= R) { return mx[rt]; } int mid = (le + ri) >> 1; int res = 0; if(mid >= L) res = max(res, query(L, R, le, mid, rt<<1)); if(mid < R) res = max(res, query(L, R, mid+1, ri, rt<<1|1)); return res; } int low[maxn],up[maxn]; int st[maxn]; int top = 0; int dp[maxn], mdp[maxn]; int vis[maxn],used[maxn]; void dfs(int u, int o) { vis[u] = true; dp[u] = dp[o] + 1; mdp[u] = 0; for(int v : mp[u]) { if(!vis[v]) dfs(v, u); mdp[u] = max(mdp[u], mdp[v] + 1); } } 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=1; i<=n; i++) { scanf("%d", &a[i]); hi[a[i]].pb(i); } for(int i=1; i<=n; i++) { while(top > 0 && a[st[top]] < a[i]) top--; if(top == 0) low[i] = 1; else low[i] = st[top] + 1; st[++top] = i; } top = 0; for(int i=n; i>=1; i--) { while(top > 0 && a[st[top]] < a[i])top--; if(top == 0) up[i] = n; else up[i] = st[top] - 1; st[++top] = i; } build(1, n, 1); int big = query(1, n, 1, n, 1); int root = n + 1; 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 - 1, 1, n, 1); 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] == 0) { que.push(v); used[v] = 1; } } } if(u < ri) { int mx = query(u+1, ri, 1, n, 1); int id = lower_bound(hi[mx].begin(), hi[mx].end(), u+1) - 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] == 0) { used[v] = 1; que.push(v); } } } } dfs(root, root); for(int i=1; i<=m; i++) { int x,y; scanf("%d%d", &x, &y); if(y == 0) { printf("%d\n", mdp[x]); } else { if(!check(x, y)) puts("0"); else printf("%d\n", abs(dp[x] - dp[y])); } } return 0; }
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<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; 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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } struct FastIO { static const int S = 4e6; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int xchar() { static char buf[S]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, S, stdin); if (pos == len) exit(0); return buf[pos++]; } inline int xuint() { int c = xchar(), x = 0; while (c <= 32) c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x; } inline int xint() { int s = 1, c = xchar(), x = 0; while (c <= 32) c = xchar(); if (c == '-') s = -1, c = xchar(); for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0'; return x * s; } inline void xstring(char *s) { int c = xchar(); while (c <= 32) c = xchar(); for (; c > 32; c = xchar()) * s++ = c; *s = 0; } inline void wchar(int x) { if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0; wbuf[wpos++] = x; } inline void wint(int x) { if (x < 0) wchar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) wchar(s[n]); wchar('\n'); } inline void wstring(const char *s) { while (*s) wchar(*s++); } ~FastIO() { if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0; } } 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+9; vector<int>v; int getid(int x){ return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } 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 = 0; while(x > 0){ 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, 1, T){ printf("Case %d:\n", cas); // scanf("%d", &n); n = io.xint(); v.clear(); for(int i=0; i<maxn; i++) er[i].clear(); memset(sum, 0, sizeof(sum)); rep(i, 1, n) { // scanf("%d", &e[i].op); e[i].op = io.xint(); if(e[i].op == 1) { // 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 = 0; rep(i, 1, n){ if(e[i].op == 1) { add(e[i].b, 1); int t = getid(e[i].c); er[t+1].pb(e[i].b); } else { for(int k=la; k <= getid(e[i].a); k++) for(int j=0; j<er[k].size(); j++){ add(er[k][j], -1); } la = getid(e[i].a)+1; int res = -1, le = 1, ri = maxn-1; while(le <= ri){ int mid = (le + ri) >> 1; if(cal(mid) >= e[i].b) {res = mid, ri = mid - 1;} else le = mid + 1; } if(res == -1) puts("-1"); else printf("%d\n", res); } } } return 0; }
skr