QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#376321#6409. Classical Data Structure ProblemLainWA 0ms3756kbC++233.8kb2024-04-04 04:26:302024-04-04 04:26:30

Judging History

你现在查看的是最新测评结果

  • [2024-04-04 04:26:30]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3756kb
  • [2024-04-04 04:26:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
mt19937 rng(chrono::steady_clock::now().
            time_since_epoch().count());
typedef struct item * pitem;
int msk;
struct item {
  int prior;
  int value;
  int vl, vr;
  int sl, sr;
  int sum;
  int lz;
  item(int value, int vl, int vr):prior(rng()), value(value), vl(vl), vr(vr) {
    sum = ((LL)value * (vr - vl + 1)) & msk;
    sl = vl, sr = vr;
    l = r = nullptr;
    lz = 0;
  }
  pitem l, r;
};
/// 0-indexed
namespace Treap {
  int cnt(pitem it){return it!=nullptr?it->sr - it->sl + 1:0;}
  int L(pitem it){return it!=nullptr?it->sl:msk;}
  int R(pitem it){return it!=nullptr?it->sr:0;}
  LL sum(pitem it) {return it!=nullptr?it->sum:0;}
  void upd_cnt (pitem it) {
    if (it!=nullptr) {
      it->sl = min(L(it->l), L(it->r));
      it->sl = min(it->sl, it->vl);
      it->sr = max(R(it->l), R(it->r));
      it->sr = max(it->sr, it->vr);
      it->sum=(sum(it->l)+sum(it->r)+it->value * (it->vr - it->vl + 1)) & msk;
    }
  }
  void push (pitem it) {
    if (it!=nullptr && it->lz>0){
//      swap (it->l, it->r);
      it->value = (it -> value + it -> lz) & msk;
      it->sum = (it -> sum + (LL)it -> lz * (it -> sr - it -> sl + 1)) & msk;
      if (it->l)  it->l->lz = (it->l->lz + it->lz) & msk;
      if (it->r)  it->r->lz = (it->r->lz + it->lz) & msk;
        it -> lz = 0;
    }
  }
  void merge (pitem & t, pitem l, pitem r) {
    push (l); push (r);
    if (l==nullptr || r==nullptr)
      t = (l!=nullptr) ? l : r;
    else if (l->prior > r->prior)
      merge (l->r, l->r, r),  t = l;
    else merge (r->l, l, r->l),  t = r;
    upd_cnt (t);
  }
  /// r will have key-th and above
  void split(pitem t,pitem &l,pitem &r,pitem &mid, int key) {
    if (t==nullptr) { l = r = mid = nullptr; return; }
    push(t);
    if (key < (t -> vl))
      split (t->l, l, t->l, mid, key),  r = t;
    else if(key > t -> vr)
      split(t->r,t->r,r,mid,key), l=t;
    else{
//        pitem x = new item(t->value, t -> vl, key - 1);
//        pitem y = new item(t->value, key, t -> vr);
//        merge(l, t -> l, x);
//        merge(r, y, t -> r);
        mid = t;
        l = t -> l;
        r = t -> r;
        t = NULL;
    }
    if(t != NULL){
        upd_cnt(t);
    }
  }

  // Splits into two trees - one with segments < key, the other with >= key
  void split2(pitem t,pitem &l,pitem &r,int key) {
    pitem mid = NULL;
	// now l < key, r > key, mid contains key
    split(t, l, r, mid, key);
    if(mid == NULL)return;

	// Split mid into two segments
	// Is mid just one segment?
    pitem x = new item(mid->value, mid -> vl, key - 1);
    pitem y = new item(mid->value, key, mid -> vr);

	// l is now the values < key
    merge(l, l, x);
	// r is now the values >= key
    merge(r, y, r);
  }


  LL query(pitem &t, int l, int r, int i) {
    pitem t1, t2, t3;

	// t1 < l, t2 >= l
    split2(t, t1, t2, l);

	// t2 <= r, t3 > r
    split2(t2, t2, t3, r + 1);

	// Push down
    LL ans = (t2->sum + (LL)(r - l + 1) * i) & msk;
    t2 -> lz = (t2 -> lz + i) & msk;
    merge(t,t1,t2); merge(t,t,t3); return ans;
  }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    msk = (1 << 30) - 1;
    int msk2 = (1 << m) - 1;
    pitem root = new item(0, 0, msk2);
    int x = 0;
    for(int i = 1; i <= n; i++){
        int l, r;
        cin >> l >> r;
        l = (l + x) & msk2;
        r = (r + x) & msk2;
        if(l > r)swap(l, r);
//        cout << l << ' ' << r << endl;
        x = (x + Treap::query(root, l, r, i)) & msk;
//        cout << x << endl;
//        for(int j = 0; j <= msk2; j++){
//            cout << Treap::query(root, j, j, 0) << endl;
//        }
    }
    cout << x << "\n";
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3756kb

input:

5 2
2 1
1 3
3 2
1 0
0 2

output:

83

result:

wrong answer 1st numbers differ - expected: '87', found: '83'