ODT 死循环求条
查看原帖
ODT 死循环求条
826774
Little_Fox_Fairy楼主2024/7/30 21:11
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=2e5+10;
struct node
{
	int l,r;
	mutable int val;
	node (int l,int r=-1,int val=0) : l (l),r (r),val (val) {}
	bool operator <(const node &x) const {return l<x.l;}
};
set<node> s;
struct Chtholly_Tree
{
	inline auto split(int pos)
	{
		auto it=s.lower_bound(node(pos));
		if (it!=s.end() and it->l==pos) return it;
		it--;
		if (it->r<pos) return s.end();
		int l=it->l,r=it->r,val=it->val;
		s.erase(it);
		s.insert(node(l,pos-1,val));
		return s.insert(node(pos,r,val)).first;
	}
	inline void assign(int l,int r,int val)
	{
		auto itr=split(r+1),itl=split(l);
		s.erase(itl,itr);
		auto tmp=s.insert(node(l,r,val)).first;
		if (--tmp!=s.begin())
		{
			auto tmp1=tmp;tmp++;
			if (tmp->val==tmp1->val) assign(tmp1->l,tmp->r,tmp->val);
		}
		else tmp++;
		if (++tmp!=s.end())
		{
			auto tmp2=tmp;tmp--;
			if (tmp->val==tmp2->val) assign(tmp->l,tmp2->r,tmp->val);
		}
		else tmp--;
		return ;
	}
	inline int query(int l,int r,int x)
	{
		for (auto it=s.begin();it!=s.end();it++)
		{
			int itl=it->l,itr=it->r;
//			cout<<itl<<" "<<itr<<" "<<it->val<<endl;
			if (itr-itl+1>=x and (it->val)==0)
			{
				if (itr-itl+1==x)
				{
					it->val=1;
					it--;auto pos=it;
					it++;
					if (pos->val==it->val) assign(pos->l,it->r,it->val);
					return itl;
				}
				s.erase(it);
				auto tmp=s.insert(node(itl,itl+x-1,1)).first;
				tmp--;auto pos=tmp;tmp++;
				if (pos->val==tmp->val) assign(pos->l,tmp->r,tmp->val);
				s.insert(node(itl+x,itr,0));
				return itl;
			}
		}
		return 0;
	}
}tr;
int n,m,e[N];
signed main()
{
	cin>>n>>m;
	s.insert(node(1,n,0));
	while (m--)
	{
		int op,l,r;
		cin>>op>>l;
		if (op==1) tr.query(1,n,l);
		else cin>>r,tr.assign(l,l+r-1,0);
	}
	return (0-0);
}
2024/7/30 21:11
加载中...