Submission #1077145

#TimeUsernameProblemLanguageResultExecution timeMemory
1077145LIFEvent Hopping (BOI22_events)C++14
10 / 100
1585 ms62232 KiB
#include<bits/stdc++.h> using namespace std; int n,q,l[500005],r[500005]; int su[800005]; int dis[5005][5005]; struct nod { int ll; int rr; int id; }node[500005]; bool cmp(nod x,nod y) { if(x.rr == y.rr)return x.ll < y.ll; else return x.rr < y.rr; } void pushup(int rt) { su[rt] = min(su[rt<<1],su[rt<<1|1]); return; } void build(int l,int r,int rt,int now) { if(l == r) { su[rt] = dis[now][l]; return; } int mid = (l+r)>>1; build(l,mid,rt<<1,now); build(mid+1,r,rt<<1|1,now); pushup(rt); return; } int query(int l,int r,int queryl,int queryr,int rt) { if(queryl <= l && r <= queryr) { return su[rt]; } int maxn = 1e9+2e6; int mid = (l+r)>>1; if(mid >= queryl)maxn = min(maxn,query(l,mid,queryl,queryr,rt<<1)); if(mid+1 <= queryr)maxn = min(maxn,query(mid+1,r,queryl,queryr,rt<<1|1)); return maxn; } void change(int l,int r,int askl,int askr,int val,int rt) { if(askl <= l && r <= askr) { su[rt] = val; return; } int mid = (l+r)>>1; if(mid >= askl)change(l,mid,askl,askr,val,rt<<1); if(mid+1 <= askr)change(mid+1,r,askl,askr,val,rt<<1|1); pushup(rt); return; } int main() { //freopen("std.out","wb",stdout); cin>>n>>q; for(int i=1;i<=n;i++) { scanf("%d%d",&l[i],&r[i]); nod temp = nod{l[i],r[i],i}; node[i] = temp; } sort(node+1,node+n+1,cmp); for(int now = 1;now<=n;now++) { for(int i=1;i<=n;i++)dis[now][i] = 1e9; int init,target; init = now; int num = 0; for(int i=1;i<=n;i++) { if(node[i].ll == l[init] && node[i].rr == r[init]) { num = i; break; } } dis[now][node[num].id] = 0; build(1,n,1,now); change(1,n,num,num,dis[now][node[num].id],1); for(int i=num+1;i<=n;i++) { int l = 1; int r = i-1; int ans = -1; while(l <= r) { int mid = (l+r)>>1; if(node[mid].rr >= node[i].ll) { ans = mid; r = mid - 1; } else l = mid + 1; } //即[ans,i-1]是滿足rr >= ll的; //cout<<"yeah2"<<endl; if(ans != -1)dis[now][node[i].id] = query(1,n,ans,i-1,1) + 1; //cout<<"yeah3"<<endl; change(1,n,i,i,dis[now][node[i].id],1); //cout<<"yeah4"<<endl; } } while(q--) { int x,y; scanf("%d%d",&x,&y); if(dis[x][y] >= 1e9) { printf("impossible\n"); } else printf("%d\n",dis[x][y]); } return 0; }

Compilation message (stderr)

events.cpp: In function 'int main()':
events.cpp:74:22: warning: unused variable 'target' [-Wunused-variable]
   74 |             int init,target;
      |                      ^~~~~~
events.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |         scanf("%d%d",&l[i],&r[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~
events.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         scanf("%d%d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...