## 题解：

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000100
#define MOD 1000000007
#include<bits/stdc++.h>
int n,m;
int p[maxn],a[maxn];
int nxt[maxn],pre[maxn];
set<pair<int,int> > S;
pair<int,int> c[maxn];

int time(int x,int y)
{
if(x == y)
return INF;
long long p1 = p[x],p2 = p[y];
if(x > y)
p2 = (p2 + a[y]) % m;
if(p2 < p1)
p2 += m;
if(p2 - p1 <= a[x])
return 1;
if(a[y] >= a[x])
return INF;
int l = 1,r = INF,ans = INF;
while(l <= r)
{
int mid = (l + r) >> 1;
if(p1 + 1ll*a[x]*mid >= p2 + 1ll*a[y]*(mid-1))
ans = mid,r = mid - 1;
else
l = mid + 1;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t,C = 1;
//scanf("%d",&t);
while(scanf("%d%d",&n,&m) != EOF)
{
for(int i = 0; i < n; i++)
{
scanf("%d%d",&p[i],&a[i]);
p[i]--;
c[i].first = p[i];
c[i].second = i;
}
sort(c,c+n);
for(int i = 0; i < n; i++)
{
nxt[c[i].second] = c[(i+1)%n].second;
pre[c[i].second] = c[(i-1+n)%n].second;
}
for(int i = 0; i < n; i++)
S.insert(make_pair(time(i,nxt[i]),i));
while(!S.empty())
{
pair<int,int> now = *S.begin();
if(now.first == INF)
break;
S.erase(now);
int x = now.second;
S.erase(make_pair(time(nxt[x],nxt[nxt[x]]),nxt[x]));
S.erase(make_pair(time(pre[x],x),pre[x]));
p[x] += now.first,a[x]--;
nxt[x] = nxt[nxt[x]];
pre[nxt[x]] = x;
S.insert(make_pair(time(pre[x],x),pre[x]));
S.insert(make_pair(time(x,nxt[x]),x));
}
printf("%d\n",S.size());
for(set<pair<int,int> >::iterator it = S.begin(); it != S.end(); it++)
printf("%d ",(*it).second+1);
printf("\n");
}
return 0;
}


#### Codeforces Round #375 (Div. 2) E One-Way Reform(欧拉路径,好题)

2016-10-05 16:35:04

#### ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) E. Team Work

2018-02-17 16:09:23

#### Codeforces Round #456 (Div. 2): E. Prime Gift（折半枚举）

2018-01-06 15:03:53

#### 【解题报告】Codeforces Round #367 (Div. 2)

2016-08-12 21:37:38

#### 解题报告：Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组）

2017-09-07 09:56:36

#### Codeforces Round #443 (Div. 2) E. Tournament

2017-11-01 16:14:48

#### 【CodeForces】CodeForces Round #464 (Div. 2) 题解

2018-02-18 11:05:43

#### Codeforces Round #392 (Div. 2)E. Broken Tree

2017-01-20 19:16:32

#### Codeforces Round #464 (Div. 2) E. Maximize!

2018-02-17 22:32:43

#### Codeforces Round #456 (Div. 2) E. Prime Gift（meet-in-the-middle）

2018-01-11 23:30:07

## 不良信息举报

Codeforces Round #342 (Div. 2) E. Frog Fights set 模拟★ ★