Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:03+00:00 2026-05-23T12:13:03+00:00

The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree’s with lazy propagation. I

  • 0

The following is the implementation of http://www.spoj.pl/problems/LITE/ using Segment Tree’s with lazy propagation. I am new to segment trees and I cannot understand why I am getting TLE. Could someone please look at it and help me correct my error?

#include <iostream>
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 100000
using namespace std;
int M[2*MAX+1];
int flag[2*MAX+1];
int count;
void refresh(int begin,int end,int n)
{
    M[n] = end-begin+1 - M[n];
    flag[n]=0;
    flag[n*2] =!flag[n*2];
    flag[n*2+1] =!flag[n*2+1];
}
void update(int begin,int end,int i,int j,int n=1)
{
    if(flag[n])
    {
        refresh(begin,end,n);
    }
    if(begin>=i && end<=j)
    {
        if(!flag[n])
        {
            refresh(begin,end,n);
        }
        flag[n] = 0;
        return;
    }
    else if(begin>=end)
    {
        return;
    }
    else
    {
        int mid = (begin+end)>>1;
        if(i<=mid)
        {
            update(begin,mid,i,j,n*2);
        }
        if(j>mid)
        {
            update(mid+1,end,i,j,n*2+1);
        }
        if(flag[2*n])
        {
            refresh(begin,mid,2*n);
        }
        if(flag[2*n+1])
        {
            refresh(mid+1,end,2*n+1);
        }
        M[n] = M[n*2]+ M[n*2+1];
    }
}
int query(int begin,int end,int i,int j,int n=1)
{
    if(flag[n])
    {
        refresh(begin,end,n);
    }
    if(begin>=i && end<=j)
    {
        return M[n];
    }
    if(begin>=end)
    {
        return 0;
    }
    int mid = (begin+end)>>1;
    int l=0,r=0;
    if(i<=mid)
    {
        l = query(begin,mid,i,j,n*2);
    }
    if(j>mid)
    {
        r = query(mid+1,end,i,j,n*2+1);
    }
    if(flag[2*n])
    {
        refresh(begin,mid,2*n);
    }
    if(flag[2*n+1])
    {
        refresh(mid+1,end,2*n+1);
    }
    M[n] = M[n*2]+ M[n*2+1];
    return l+r;
}
int main()
{
    memset(M,0,sizeof M);
    int n,m,a,b,c;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        if(a==0)
        {
            update(1,n,b,c);
        }
        else
        {
            printf("%d\n",query(1,n,b,c));
        }
    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:13:03+00:00Added an answer on May 23, 2026 at 12:13 pm

    M[node]^=1; might be faster than M[node] = (M[node]==0)?1:0;, and (begin+end)>>1 faster than (begin/end)/2, but not very relevant

    LE: Try if making the recursive functions inline will run faster. I think it unravels the recursion a couple of times and works a little bit faster. Maybe sending the parameters as references will make it run faster, try that out. If the test cases are chosen properly you still shouldn’t be able to pass the tests with this trickery, but it helps sometimes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Testing the Javascript Implementation of MD5 here: http://www.webtoolkit.info/javascript-md5.html gives the following output: MD5(muzaaya) =
I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a
The following XML implementation seems to work fine: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent
Take the following naive implementation of a nested async loop using the ThreadPool: ThreadPool.SetMaxThreads(10,
I'm new to iOS programming and I'm reading this tutorial to get along http://www.raywenderlich.com/1797/how-to-create-a-simple-iphone-app-tutorial-part-1
I am getting error in APNS implementation I used code from here http://www.easyapns.com/apple-delegate I
I'm following this tutorial for the login and registration http://www.raywenderlich.com/13511/how-to-create-an-app-like-instagram-with-a-web-service-backend-part-12 . When I click
I am trying to implement following look: http://www.primefaces.org/showcase/ui/selectManyCheckbox.jsf Notice there is no css-borders around
Well, I downloaded Rijndael sources from here: http://www.codeproject.com/Articles/1380/A-C-Implementation-of-the-Rijndael-Encryption-Decr I have the following code: int
The created the following web application: http://www.web-allbum.com/ I also added it to the Chrome

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.