TLE#11求卡常
查看原帖
TLE#11求卡常
754502
_AyachiNene楼主2023/8/11 17:04
#include<bits/stdc++.h>
#define int long long
/* --------------- fast io --------------- */ // begin
namespace Fread {
  const int SIZE = 1 << 21;
  char buf[SIZE], *S, *T;
  inline char getchar() {
    if (S == T) {
      T = (S = buf) + fread(buf, 1, SIZE, stdin);
      if (S == T) return EOF;
    }
    return *S++;
  }
} // namespace Fread
namespace Fwrite {
  const int SIZE = 1 << 21;
  char buf[SIZE], *S = buf, *T = buf + SIZE;
  inline void flush() {
    fwrite(buf, 1, S - buf, stdout);
    S = buf;
  }
  inline void putchar(char c) {
    *S++ = c;
    if (S == T) flush();
  }
  struct NTR {
    ~ NTR() { flush(); }
  }ztr;
} // namespace Fwrite
#define getchar Fread :: getchar
#define putchar Fwrite :: putchar
namespace Fastio {
  struct Reader {
    template<typename T>
    Reader& operator >> (T& x) {
      char c = getchar();
      T f = 1;
      while (c < '0' || c > '9') {
	if (c == '-') f = -1;
	c = getchar();
      }
      x = 0;
      while (c >= '0' && c <= '9') {
	x = x * 10 + (c - '0');
	c = getchar();
      }
      x *= f;
      return *this;
    }
    Reader& operator >> (char& c) {
      c = getchar();
      while (c == '\n' || c == ' ') c = getchar();
      return *this;
    }
    Reader& operator >> (char* str) {
      int len = 0;
      char c = getchar();
      while (c == '\n' || c == ' ') c = getchar();
      while (c != '\n' && c != ' ') {
	str[len++] = c;
	c = getchar();
      }
      str[len] = '\0';
      return *this;
    }
    Reader(){}
  }cin;
  const char endl = '\n';
  struct Writer {
    template<typename T>
    Writer& operator << (T x) {
      if (x == 0) { putchar('0'); return *this; }
      if (x < 0) { putchar('-'); x = -x; }
      static int sta[45];
      int top = 0;
      while (x) { sta[++top] = x % 10; x /= 10; }
      while (top) { putchar(sta[top] + '0'); --top; }
      return *this;
    }
    Writer& operator << (char c) {
      putchar(c);
      return *this;
    }
    Writer& operator << (char* str) {
      int cur = 0;
      while (str[cur]) putchar(str[cur++]);
      return *this;
    }
    Writer& operator << (const char* str) {
      int cur = 0;
      while (str[cur]) putchar(str[cur++]);
      return *this;
    }
    Writer(){}
  }cout;
} // namespace Fastio
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
/* --------------- fast io --------------- */ // end
using namespace std;
struct node
{
	int l,r,id;
}q[114514];
int n,m,k;
int size,belong[114514];
int ans[114514],l=1,r,now;
int a[114514],t[114514];
int sum1[114514],sum2[114514];
int val[114514];
map<int,int>cnt;
bool cmp(node x,node y)
{
	if(belong[x.l]==belong[y.l])
		return x.r<y.r;
	return belong[x.l]<belong[y.l];
}
void add(int x,int op)
{
	int y=k;
	if(op)
		y=-y;
	now+=cnt[val[x]+y];
	++cnt[val[x]];
}
void del(int x,int op)
{
	int y=k;
	if(op)
		y=-y;
	--cnt[val[x]];
	now-=cnt[val[x]+y];
}
signed main()
{
	cin>>n>>k;
	size=sqrt(n);
	for(int i=1;i<=n;i++)
		cin>>t[i];
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		sum1[i]=sum1[i-1]+(t[i]==1)*a[i];
		sum2[i]=sum2[i-1]+(t[i]==2)*a[i];
		val[i]=sum1[i]-sum2[i];
	}
	for(int i=1;i<=n;i++)
		belong[i]=i/size+1;
	cin>>m;
	for(int i=1;i<=m;i++)
		cin>>q[i].l>>q[i].r,q[i].id=i,q[i].l--;
	sort(q+1,q+m+1,cmp);
	for(int i=1;i<=m;i++)
	{
		while(l<q[i].l)
			del(l++,0);
		while(l>q[i].l)
			add(--l,0);
		while(r<q[i].r)
			add(++r,1);
		while(r>q[i].r)
			del(r--,1);
		ans[q[i].id]=now;
	}
	for(int i=1;i<=m;i++)
		cout<<ans[i]<<endl;
}
2023/8/11 17:04
加载中...