ruler method

- - Since it's late now, the thief wants to sleep, so first get an incomplete version of 23333












example: topic



Value_Dragon is a rich man. It's almost New Year's Eve, so he's going to give out red envelopes. But the way he gave out red envelopes is very strange. He has n people lined up in a row. Each time a section [l, r] in 1-n is selected and sent, one dollar is given to each person in the interval. In this way, m red envelopes were sent. After sending it, he wants to know how many intervals in the sub-interval of [1,n] satisfy the following requirements

  1. The total amount of money obtained in this interval is not less than s

  2. This interval can be divided into two disjoint subintervals and the sum of money obtained in each subinterval is not less than w

(Note: the sub-interval of an interval includes itself)

Anti-pit reminder, an interval with a length of 1, such as [1,1], cannot be split into two sub-intervals

Input

The first line is an integer T representing the number of groups of data.
Next, there are T groups of data
. There are four integers at the beginning of each group of data, representing nmsw and the
next m lines respectively. Each line is two numbers l, r represents the left and right endpoints of the interval [l, r]

where T<=10

n<=10^6,m<=10^5

0<l<=r<=n

0<=w<=s<10^8

Output

Output one line for each set of data, representing the number of intervals that meet the requirements

SampleInput
4
1 0 0 0

1000000 0 0 0

1000000 1 0 0
1 1000000

10 10 20 14
2 10
5 9
5 5
6 8
2 6
9 10
6 7
6 10
4 5
5 7
SampleOutput
0
499999500000
499999500000
8

Give the code for now, I will add it - - 23333


#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;



#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e6+10;
const double EPS=1e-7;
const int mod=1000000007;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*lch[root] = build(L1,p-1,L2+1,L2+cnt);
    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/
/*lch[root] = build(L1,p-1,L2,L2+cnt-1);
    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}

int n,m,s,w;
int a[maxx],sum[maxx];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        me(a);me(sum);
        scanf("%d%d%d%d",&n,&m,&s,&w);
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[x]++;a[y+1]--;
        }
        for(int i=1;i<=n;i++)
        {
            a[i]=a[i-1]+a[i];
        }
        for(int i=1;i<=n;i++)
            sum[i]=sum[i-1]+a[i];
        LL ans=0,l=1,r=1;
        for(int i=1;i<=n;i++)
        {
            while(r<i&&sum[i]-sum[r]>=w)
                r++;
            while(l<r-1&&sum[i]-sum[l]>=s&&sum[r-1]-sum[l]>=w)
                l++;
            if(l<r&&sum[i]-sum[l-1]>=s&&sum[i]-sum[r-1]>=w&&
                sum[r-1]-sum[l-1]>=w)
                ans+=l;
        }
        printf("%lld\n",ans);
    }
}


























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326547244&siteId=291194637