提出 #46093739


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//#define M
//#define mo
#define N 500010
int n, m, i, j, k, T;
int rt, q, op, l, r; 
char s[N]; 

struct Segment_tree {
	int tot, ls[N<<2], rs[N<<2]; 
	struct node {
		int mx, s, L, R; 
		void init() { mx=s=L=R=1; }
	}a1[N<<2], a0[N<<2];
	int tag[N<<2]; 
	void push_up(node &k, node l, node r) {
		k.mx=max(max(l.mx, r.mx), l.R+r.L); 
		k.s=((l.s && r.s) ? l.s+r.s : 0); 
		k.L=(l.s ? l.s+r.L : l.L); 
		k.R=(r.s ? r.s+l.R : r.R); 
	}
	void print(int k, int l, int r) {
		printf("%d : [%d %d]\n", k, l, r); 
		printf("0 : %d %d %d %d\n", a0[k].mx, a0[k].s, a0[k].L, a0[k].R); 
		printf("1 : %d %d %d %d\n\n", a1[k].mx, a1[k].s, a1[k].L, a1[k].R); 
	}
	void build(int &k, int l, int r) {
		if(!k) k=++tot; 
		if(l==r) {
			if(s[l]=='1') a1[k].init(); 
			else a0[k].init(); 
			return ; 
		}
		int mid=(l+r)>>1; 
		build(ls[k], l, mid); build(rs[k], mid+1, r); 
		push_up(a0[k], a0[ls[k]], a0[rs[k]]); 
		push_up(a1[k], a1[ls[k]], a1[rs[k]]); 
//		printf("%d : [%d %d]\n", k, l, r); 
//		printf("0 : %d %d %d %d\n", a0[k].mx, a0[k].s, a0[k].L, a0[k].R); 
//		printf("1 : %d %d %d %d\n", a1[k].mx, a1[k].s, a1[k].L, a1[k].R); 
	}
	void gai(int k) {
		tag[ls[k]]^=1; tag[rs[k]]^=1; 
		swap(a0[k], a1[k]); 
//		printf("Then : "); print(k, 0, 0); 
	}
	void push_down(int k) {
		if(tag[k]) gai(k), tag[k]=0; 
	}
	void modify(int k, int l, int r, int x, int y) {
//		push_down(k); 
//		printf("Before : "); print(k, l, r); 
//		printf("Before : %d : [%d %d]\n", k, l, r); 
//		printf("0 : %d %d %d %d\n", a0[k].mx, a0[k].s, a0[k].L, a0[k].R); 
//		printf("1 : %d %d %d %d\n\n", a1[k].mx, a1[k].s, a1[k].L, a1[k].R); 
		if(l>=x && r<=y) return gai(k), void(); 
		int mid=(l+r)>>1; 
		push_down(ls[k]); push_down(rs[k]); 
		if(x<=mid) modify(ls[k], l, mid, x, y); 
		if(y>=mid+1) modify(rs[k], mid+1, r, x, y); 
		push_up(a0[k], a0[ls[k]], a0[rs[k]]); 
		push_up(a1[k], a1[ls[k]], a1[rs[k]]); 
		
//		if(ls[k]==7) print(7, 3, 3); 
		
//		printf("%d : [%d %d]\n", k, l, r); 
//		printf("0 : %d %d %d %d\n", a0[k].mx, a0[k].s, a0[k].L, a0[k].R); 
//		printf("1 : %d %d %d %d\n\n", a1[k].mx, a1[k].s, a1[k].L, a1[k].R); 
	}
	node que(int k, int l, int r, int x, int y) {
		push_down(k); 
		if(l>=x && r<=y) return a1[k]; 
		int mid=(l+r)>>1, flg=0; 
		node s1, s2, s3; 
				push_down(ls[k]); push_down(rs[k]); 

		if(x<=mid) s1=que(ls[k], l, mid, x, y), flg++; 
		if(y>=mid+1) s2=que(rs[k], mid+1, r, x, y), flg+=2; 
		push_up(a0[k], a0[ls[k]], a0[rs[k]]); 
		push_up(a1[k], a1[ls[k]], a1[rs[k]]); 
		if(flg==1) return s1; if(flg==2) return s2; 
		push_up(s3, s1, s2); return s3; 
	}
}Seg;

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
//	srand(time(NULL));
//	T=read();
//	while(T--) {
//
//	}
	n=read(); q=read(); 
	scanf("%s", s+1); Seg.build(rt, 1, n); 
//	printf("%d\n", Seg.a1[1].mx); 

	while(q--) {
		op=read(); l=read(); r=read(); 
		if(op==1) Seg.modify(1, 1, n, l, r); 
		else printf("%d\n", Seg.que(1, 1, n, l, r).mx); 
	}
	return 0;
}

提出情報

提出日時
問題 F - Vacation Query
ユーザ zhangtingxi
言語 C++ 20 (gcc 12.2)
得点 550
コード長 3380 Byte
結果 AC
実行時間 151 ms
メモリ 47276 KiB

コンパイルエラー

Main.cpp: In member function ‘Segment_tree::node Segment_tree::que(int, int, int, int, int)’:
Main.cpp:89:17: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
   89 |                 if(flg==1) return s1; if(flg==2) return s2;
      |                 ^~
Main.cpp:89:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
   89 |                 if(flg==1) return s1; if(flg==2) return s2;
      |                                       ^~
Main.cpp: In function ‘int main()’:
Main.cpp:104:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  104 |         scanf("%s", s+1); Seg.build(rt, 1, n);
      |         ~~~~~^~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 550 / 550
結果
AC × 1
AC × 17
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 03_min_00.txt, 04_max_00.txt, 05_corner_00.txt, 05_corner_01.txt, 05_corner_02.txt, 05_corner_03.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3652 KiB
01_small_00.txt AC 12 ms 3652 KiB
01_small_01.txt AC 14 ms 3620 KiB
01_small_02.txt AC 1 ms 3968 KiB
01_small_03.txt AC 1 ms 4024 KiB
01_small_04.txt AC 1 ms 3848 KiB
02_random_00.txt AC 151 ms 47272 KiB
02_random_01.txt AC 121 ms 32132 KiB
02_random_02.txt AC 148 ms 47100 KiB
02_random_03.txt AC 91 ms 31044 KiB
02_random_04.txt AC 148 ms 47276 KiB
03_min_00.txt AC 7 ms 3720 KiB
04_max_00.txt AC 32 ms 43160 KiB
05_corner_00.txt AC 80 ms 43356 KiB
05_corner_01.txt AC 79 ms 43292 KiB
05_corner_02.txt AC 73 ms 43212 KiB
05_corner_03.txt AC 45 ms 43212 KiB