標籤:des 判斷 分享 code src width one load 一個
題目描述
輸入
輸出
範例輸入11 10000 query 5 construct 5 500 100 query 500 query 1000 construct 10 90 5 query 44 destruct 44 66 query 55 construct 50 60 3 query 46 query 6000 範例輸出0 975 0 9999 9775 9984 0 提示
題解
這道題給你三種操作(這道題用set維護區間)
對於construct操作,我們可以發現加入的區間要麼和所有的區間都不相交,要麼就是區間的大小比某個區間的v還要小
construct操作讀入的x,y我們可以可以把y改成x+(y-x)/v*v(最後一個訊號站的位置)
對於destruct操作,我們判斷一下左右端點是否把其他的區間截斷了
對於query操作,我們只要找一下離這個點最近的一個區間就可以了
具體細節可以看一下代碼
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 struct node{ 5 int x,y,v; 6 bool operator <(const node &a) const{ 7 return x<a.x; 8 } 9 };10 typedef multiset<node>::iterator It;11 int m,l,r,v,x,y;12 ll c;13 char s[20];14 multiset<node> q;15 int calc(int x,int y,int v){ return x+(y-x)/v*v; }16 void solve_c(){17 scanf("%d%d%d",&x,&y,&v);18 y=calc(x,y,v);19 It point=q.upper_bound((node){x,0,0});20 if (point!=q.begin()){21 point--;22 int st=point->x,ed=point->y;23 if (st<x&&ed>y){24 int stp=point->v;25 q.erase(point);26 q.insert((node){st,calc(st,x,stp),stp});27 if (st!=ed)28 q.insert((node){calc(st,x,stp)+stp,ed,stp});29 }30 }31 q.insert((node){x,y,v});32 }33 void solve_d(){34 scanf("%d%d",&x,&y);35 It point=q.lower_bound((node){x,0,0});36 if (point!=q.begin()){37 point--;38 int st=point->x,ed=point->y;39 if (st<x&&ed>=x){40 int stp=point->v;41 q.erase(point);42 q.insert((node){st,calc(st,x-1,stp),stp});43 if (ed>y)44 q.insert((node){calc(st,r,stp)+stp,ed,stp});45 }46 }47 point=q.upper_bound((node){y,0,0});48 if (point!=q.begin()){49 point--;50 int st=point->x,ed=point->y;51 if (st<=y&&ed>y){52 int stp=point->v;53 q.erase(point);54 q.insert((node){calc(st,y,stp)+stp,ed,stp});55 }56 }57 q.erase(q.lower_bound((node){x,0,0}),q.upper_bound((node){y,0,0}));58 }59 void solve_q(){60 scanf("%d",&x);61 int d=1e9;62 It point=q.lower_bound((node){x,0,0});63 if (point!=q.end()) d=min(d,point->x-x);64 if (point!=q.begin()){65 point--;66 int st=point->x,ed=point->y;67 if (ed>=x){68 int stp=point->v;69 d=min(d,x-calc(st,x,stp));70 if (st!=ed) d=min(d,calc(st,x,stp)+stp-x);71 } else d=min(d,x-point->y);72 }73 if (d==1e9) puts("0");74 else printf("%lld\n",max(0ll,c-(ll)d*d));75 }76 int main(){77 scanf("%d%lld",&m,&c);78 for (int i=1;i<=m;i++){79 scanf("%s",s); 80 if (s[0]==‘q‘) solve_q(); else81 if (s[0]==‘c‘) solve_c(); else82 if (s[0]==‘d‘) solve_d();83 }84 return 0;85 }
View Code
YYHS-手機訊號