求卡常+卡空间
查看原帖
求卡常+卡空间
377873
EricWan楼主2024/8/18 13:29

我尝试使用一种新的方法做这个题,但是 1TLE+nMLE

没有格式化,很多无用代码,但问题不大。

// Problem: P4117 [Ynoi2018] 五彩斑斓的世界
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4117
// Memory Limit: 64 MB
// Time Limit: 7500 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
//#include <bits/extc++.h>
#define min(x,y) (((x)<(y))?(x):(y))
#define max(x,y) (((x)>(y))?(x):(y))
#define lowbit(x) ((x)&-(x))
#define abs(x) (((x)<(0))?(-(x)):(x))
#define swap(a,b) a^=b^=a^=b
#define INF 1e18
#define MAXA 100005
#define MAXQ 500005
#define MAXN 1000005
using namespace std;
//using namespace __gnu_cxx;
//using namespace __gnu_pbds;
namespace fast_io {
	#define MAX_OUTPUT 10000000
    char ch,a[MAX_OUTPUT],*p=a,*l=a,sep=' ';//a:buffer[MAXN]ea of speed output p:the head of the queue l:the tail of the queue sep:the sperator of vector output
	#define def_scan_int(A) inline void scan(A&x){x=0;do{ch=getchar();}while(ch<48||ch>57);while(ch>=48&&ch<=57){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}}	//scan integer
	#if 0	//0:normal output 1:speed output(need flush() after output)
	#define printc(A) *(p++)=A	//speed char output
	#define prints(A,B) memcpy(p,A,B)	//speed char[] output
	#else
	#define printc(A) putchar(A)	//normal char output
	#define prints(A,B) for(int i=0;i<B;i=-~i){putchar(A[i]);}	//speed char[] output
	#endif
	#define def_print_int(A) void print(const A x){if(9<x){print(x/10);}printc(x%10|48);}	//print integer
    def_scan_int(short);	//scan short
    def_scan_int(signed);	//scan int
    def_scan_int(long long);	//scan long long
    def_scan_int(__int128);	//scan __int128
    inline void scan(char&x){do{x=getchar();}while(x<=32);}	//scan char
    inline void scan(char*x){do{ch=getchar();}while(ch<=32);while(ch>32){*(x++)=ch;ch=getchar();}}	//scan char*
    inline void scan(string&x){do{ch=getchar();}while(ch<=32);while(ch>32){x.push_back(ch);ch=getchar();}}	//scan string
    template<typename T>
    inline void scan(vector<T>&x){for(auto&it:x){scan(it);}}	//scan vector<T>
    inline void read(){}	//read(a)={scan(a);read();}
    template<typename T,typename...arg>
    inline void read(T&x,arg&...oth){scan(x);read(oth...);}	//read(a,b,c,d,e)={scan(a);read(b,c,d,e);}
    def_print_int(short);	//print short
    def_print_int(signed);	//print int
    def_print_int(long long);	//print long long
    def_print_int(__int128);	//print __int128
    inline void print(const char x){printc(x);}	//print char
    inline void print(const char*x){prints(x,strlen(x));}	//print char*
    inline void print(const string x){prints(x,x.size());}	//print string
    template<typename T>
    inline void print(const vector<T>x){for(auto&it:x){print(it);putchar(sep);}}	//print vector<T>
    // inline void write(){}	//write(a)={print(a);write();}
    template<typename T,typename...arg>
    // inline void write(const T x,const[MAXN]g...oth){print(x);write(oth...);}	//write(a,b,c,d,e)={print(a);write(b,c,d,e);}
    inline void flush(){fwrite(l,1,p-l,stdout);l=p;}	//flush the queue to the console,clear the queue
}
#define read fast_io::read
// #define write fast_io::write
#define sep fast_io::sep
#define flush fast_io::flush
void write(const int i)
{
	if (!i)
	{
		return;
	}
	write(i / 10);
	putchar('0' + i % 10);
}
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int lcm(int a, int b) {
	return a * b / gcd(a, b);
}
int exgcd(int a, int b, int &x, int &y) {
	if (b == 0) {
		x = 1;
		y = 0;
		return a;
	}
	int ans = exgcd(b, a % b, x, y);
	int X = y;
	int Y = x - (a / b) * y;
	x = X;
	y = Y;
	return ans;
}
int quickpower(int a, int b) {
//	if (b < 0)
//	{
//		return quickpower(a, b + mod - 1);
//	}
	int c = 1;
	while (b) {
		if (b & 1) {
			c *= a;
//			c %= mod;
		}
		a *= a;
//		a %= mod;
		b >>= 1;
	}
	return c;
}
int quickpower(int a, int b, int mod) {
//	if (b < 0)
//	{
//		return quickpower(a, b + mod - 1);
//	}
	a %= mod;
	int c = 1;
	while (b) {
		if (b & 1) {
			c *= a;
			c %= mod;
		}
		a *= a;
		a %= mod;
		b >>= 1;
	}
	return c;
}
int countbit(int x) {
	int ans = 0;
	while (x) {
		ans += (x & 1);
		x >>= 1;
	}
	return ans;
}
int log2_(int k)
{
	int l = 0, r = 62;
	while (l < r)
	{
		if ((1ll << (l + r + 1 >> 1)) > k)
		{
			r = (l + r + 1 >> 1) - 1;
		}
		else
		{
			l = (l + r + 1 >> 1);
		}
	}
	return l;
}
struct operation
{
	int op, l, r, x, id;
};
inline operation MOP(int &op, int &l, int &r, int &x, int &id)
{
	operation ans;
	ans.op = op;
	ans.l = l;
	ans.r = r;
	ans.x = x;
	ans.id = id;
	return ans;
}
int n, q, a[MAXN], N, boxl, boxr, op[MAXQ], l[MAXQ], r[MAXQ], x[MAXQ], ans[MAXQ], L = 2500;
// vector<operation> OP;
unordered_set<int> box[MAXA];
signed main()
{
	read(n, q);
	for (register int i = 1; i <= n; i=-~i)
	{
		read(a[i]);
	}
	N = (n + L - 1) / L * L;
	for (register int i = 1; i <= q; i=-~i)
	{
		read(op[i], l[i], r[i], x[i]);
		// for (int kl = 1, kr = L, ki = 1; kr <= N; ki++, kl += L, kr += L)
		// {
			// if (l[i] <= kr && r[i] >= kl)
			// {
				// OP[ki].push_back(MOP(op[i], max(l[i], kl), min(r[i], kr), x[i], i));
			// }
		// }
	}
	for (register int kl = 1, kr = L, ki = 1; kr <= N; ki++, kl += L, kr += L)
	{
		// OP.clear();
		// for (int i = 1; i <= q; i=-~i)
		// {
			// if (l[i] <= kr && r[i] >= kl)
			// {
				// OP.push_back(MOP(op[i], max(l[i], kl), min(r[i], kr), x[i], i));
			// }
		// }
// 		for (int i = 0; i <= 100001; i=-~i)
// 		{
// 			box[i].clear();
// 		}
		for (register int i = kl; i <= kr; i=-~i)
		{
	// cout << 1 << " " << a[i] << endl;
			box[a[i]].insert(i);
	// cout << 2 << endl;
			boxr = max(boxr, a[i]);
	// cout << 3 << endl;
		}
		boxl = 0;
		// if (ki == 2)
		// {
			// cout << "begin: boxl = " << boxl << " boxr = " << boxr << endl;
			// for (int j = boxl; j <= boxr; j=-~j)
			// {
				// if (box[j].empty())
				// {
					// continue;
				// }
				// cout << j << ": ";
				// for (int k : box[j])
				// {
					// cout << k << " ";
				// }
				// cout << endl;
			// }
		// }
		for (register int I = 1; I <= q; I=-~I)
		{
			register operation i;
			if (l[I] <= kr && r[I] >= kl)
			{
				 i = MOP(op[I], max(l[I], kl), min(r[I], kr), x[I], I);
			}
			else
			{
				continue;
			}
			if (i.op == 2)
			{
				if (i.l != kl || i.r != kr)
				{
					for (int j = i.l; j <= i.r; j=-~j)
					{
						if (a[j] == i.x + boxl)
						{
							ans[i.id]++;
						}
					}
				}
				else if (i.x + boxl <= boxr)
				{
					ans[i.id] += box[i.x + boxl].size();
				}
				// if (i.id == 23)
				// {
					// cout << "i.id = " << i.id << ": boxl = " << boxl << " boxr = " << boxr << endl;
					// for (int j = boxl; j <= boxr; j=-~j)
					// {
						// if (box[j].empty())
						// {
							// continue;
						// }
						// cout << j << ": ";
						// for (int k : box[j])
						// {
							// cout << k << " ";
						// }
						// cout << endl;
					// }
					// cout << i.l << " " << i.r << " " << i.x << " " << boxl << " " << boxr << " " << box[i.x + boxl].size() << endl;
				// }
				// if (ki == 2)
				// {
					// cout << "i.id = " << i.id << ": boxl = " << boxl << " boxr = " << boxr << endl;
					// for (int j = boxl; j <= boxr; j=-~j)
					// {
						// if (box[j].empty())
						// {
							// continue;
						// }
						// cout << j << ": ";
						// for (int k : box[j])
						// {
							// cout << k << " ";
						// }
						// cout << endl;
					// }
				// }
			}
			else
			{
				if (i.l != kl || i.r != kr)
				{
					for (register int j = i.l; j <= i.r; j=-~j)
					{
						if (a[j] - boxl > i.x)
						{
							// if (box[a[j]].count(j) == 0)
							// {
								// cout << "i.id = " << i.id << ": boxl = " << boxl << " boxr = " << boxr << endl;
								// for (register int j = 0; j <= 100; j=-~j)
								// {
									// if (box[j].empty())
									// {
										// continue;
									// }
									// cout << j << ": ";
									// for (register int k : box[j])
									// {
										// cout << k << " ";
									// }
									// cout << endl;
								// }
								// cout << ki << " " << i.id << " " << j << " " << a[j] << endl;
								// exit(0);
							// }
							box[a[j]].erase(j);
							a[j] -= i.x;
							box[a[j]].insert(j);
						}
					}
				}
				else
				{
					if (boxr - boxl <= i.x || i.x == 0)
					{
						continue;
					}
					const int unmove = i.x + 1, move = boxr - boxl - i.x;
					if (unmove > move)
					{
						for (register int j = boxl + i.x + 1; j <= boxr; j=-~j)
						{
							for (register int k : box[j])
							{
								a[k] -= i.x;
								box[a[k]].insert(k);
							}
							box[j].clear();
						}
						boxr = max(boxr - i.x, boxl + i.x);
					}
					else
					{
						for (register int j = boxl + i.x; j >= boxl; j=~-j)
						{
							for (register int k : box[j])
							{
								a[k] += i.x;
								box[a[k]].insert(k);
							}
							box[j].clear();
						}
						boxl = min(boxr - i.x, boxl + i.x);
					}
				}
				// if (ki == 2)
				// {
					// cout << "i.id = " << i.id << ": boxl = " << boxl << " boxr = " << boxr << endl;
					// for (int j = boxl; j <= boxr; j=-~j)
					// {
						// if (box[j].empty())
						// {
							// continue;
						// }
						// cout << j << ": ";
						// for (int k : box[j])
						// {
							// cout << k << " ";
						// }
						// cout << endl;
					// }
					// for (int j = kl; j <= kr; j=-~j)
					// {
						// cout << a[j] << " ";
					// }
					// cout << endl;
				// }
			}
			// if (ki == 1 && box[0].size() == 0)
			// {
				// cout << i.id << "!!!!!!!!!!!!!!!\n";
			// }
		}
		for (register int i = boxl; i <= boxr; i=-~i)
		{
			box[i].clear();
		}
		// cout << "ans[4] = " << ans[4] << endl;
	}
	for (register int i = 1; i <= q; i=-~i)
	{
		if (op[i] == 2)
		{
			if (ans[i] == 0)
			{
				putchar('0');
			}
			else
			write(ans[i]);
			putchar('\n');
		}
	}
	return 0;
}
2024/8/18 13:29
加载中...