线段树套线段树模板题
链接:http://poj.org/problem?id=2155
题解:
代码:
#include <bits/stdc++.h> using namespace std; int n,m,k; char c; #define maxn 1111 #define mid (p[x].h+p[x].t)/2 struct xds { int h,t; struct re { int h,t,x,rev; }p[maxn*4]; void down(int x) { if (!p[x].rev) return; if (p[x].h!=p[x].t) { p[x*2].rev^=1; p[x*2+1].rev^=1; } else { p[x].x^=1; } p[x].rev=0; } void build2(int x,int h,int t) { p[x].h=h; p[x].t=t; p[x].x=p[x].rev=0; if (h==t) return; build2(x*2,h,mid); build2(x*2+1,mid+1,t); } void change2(int x,int h,int t) { down(x); if (p[x].h>t||p[x].t<h) return; if (h<=p[x].h&&p[x].t<=t) { p[x].rev^=1; down(x); return; } change2(x*2,h,t); change2(x*2+1,h,t); } int query2(int x,int goal) { down(x); if (p[x].h==p[x].t) return(p[x].x); if (goal>mid) return(query2(x*2+1,goal)); else return(query2(x*2,goal)); } }p[maxn*4]; void build1(int x,int h,int t,int m) { p[x].h=h; p[x].t=t; p[x].build2(1,1,m); if (h==t) return; build1(x*2,h,mid,m); build1(x*2+1,mid+1,t,m); } void change1(int x,int x1,int x2,int y1,int y2) { if (p[x].h>x2||p[x].t<x1) return; if (x1<=p[x].h&&p[x].t<=x2) { p[x].change2(1,y1,y2); return; } change1(x*2,x1,x2,y1,y2); change1(x*2+1,x1,x2,y1,y2); } int query1(int x,int a,int b) { if (p[x].h==p[x].t) { return(p[x].query2(1,b)); } if (a>mid) return(query1(x*2+1,a,b)); else return(query1(x*2,a,b)); } int main() { freopen("noip.in","r",stdin); freopen("noip.out","w",stdout); cin>>k; int a1,b1,a2,b2; for (int o=1;o<=k;o++) { cin>>n>>m; build1(1,1,n,n); for (int i=1;i<=m;i++) { cin>>c; if (c=='C') { cin>>a1>>b1>>a2>>b2; change1(1,a1,a2,b1,b2); } if (c=='Q') { cin>>a1>>b1; cout<<query1(1,a1,b1); } } } }