Submission #40528631


Source Code Expand

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
using namespace std;
const ll MAXN=1010;
ll n,m,q,a[MAXN],b[MAXN],lca;

ll calc(ll x,int mode=0){
	ll L=1,R=n,t=0,lt=0,rt=0;
	while(1){
		ll M=(L*a[t%m] + R*b[t%m]) / (a[t%m] + b[t%m]);
		if(M==x)break;
		if(M>x)R=M-1,rt+=(t>=lca);else L=M+1,lt+=(t>=lca);
		t++;
	}
	return (mode==0) * t + (mode==1) * lt + (mode==2) * rt;
}
ll calc2(ll x,ll y){
	ll L=1,R=n,t=0;
	while(1){
		ll M=(L*a[t%m] + R*b[t%m]) / (a[t%m] + b[t%m]);
		if(M==x || M==y)break;
		if((M>x) != (M>y))break;
		t++;
		if(M>x)R=M-1;else L=M+1;
	}

	return t;
}

void solve(ll L,ll R){
	lca=calc2(L,R);
	ll val=calc(L) + calc(R) - 2*lca;

	ll ans=2*(R-L+calc(L,1)+calc(R,2));

	ans-=val;
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);

	cin>>n>>m;
	rep(i,0,m-1)cin>>a[i]>>b[i];
	cin>>q;
	rep(i,1,q){
		ll L,R;cin>>L>>R;
		solve(L,R);
	}


    return 0;
}

Submission Info

Submission Time
Task E - Difference Sum Query
User Crying
Language C++ (GCC 9.2.1)
Score 900
Code Size 1261 Byte
Status AC
Exec Time 102 ms
Memory 3636 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 2
AC × 48
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_srnd_00.txt, 01_srnd_01.txt, 01_srnd_02.txt, 01_srnd_03.txt, 01_srnd_04.txt, 01_srnd_05.txt, 01_srnd_06.txt, 01_srnd_07.txt, 01_srnd_08.txt, 01_srnd_09.txt, 02_rnd_00.txt, 02_rnd_01.txt, 02_rnd_02.txt, 02_rnd_03.txt, 02_rnd_04.txt, 02_rnd_05.txt, 02_rnd_06.txt, 02_rnd_07.txt, 02_rnd_08.txt, 02_rnd_09.txt, 03_worst_00.txt, 03_worst_01.txt, 03_worst_02.txt, 03_worst_03.txt, 03_worst_04.txt, 03_worst_05.txt, 03_worst_06.txt, 03_worst_07.txt, 04_max_00.txt, 05_min_00.txt, 06_root_00.txt, 06_root_01.txt, 06_root_02.txt, 07_sall_00.txt, 07_sall_01.txt, 07_sall_02.txt, 07_sall_03.txt, 07_sall_04.txt, 07_sall_05.txt, 07_sall_06.txt, 07_sall_07.txt, 08_edge_00.txt, 08_edge_01.txt, 08_edge_02.txt, 08_edge_03.txt, 08_edge_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 7 ms 3536 KiB
00_sample_01.txt AC 2 ms 3628 KiB
01_srnd_00.txt AC 46 ms 3532 KiB
01_srnd_01.txt AC 38 ms 3608 KiB
01_srnd_02.txt AC 41 ms 3536 KiB
01_srnd_03.txt AC 40 ms 3484 KiB
01_srnd_04.txt AC 39 ms 3576 KiB
01_srnd_05.txt AC 38 ms 3524 KiB
01_srnd_06.txt AC 40 ms 3476 KiB
01_srnd_07.txt AC 39 ms 3560 KiB
01_srnd_08.txt AC 42 ms 3524 KiB
01_srnd_09.txt AC 39 ms 3516 KiB
02_rnd_00.txt AC 76 ms 3516 KiB
02_rnd_01.txt AC 74 ms 3520 KiB
02_rnd_02.txt AC 75 ms 3536 KiB
02_rnd_03.txt AC 75 ms 3540 KiB
02_rnd_04.txt AC 80 ms 3488 KiB
02_rnd_05.txt AC 75 ms 3536 KiB
02_rnd_06.txt AC 75 ms 3556 KiB
02_rnd_07.txt AC 73 ms 3576 KiB
02_rnd_08.txt AC 76 ms 3624 KiB
02_rnd_09.txt AC 73 ms 3624 KiB
03_worst_00.txt AC 100 ms 3560 KiB
03_worst_01.txt AC 59 ms 3472 KiB
03_worst_02.txt AC 55 ms 3512 KiB
03_worst_03.txt AC 98 ms 3536 KiB
03_worst_04.txt AC 98 ms 3612 KiB
03_worst_05.txt AC 57 ms 3536 KiB
03_worst_06.txt AC 58 ms 3580 KiB
03_worst_07.txt AC 102 ms 3580 KiB
04_max_00.txt AC 61 ms 3520 KiB
05_min_00.txt AC 4 ms 3592 KiB
06_root_00.txt AC 53 ms 3540 KiB
06_root_01.txt AC 54 ms 3560 KiB
06_root_02.txt AC 54 ms 3636 KiB
07_sall_00.txt AC 6 ms 3624 KiB
07_sall_01.txt AC 6 ms 3516 KiB
07_sall_02.txt AC 2 ms 3580 KiB
07_sall_03.txt AC 2 ms 3520 KiB
07_sall_04.txt AC 2 ms 3620 KiB
07_sall_05.txt AC 2 ms 3516 KiB
07_sall_06.txt AC 29 ms 3560 KiB
07_sall_07.txt AC 33 ms 3604 KiB
08_edge_00.txt AC 64 ms 3608 KiB
08_edge_01.txt AC 64 ms 3580 KiB
08_edge_02.txt AC 63 ms 3608 KiB
08_edge_03.txt AC 63 ms 3536 KiB
08_edge_04.txt AC 64 ms 3608 KiB