题意:直角边为n的网格巧克力,一格为一块,选择斜边上一点,从左或上吃,直到吃到空气,称为一次操作。给出几个操作,问各能吃几块。如果x是当前要吃的横坐标,在已经吃过的中找x1>=x的第一个x1,即lower_bound()。如果x1==x,结果就是0;否则假设x是往上吃,x1也是往上吃,因为x和x1间没有往左吃的,因此x1结束的地方也是x结束的地方。如果x往上吃,x1往左吃,因为x1横坐标最小,纵坐标最大,对x能吃到的位置仅受到它的影响。
乱码:
//#pragma comment(linker,"/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #include <stack> #include <list> using namespace std; const int SZ=1000010,INF=0x7FFFFFFF; typedef long long lon; const double EPS=1e-9; struct nd{ char drc; int res; int x,y; nd(char a,int b,int _x,int _y):drc(a),res(b),x(_x),y(_y){} }; int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); int n,m; cin>>n>>m; map<int,nd> mp; for(int i=0;i<m;++i) { int x,y; char drc; cin>>x>>y>>drc; if(i==0) { int res=0; if(drc=='U') { res=y; } else res=x; cout<<res<<endl; mp.insert(make_pair(x,nd(drc,res,x,y))); } else { int res=0; if(drc=='U') { auto it=mp.lower_bound(x); if(it->first==x)res=0; else if(it==mp.end())res=y; else if(it->second.drc=='U') { res=y-it->second.y+it->second.res; } else { res=y-it->second.y; } } else { auto it=mp.lower_bound(x); if(it==mp.end()) { --it; if(it->second.drc=='U')res=x-it->second.x; else res=x-it->second.x+it->second.res; } else if(it->first!=x) { if(it==mp.begin())res=x; else { --it; if(it->second.drc=='U')res=x-it->second.x; else res=x-it->second.x+it->second.res; } } else {res=0; // if(it->second.drc=='U')res=x-it->second.x; // else res=x-it->second.x+it->second.res; } } mp.insert(make_pair(x,nd(drc,res,x,y))); cout<<res<<endl; } } return 0; }