关于线段树的数组大小
查看原帖
关于线段树的数组大小
172370
fzj2007楼主2020/7/15 17:15

RT,不是 200005×4200005\times4 左右吗?然后就RE了,数组开大一点就AC。求助

//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std; 
using std::cin;
using std::cout;
using std::endl;
namespace IN{
    const int MAX_INPUT = 1000000;
    #define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
    char buf[MAX_INPUT],*p1,*p2;
    template<typename T>inline bool read(T &x) {
        static std::streambuf *inbuf=cin.rdbuf();
        x=0;
        register int f=0,flag=false;
        register char ch=getc();
        while(!isdigit(ch)){
            if (ch=='-') f=1;
        	ch=getc();
        }
        if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
        while(isdigit(ch)) {
            x=x*10+ch-48;
            ch=getc();
        }
        x=f?-x:x;
        return flag;
    }
    template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
       return read(a)&&read(args...);
    }
    inline int getch(){
    	static std::streambuf *inbuf=cin.rdbuf();
    	char ch=getc();
    	while(ch!='0'&&ch!='1') ch=getc();
    	return (ch^'0');
	}
    #undef getc
}

namespace OUT{
    template<typename T>inline void put(T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top=0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc('\n');
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc('\n');
    }
    inline void putc(const char ch){
        static std::streambuf *outbuf=cout.rdbuf();
        outbuf->sputc(ch);
    }
    inline void putstr(string s){
    	for(register int i=0;i<s.length();i++) putc(s[i]);
	}
    template<typename T>inline void put(const char ch,T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top = 0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc(ch);
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc(ch);
    }
    template<typename T,typename ...Args> inline void put(T a,Args ...args){
        put(a);put(args...);
    }
    template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
        put(ch,a);put(ch,args...);
    }
}
using namespace IN;
using namespace OUT;
struct tree{
	long long val,tag;
}t[200005<<2];//就这里,这样RE,开成200005<<3就A了
#define lc(x) (x<<1)
#define rc(x) (x<<1|1)
#define mid ((l+r)>>1)
int n,m,opt,x,y;
inline void push_up(int rt){
	t[rt].val=t[lc(rt)].val+t[rc(rt)].val;
}
inline void push_down(int rt,int len){
	if(t[rt].tag){
		t[lc(rt)].val=(len-(len>>1))-t[lc(rt)].val;
		t[rc(rt)].val=(len>>1)-t[rc(rt)].val;
		t[lc(rt)].tag^=1;
		t[rc(rt)].tag^=1;
		t[rt].tag=0;
	}
}
void build(int l,int r,int rt){
	if(l==r) return t[rt].val=getch(),void();
	build(l,mid,lc(rt)),build(mid+1,r,rc(rt)),push_up(rt);
}
/*
void debug(int l,int r,int rt){
	if(l==r) return;
	push_down(rt,(r-l+1));
	debug(l,mid,lc(rt)),debug(mid+1,r,rc(rt));
	cout<<l<<" "<<r<<" "<<rt<<" "<<t[rt].val<<endl;
}
*/
void update(int l,int r,int rt,int cl,int cr){
	push_down(rt,(r-l+1));
	if(cl<=l&&r<=cr) return t[rt].tag^=1,t[rt].val=(r-l+1)-t[rt].val,void();
	if(cl<=mid) update(l,mid,lc(rt),cl,cr);
	if(cr>mid) update(mid+1,r,rc(rt),cl,cr);
	push_up(rt); 
}
long long query(int l,int r,int rt,int ql,int qr){
	if(ql<=l&&r<=qr) return t[rt].val;
	push_down(rt,r-l+1);
	long long ans=0;
	if(ql<=mid) ans+=query(l,mid,lc(rt),ql,qr);
	if(qr>mid) ans+=query(mid+1,r,rc(rt),ql,qr);
	return ans;
}
/*#define deb*/
int main(int argc, char const *argv[]){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read(n,m);
    build(1,n,1);
    while(m--){
    	read(opt,x,y);
    	if(opt) put('\n',query(1,n,1,x,y));
    	else update(1,n,1,x,y);
    	#ifdef deb
    	debug(1,n,1);
    	system("pause");
    	#endif
	}
    return 0;
}
2020/7/15 17:15
加载中...