Codeforces 915E. Physical Education Lessons(動態開點線段樹)
阿新 • • 發佈:2019-01-25
lazy first out != cati oot problems 使用 href
E. Physical Education Lessons
題目:一段長度為n的區間初始全為1,每次成段賦值0或1,求每次操作後的區間總和。(n<=1e9,q<=3e5)
題意:用線段樹做的話,沒什麽思維量,主要是空間復雜度的問題。因此采用動態開點的辦法,即需要用到的節點,在使用前分配內存,沒有用到的就虛置。這樣每次操作新增的節點約logn個。則q次修改需要的空間大約是qlogn。但是,在這個數量級上盡可能開的再大一些,防止RE。
#include<bits/stdc++.h> #define dd(x) cout<<#x<<" = "<<x<<" " #define de(x) cout<<#x<<" = "<<x<<"\n" #define sz(x) int(x.size()) #define All(x) x.begin(),x.end() #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> P; typedef priority_queue<int> BQ; typedef priority_queue<int,vector<int>,greater<int> > SQ; const int maxn=1e7+5e6+10,mod=1e9+7,INF=0x3f3f3f3f; int sum[maxn],ls[maxn],rs[maxn],lazy[maxn],id,root; void push_dw(int l,int r,int rt) { if (lazy[rt]!=-1) { if (!ls[rt]) ls[rt]=++id; if (!rs[rt]) rs[rt]=++id; int lson=ls[rt],rson=rs[rt]; int m=(l+r)>>1; lazy[lson]=lazy[rson]=lazy[rt]; sum[lson]=lazy[rt]*(m-l+1); sum[rson]=lazy[rt]*(r-m); lazy[rt]=-1; } } void upd(int L,int R,int c,int l,int r,int& rt) { if (!rt) rt=++id; if (L<=l&&r<=R) { lazy[rt]=c; sum[rt]=(r-l+1)*c; return; } push_dw(l,r,rt); int m=(l+r)>>1; if (L<=m) upd(L,R,c,l,m,ls[rt]); if (R>m) upd(L,R,c,m+1,r,rs[rt]); sum[rt]=sum[ls[rt]]+sum[rs[rt]]; } int main() { int n,q; cin>>n>>q; memset(lazy,-1,sizeof(lazy)); for (int i=0;i<q;++i) { int l,r,ty; scanf("%d%d%d",&l,&r,&ty); upd(l,r,ty==1?1:0,1,n,root); printf("%d\n",n-sum[1]); } return 0; }
Codeforces 915E. Physical Education Lessons(動態開點線段樹)